代码之家  ›  专栏  ›  技术社区  ›  NLed

如何在matlab中屏蔽图像的一部分?

  •  3
  • NLed  · 技术社区  · 14 年前

    我想知道如何隐藏黑白图像的一部分?

    我得到了一个需要边缘检测的对象,但是我在背景中有其他白色干扰对象,它们在目标对象之下…我想把图像的整个下半部分遮罩成黑色,我该怎么做?

    谢谢!!

    编辑

    我还想遮罩一些其他部分(顶部)。我该怎么做?

    请解释代码,因为我真的很想了解它是如何工作的,并在我自己的理解中实现它。

    编辑2

    我的图像是480x640…有没有办法掩盖特定像素?例如,图像中的180x440…

    2 回复  |  直到 8 年前
        1
  •  6
  •   gnovice    14 年前

    如果你有 2-D grayscale intensity image 存储在矩阵中 A ,可以通过执行以下操作将下半部分设置为黑色:

    centerIndex = round(size(A,1)/2);         %# Get the center index for the rows
    A(centerIndex:end,:) = cast(0,class(A));  %# Set the lower half to the value
                                              %#   0 (of the same type as A)
    

    这是通过首先获取 使用函数 SIZE ,除以2,四舍五入,得到靠近图像高度中心的整数索引。然后,向量 centerIndex:end 索引从中心索引到结尾的所有行,以及 : 索引所有列。所有这些索引元素都设置为0表示黑色。

    函数 CLASS 用于获取的数据类型 以便可以使用函数将0强制转换为该类型 CAST . 这个 可以 不过,这不是必需的,因为0可能会自动转换为 没有他们。

    如果要创建 logical index 要用作遮罩,可以执行以下操作:

    mask = true(size(A));  %# Create a matrix of true values the same size as A
    centerIndex = round(size(A,1)/2);  %# Get the center index for the rows
    mask(centerIndex:end,:) = false;   %# Set the lower half to false
    

    现在, mask 是一个逻辑矩阵 true (即“1”)表示要保留的像素,以及 false (即“0”)表示要设置为0的像素。您可以设置更多元素 面具 如你所愿。然后,当要应用遮罩时,可以执行以下操作:

    A(~mask) = 0;  %# Set all elements in A corresponding
                   %#   to false values in mask to 0
    
        2
  •  0
  •   Jerry T    8 年前
    function masked = maskout(src,mask)
        % mask: binary, same size as src, but does not have to be same data type (int vs logical)
        % src: rgb or gray image
        masked = bsxfun(@times, src, cast(mask,class(src)));
    end