Using rgb2ind for colour animated gif
23 views (last 30 days)
Show older comments
I have a workaround but this is a request for something more elegant.
I'd like to write a colour animated gif from a series of images (not captured from a figure window, although this is still applicable).
to ensure my colour images are not converted to indexed greyscale I have to "stitch" all the frames into a single image, then run rgb2ind to obtain the optimised colormap for all images in the sequence. Then I convert all individual frames to indexed colour using rgb2ind and then finally use imwrite to save the gif:
a=imread('frame1.jpg']);
b=imread('frame2.jpg']);
temp=[a;b];
[~,map]=rgb2ind(temp,256);
c(:,:,:,1)=rgb2ind(a,map);
c(:,:,:,2)=rgb2ind(b,map);
imwrite(c,map,'myGif.gif','Loopcount',inf,'DelayTime',0.5)
So the requests are:
- Is there a more elegant way of doing this that I'm missing?
- Can rgb2ind be made to work with 4D (or indeed any dimension) of array? it's just a bunch of numbers after all...
- Can imwrite have a colormap switches for gifs like 'colormap', 'optimise'?
0 Comments
Answers (1)
DGM
on 25 Jul 2022
Edited: DGM
on 25 Jul 2022
I posted this already as a comment in the other thread, but I'll put it here too:
MIMT gifwrite() works to write I/IA/RGB/RGBA and indexed color images from 4D arrays. MIMT gifread() can read the image with minimal hassle.
Pay attention to the synopsis if you intend to read GIF files in versions newer than R2018a -- regardless of what tools you're using. If you're using a newer version, you will not be able to correctly read most multiframe color GIF files unless you make that effort.
Stitching the frames together to generate a colormap is a bad idea, since it means that no frame can have a unique optimized local color table. The more dynamic the color content is over the sequence, the worse the entire image will look.
If you don't use third-party tools like those to do the job, the canonical way to do this with imwrite() is tedious. That's just the way it is. You would have to write a loop and quantize each frame individually if you want good color range. The same goes for reading the file.
0 Comments
See Also
Categories
Find more on Blue in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!