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 = 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', []);

getframe函数也是得到相同的数据结构,frame2im函数将此数据结构转换为一个数组,相当于提取cdata数据,里面存储的是R、G、B的数据,rgb2ind函数相当于将RGB图像转换为索引图像,imind为索引值,cm为索引矩阵。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值