登录论坛

查看完整版本 : 如何在Matlab中掩盖部分图像?


poster
2019-12-10, 20:41
我想知道如何遮盖黑白色图像的一部分?

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

谢谢 !!

编辑

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

请解释一下代码,因为我很想了解它的工作原理并以自己的理解实施。

编辑2

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



回答:

如果矩阵A存储了二维灰度强度图像 (http://www.mathworks.com/access/helpdesk/help/techdoc/creating_plots/f2-10709.html#f2-175) ,则可以通过执行以下操作将下半部分设置为黑色:

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 (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/size.html)获取A的行数,然后将其除以2,然后四舍五入以获得靠近图像高度中心的整数索引。然后,向量centerIndex:end索引从中心索引到centerIndex:end所有行,而:索引所有列。所有这些索引元素均设置为0以表示黑色。

函数CLASS (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/class.html)用于获取A的数据类型,以便可以使用函数CAST (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/cast.html)将0强制转换为该类型。不过,这可能不是必需的,因为没有它们,0可能会自动转换为A的类型。

如果要创建用作屏蔽的逻辑索引 (http://www.mathworks.com/access/helpdesk/help/techdoc/math/f1-85462.html#bq7egb6-1) ,可以执行以下操作:

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”)。您可以根据需要将更多mask元素设置为false 。然后,当您要应用遮罩时,可以执行以下操作:

A(~mask) = 0; %# Set all elements in A corresponding %# to false values in mask to 0

更多&回答... (https://stackoverflow.com/questions/2841882)