matlab|matlab gif文件生成
frame包含RGB图像信息和颜色映射表
image只是RGB图像
indexed image:
索引图像是一种把像素值直接作为RGB调色板下标的图像。索引图像可把像素值直接映射为调色板数值。
调色板通常与索引图像存储在一起,装载图像时,调色板将和图像一同自动装载。
索引模式和灰度模式比较类似,它的每个象素点也可以有256种颜色容量,但它可以负载彩色。索引模式的图像最多只能有256种颜色。当图像转换成索引模式时,系统会自动根据图像上的颜色归纳出能代表大多数的256种颜色,就象一张颜色表,然后用这256种来代替整个图像上所有的颜色信息。
Gif文件可能只能显示索引图像。
生成方法1:
frame = getframe(1);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if n == 1;
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
生成方法2:
X(m,n,1,k)=floor(a*X1(m,n)+b*X2(m,n));
imwrite(X,map,'225','gif')
读取方法:
[Ii, map] = imread('pig.gif', 'frames', i);
F(:, i) = im2frame(flipud(Ii), map);
有趣代码:
H={'*.bmp';
'*.jpg';
'*.gif';
'*.tif';
'*.tga'};
[filename,pathname]=uigetfile(H,'请选择需要打开的图像');
file=strcat(pathname,filename);
I=imread(file);
figure('NumberTitle', 'off', 'ToolBar', 'none', 'Menu', 'none');
pos = get(gcf, 'position');
set(gcf, 'position', [pos(1) pos(2) W H]);
set(gca, 'position', [0 0 1 1]);
一个gif的例子:讲avi文件转换为gif文件
代码如下
%%
function main
clc
clear all
close all
%%
fileavi = 'Tunning_Fork10.avi';
[pathstr, name] = fileparts(fileavi);
filegif = fullfile(pathstr,[name '.gif']);
%%
xyloObj = mmreader(fileavi);
% mmreader很好用
nFrames = xyloObj.NumberOfFrames;
vidHeight = xyloObj.Height;
vidWidth = xyloObj.Width;
%% Preallocate movie structure.
mov(1:nFrames) = struct('cdata', zeros(vidHeight, vidWidth, 3, 'uint8'), 'colormap', []);
for k = 1 : nFrames
mov(k).cdata = https://www.it610.com/article/read(xyloObj, k);
writetogif(mov(k),filegif,k,inf,0.05);
end
% Size a figure based on the video's width and height.
hf = figure;
set(hf, 'position', [150 150 vidWidth vidHeight])
% Play back the movie once at the video's frame rate.
movie(hf, mov, 1, xyloObj.FrameRate);
end
%% 向gif文件写图片
function writetogif(frame,file,k,loopcount,delaytime)
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if k == 1;
imwrite(imind,cm,file,'gif', 'Loopcount',loopcount);
else
imwrite(imind,cm,file,'gif','WriteMode','append','DelayTime',delaytime);
end
end
从上面也可以看到,从avi读取的每一幅图像都是一个数据结构,包含cdata和colormap两个域,即
struct('cdata', zeros(vidHeight, vidWidth, 3, 'uint8'),...
'colormap', []);
【matlab|matlab gif文件生成】 getframe函数也是得到相同的数据结构,frame2im函数将此数据结构转换为一个数组,相当于提取cdata数据,里面存储的是R、G、B的数据,rgb2ind函数相当于将RGB图像转换为索引图像,imind为索引值,cm为索引矩阵。
推荐阅读
- django-前后端交互
- 如何在Mac中的文件选择框中打开系统隐藏文件夹
- 使用composer自动加载类文件
- ssh生成公钥秘钥
- Android系统启动之init.rc文件解析过程
- 微信小程序基础知识
- 误删/清空.bashrc文件/内容
- JavaScript|vue 基于axios封装request接口请求——request.js文件
- hough变换检测的matlab程序
- 插件化无法获取或找到.so文件