3

我想知道如何遮盖黑白图像的一部分?

我有一个需要进行边缘检测的对象,但我在目标对象下方的背景中有其他白色干扰对象...我想将图像的整个下部遮盖为黑色,我该怎么做?

谢谢 !!

编辑

我还想掩盖其他一些部分(顶部)......我该怎么做?

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

编辑2

我的图像是 480x640 ... 有没有办法屏蔽特定像素?例如图像中的 180x440 ...

4

2 回答 2

6

如果您有存储在 matrix中的2-D 灰度强度图像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)

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

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

如果要创建逻辑索引以用作掩码,可以执行以下操作:

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”)用于您想要保留的像素, (即“0”)用于false您想要设置为 0 的像素。您可以根据需要设置更多元素。然后,当您想应用遮罩时,您可以执行以下操作:maskfalse

A(~mask) = 0;  %# Set all elements in A corresponding
               %#   to false values in mask to 0
于 2010-05-15T22:01:07.437 回答
0
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
于 2016-07-23T23:03:26.963 回答