【问题标题】:Read Animated GIF with transparency or alpha channel with LeaveInPlace Disposal Method使用 LeaveInPlace Disposal Method 读取具有透明度或 Alpha 通道的动画 GIF
【发布时间】:2023-03-23 23:16:01
【问题描述】:

当我阅读这个GIF然后用imshow(I(:,:,:,2),map);显示它时,gif的框架上有污点,我认为可能是因为GIF处理方法。如何处理?

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

这就是我得到的。

【问题讨论】:

    标签: matlab gif animated-gif


    【解决方案1】:

    使用下面的代码imshow 带有 alpha 通道的动画 gif 的第 2 帧。

    这里只有 gif 的第一帧有完整的笑脸图像,笑脸的背景包含TransparentColor。在其余帧中,一些笑脸像素也设置为TransparentColor。因此,要获取其他帧,您必须将 TransparentColor 像素替换为前一帧 rgb 图像中的像素。这就像将所需的帧放在前面的帧之上以获得完整的图像。

    % 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);
    

    由于此 gif 具有透明的像素。在这里,我使用白色填充透明部分。如果您需要不同的颜色更改[1 1 1] in

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

    到所需的值。每个值都与0 to 1 不同。其中 0 表示完全不存在该颜色分量,而 1 表示完全存在该颜色分量。在颜色之间使用十进制值(0.25,0.7,....等)

    【讨论】:

    • @Fitri 我已经用逐行解释更新了答案。
    • 我也更新了代码。以前的代码仅适用于第 1 帧和第 2 帧。我已更正它,现在它适用于所有帧
    猜你喜欢
    • 2010-10-25
    • 2016-04-30
    • 2021-03-29
    • 1970-01-01
    • 2012-01-24
    • 1970-01-01
    • 2010-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多