Fitri
Fitri

Reputation: 47

Read Animated GIF with transparency or alpha channel with LeaveInPlace Disposal Method

When I read this GIF and then show it with imshow(I(:,:,:,2),map); there are stains on the frame of the gif, I think it may because the GIF disposalMethod. How to deal with it?

intended, animated gif

[I map]=imread('smile.gif');

This is what i got.

my result, with stains

Upvotes: 2

Views: 464

Answers (1)

Xcross
Xcross

Reputation: 2132

Use the below code to imshow frame 2 of the animated gif with alpha channel.

In here only the first frame of the gif has the full smiley image and the background of smiley contains TransparentColor. In rest of the frames some pixels of smiley are also set as TransparentColor. So to get the other frames you have to replace the TransparentColor pixels with the pixels from rgb image of previous frame. It is like placing the required frame on top of the previous frames to get the full image.

% which frame to show
frame=2;

% filename of gif image
filename='smile.gif';

% Reading gif image
[I map]=imread(filename);

% get information from graphics file( we need the TransparentColor 
% and ColorTable of the gif)
info=imfinfo(filename);

% Set the transparent color to what ever color you like. 
% Because this will be the background color for frame 1 and this
% will be copied to the next frames.
info(1).ColorTable(TransparentColor,:)=[1 1 1];
% RGB Image of first frame
im_new=ind2rgb(I(:,:,:,1),info(1).ColorTable);

% loop from second to the required frame
for frameIndex=2:frame
% get information from graphics file( we need the TransparentColor 
% and ColorTable of the gif)
info=imfinfo(filename);    

% Get the transparentColor of current frame
TransparentColor=info(frameIndex).TransparentColor;

% Change that transparentColor in the map to [NaN NaN NaN]
info(frameIndex).ColorTable(TransparentColor,:)=[NaN NaN NaN];

% Generate rgb image with I and modified color table
imNaN=ind2rgb(I(:,:,:,frameIndex),info(frameIndex).ColorTable);

% We are setting it as [NaN NaN NaN] because then we can find 
% thoses transparent pixels using 'isnan(imNaN)'. 

% Change that transparentColor in the map to [0 0 0] and generate 
% another rgb image
info(frameIndex).ColorTable(TransparentColor,:)=[0 0 0];
im=ind2rgb(I(:,:,:,frameIndex),info(frameIndex).ColorTable);

% 'im' will have [0 0 0] in pixel places of [NaN NaN NaN].
% We are putting zero here because then we can find where there 
% is NaN in 'imNaN' and get those pixels from the previous rgb frame and 
% add thoses with the zero in 'im'.

% copy the previous rgb frame to 'im0'
im0=im_new;

% Now as said before we are going to find the pixels with 
% 'NaN' present using 'isnan(imNaN)' this will be one or zero 
% for each pixel. If the pixel is 'NaN' then 'one' otherwise 'zero'.
% Now we mutiply this with the coresponding pixel in frame one. If the
% pixel is not 'NaN' then the product will zero otherwise pixel 
% value of im0. We add this to 'im'. Which has 'zero' instead of 'NaN'. 
% The result will be, where there is 'NaN', pixel from 'im0' is copied 
% to im_new otherwise 'im_new' will have 'im'.
im_new=((isnan(imNaN).*im0))+im;

% This is repeated till the required frame is reached
end

% show image
imshow(im_new);

Since this gif has pixels that are transparent. In here I have used white to fill the transparent part. If you need a different color change [1 1 1] in

info(1).ColorTable(TransparentColor,:)=[1 1 1];

to the required value. Each value varies from 0 to 1. For which 0 is complete absence of that color component and 1 is complete presence of that color component. For in between colors use decimal values (0.25,0.7,....etc)

Upvotes: 1

Related Questions