MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   如何在Matlab中掩盖部分图像? (https://www.labfans.com/bbs/showthread.php?t=23431)

poster 2019-12-10 20:41

如何在Matlab中掩盖部分图像?
 
我想知道如何遮盖黑白色图像的一部分?

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

谢谢 !!

[B]编辑[/B]

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

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

[B]编辑2[/B]

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



[B]回答:[/B]

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

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

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

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

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

[url=https://stackoverflow.com/questions/2841882]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 01:09

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.