Using rgb2ind for colour animated gif

조회 수: 10(최근 30일)
Will Reeves
Will Reeves 2022년 7월 25일
편집: DGM 2022년 7월 25일
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);
Error using matlab.internal.lang.capability.Capability.require
This functionality is not available on remote platforms.

Error in implay (line 51)
matlab.internal.lang.capability.Capability.require(...
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'?

답변(1개)

DGM
DGM 2022년 7월 25일
편집: DGM 2022년 7월 25일
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.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by