【问题标题】:Displaying a png image without the background显示没有背景的 png 图像
【发布时间】:2014-06-03 08:05:54
【问题描述】:

http://sweetclipart.com/multisite/sweetclipart/files/sunglasses_black.png

我已经在 MATLAB 中使用 [X,map,alpha]=imread('...','png') 读取了 png 图像。 现在我想把这个 png 图像放在另一个图像上。但我希望不显示读取的 png 的背景颜色。在链接中,我希望仅显示太阳镜而没有“白色”背景(背景是另一张图片)。

【问题讨论】:

    标签: matlab image-processing png


    【解决方案1】:

    Alpha 通道是不透明度,与透明度相反。

    MATLAB 图形通过 AlphaData 属性支持 alpha 混合:

    background = uint8(255*rand(size(alpha)));
    imshow(background)
    hold on
    h = imshow(X);
    set(h, 'AlphaData', alpha)
    

    结果:

    给定另一个图像 Y 和您的图像 X 及其 alpha 数据,您可以使用 alpha 生成混合图像。在您的情况下,alpha 只有值 0 和 255,但通常可以有不同的不透明度。在这个简单的例子中,同样有噪声背景,但是是彩色的:

    out = uint8(255*rand(size(X))); % Y
    hardMask = repmat(alpha==255,1,1,3);
    out(hardMask) = X(hardMask);
    imwrite(out,'sunglass_alphaC.png')
    

    这是一张大图,所以我在这里调整了输出大小:

    如果您的 Alpha 通道实际上具有两个以上的透明度,您可以与背景混合:

    s = double(alpha)/255;
    Yout = uint8((bsxfun(@times,1-s,double(Y)) + bsxfun(@times,s,double(X))));
    

    对于灰度,bsxfun 可以仅替换为 .-(例如 s.*double(X))。

    【讨论】:

      【解决方案2】:

      你可以这样做:

      % Image with alpha channel
      [glasses, ~, alpha] = imread('http://sweetclipart.com/multisite/sweetclipart/files/sunglasses_black.png');
      
      % OPTIONAL: Let's rescale it (it's very big!)
      glasses = imresize(glasses, 0.1);
      alpha = imresize(alpha, 0.1);
      
      % An image of a person (let's put the glasses on the person).
      person = imread('http://cinemacao.com/wp-content/uploads/2013/12/Scarlett-CAPA-2.jpg');
      
      % Lets make the alpha MxNx3 (so we can combine it with the RGB channels).
      alpha = repmat(alpha, [1 1 3]);
      
      % And convert everything from uint8 to double (to avoid precision issues).
      glasses = im2double(glasses);
      alpha = im2double(alpha);
      person = im2double(person);
      
      % Final image
      
      % Let x,y be the top-left coordinates where we'll put the glasses.
      x = 440;
      y = 450;
      
      % Let's combine the images.
      img3 = person;
      img3(y:y+size(glasses,1)-1, x:x+size(glasses,2)-1, :) = ...
          glasses .* alpha + ...
          person(y:y+size(glasses,1)-1, x:x+size(glasses,2)-1, :) .* (1 - alpha);
      
      % An display the result.
      imshow(img3);
      

      结果:

      【讨论】:

      • 天哪,非常感谢你。你能解释一下最后一个“组合”部分发生了什么(“...”和“.*”是什么意思)?
      • 当然。 ... 只是告诉 Matlab 命令被分成多行的一种方式,... 后面的行是当前行的一部分。 .* 运算符是一个点积运算符。它需要两个大小相同的矩阵 A 和 B,并产生一个矩阵 C(大小相同),其中每个 C(i,j) = A(i,j) * B(i,j)。会发生什么:首先是img3 = person。然后,我们取 img3 的感兴趣部分(与眼镜大小相同)并将其替换为那里的内容和眼镜的加权和。 alpha 规定什么是“人”,(1 - alpha) 规定什么是“眼镜”。
      【解决方案3】:

      我对Matlab不太了解,但似乎是[X,map,alpha]使用“alpha”作为alpha通道; Alpha 通道表示透明度级别。 (可能你已经知道了)。还要检查图像本身是否设置了 alpha 通道。默认情况下,PNG 不将“白色”识别为 Alpha 通道。在这种情况下,去你最喜欢的“*shop”软件编辑照片(也许用魔术工具选择将作为背景,去选择反向,复制粘贴到之前指定背景图像为透明的新图像)。

      【讨论】:

      • 如何解释这个 Alpha 通道数据?我必须编写一些代码来仅复制不透明的像素。
      • 嗯不知道你到底需要什么,如果你有数据结构中的图像,只需尝试从 alpha 通道获取字节。字节等于 1 表示 100% 透明,0 表示 0% 透明。它们也可以是 256 代表 100% 和 0 代表 0%。检查 imagea 是否有 Xor 函数,这样你就可以对两个图像进行异或重叠
      猜你喜欢
      • 2012-10-16
      • 2017-04-27
      • 2023-04-10
      • 2011-08-23
      • 2018-03-15
      • 2013-09-26
      • 2018-10-31
      • 1970-01-01
      • 2021-08-08
      相关资源
      最近更新 更多