如何在matlab中遮盖图像的一部分?

发布于 2024-09-01 12:24:15 字数 316 浏览 9 评论 0原文

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

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

谢谢 !!

编辑

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

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

EDIT2

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

I would like to know how to mask part of an image that is in BLACK & WHITE ?

I got an object that needs to be edge detected, but I have other white interfering objects in the background that are below the target objet ... I would like to mask the entire lower part of an image to black, how can I do that ?

Thanks !!

EDIT

I also want to mask some other parts (top part) ... how can I do that ?

Please explain the code because I really wnat to learn how it works and implement it in my own understanding.

EDIT2

My image is 480x640 ... Is there a way to mask specific pixels ? for example 180x440 from the image ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过度放纵 2024-09-08 12:24:15

如果您有二维灰度强度图像存储在矩阵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,然后四舍五入off 以获得图像高度中心附近的整数索引。然后,向量 centerIndex:end 索引从中心索引到末尾的所有行,并且 : 索引所有列。所有这些索引元素都设置为 0 以表示黑色。

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

如果您想创建逻辑索引< /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”)代表您想要保留的像素和 false(即“0”)表示要设置为 0 的像素。您可以根据需要将 mask 的更多元素设置为 false 。然后,当您想要敷面膜时,可以执行以下操作:

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

If you have a 2-D grayscale intensity image stored in matrix A, you can set the lower half to black by doing the following:

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)

This works by first getting the number of rows in A using the function SIZE, dividing that by 2, and rounding it off to get an integer index near the center of the image height. Then, the vector centerIndex:end indexes all the rows from the center index to the end, and : indexes all the columns. All of these indexed elements are set to 0 to represent the color black.

The function CLASS is used to get the data type of A so that 0 can be cast to that type using the function CAST. This may not be necessary, though, as 0 will probably be automatically converted to the type of A without them.

If you want to create a logical index to use as a mask, you can do the following:

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

Now, mask is a logical matrix with true (i.e. "1") for pixels you want to keep and false (i.e. "0") for pixels you want to set to 0. You can set more elements of mask to false as you wish. Then, when you want to apply the mask, you can do the following:

A(~mask) = 0;  %# Set all elements in A corresponding
               %#   to false values in mask to 0
一人独醉 2024-09-08 12:24:15
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文