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

NLe*_*Led 3 matlab mask image-processing

我想知道如何屏蔽BLACK&WHITE中的部分图像?

我有一个需要边缘检测到的物体,但我在背景中有其他白色干扰物体在目标物体下方...我想将图像的整个下半部分掩盖为黑色,我该怎么做?

谢谢 !!

编辑

我也想掩盖一些其他部分(上半部分)......我该怎么做?

请解释一下代码,因为我真的想知道它是如何工作的,并按照我自己的理解实现它.

EDIT2

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

gno*_*ice 6

如果您在矩阵中存储了二维灰度强度图像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)
Run Code Online (Sandbox Code Playgroud)

这首先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
Run Code Online (Sandbox Code Playgroud)

现在,mask是一个合乎逻辑的矩阵true(即"1")的像素要保持和false(即"0"),您要设置为0.您可以设置多个元素的像素maskfalse你的愿望.然后,当您想要应用蒙版时,您可以执行以下操作:

A(~mask) = 0;  %# Set all elements in A corresponding
               %#   to false values in mask to 0
Run Code Online (Sandbox Code Playgroud)