【问题标题】:How to multiply binary image and rgb image in matlab?如何在matlab中将二进制图像和rgb图像相乘?
【发布时间】:2012-12-13 19:27:38
【问题描述】:

我有一个二进制图像,它是另一个彩色图像的分割形式。

如您所知,二进制图像是 2-d,而 rgb 图像是 3-d,我如何将它们相乘?

我尝试了这段代码,结果是一张奇怪的图片

function  skinCrop(bwSkin,colorSkin)

for i = 1:size(colorSkin,1)
    for j = 1:size(colorSkin,1)
        if bwSkin(i,j) == 0
            colorSkin(i,j,:) = 0;
        end
    end
end
imshow(colorSkin);
end

原图为

生成的图像是:

我原以为它是黑色背景下的一只手,那为什么右边的部分会这样呢?

【问题讨论】:

    标签: matlab matrix-multiplication image-segmentation


    【解决方案1】:

    在 matlab 中不需要时应避免循环:

    mask = cat(3,bwSkin,bwSkin,bwSkin);
    output = mask.*colorSkin;
    

    您可能必须更改类型才能使乘法成功:

    output = uint8(mask).*colorSkin;
    

    或者:

    output = double(mask).*colorSkin;
    

    【讨论】:

      【解决方案2】:

      您的第二个维度使用了错误的维度长度:

      for j = 1:size(colorSkin,1)
      

      应该是

      for j = 1:size(colorSkin,2)
      

      【讨论】:

        【解决方案3】:

        一种更有效的乘法方法是

        function mult = skinCrop( bwSkin, colorSkin )
        % 
        % multiplying 3D color image with 2D mask
        %
        
        mult = nsxfun( @times, bwSkin, colorSkin );
        
        % show the result
        imshow( mult ); title('skin cropped image');
        

        正如@zenopy 所说,您可能需要将变量转换为double 类型。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-08-15
          • 1970-01-01
          • 2012-04-26
          • 2011-09-15
          • 1970-01-01
          • 2020-09-02
          • 1970-01-01
          • 2012-10-29
          相关资源
          最近更新 更多