(MATLAB) Colormap for video/animation

廉价感情. 提交于 2019-12-13 02:23:51

问题


I have some code that produces a series of grayscale images. I am then able to save the images individually in a loop and save them with a colormap applied. i.e.

file = sprintf('image_%04d.png',x);
imwrite(image1,jet,file,'png');

So i get my images out the other end and they have the correct colormapping which is colormap(jet).

However, when in my next program, I try to cobble these images together to form a short animation (yes I know I should just make the movie in the same loop as above), I get a grayscale movie!!! How does that happen when the source images are color?

Further more I notice that if I load an individual image and then type:

imshow(A)

i get a grayscale image too! but if I type:

image(A)

it gives me what I wanted, which is the colormapped image.

Why does it do this? how can I get it to make a movie with the correct colormap? and is there a way I can add the colormap to the image before saving it (as above I add the map during imwrite)?

p.s. I have tried using :

video = videowriter(VideoFileName,'MPEG-4')
video.Colormap = jet  (or) colormap(jet) or 'jet'

matlab doesnt like that. :(


回答1:


Alex, VideoWriter has a profile called 'Indexed AVI' which will allow you to save the image with the colormap information. You can use the code below:

vwObj = VideoWriter('myfile.avi', 'Indexed AVI');
vwObj.Colormap = jet(256);
open(vwObj);
writeVideo(vwObj, image1);   % Repeat for all images that you want
close(vwObj);

MPEG-4 files do not accept a Colormap property. However you can specify a colormap at the time of writing by supplying a MATLAB frame as bellow:

vwObj = VideoWriter('myfile', 'MPEG-4');
open(vwObj);
f.cdata = image1;
f.colormap = jet(256);
% The colormap will be applied before writing the data to the MPEG4 file
writeVideo(vwObj, f); 
close(vwObj);

Hope this helps.

Dinesh



来源:https://stackoverflow.com/questions/18277333/matlab-colormap-for-video-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!