MATLAB 中的轮廓检测

发布于 2024-11-04 07:10:11 字数 442 浏览 1 评论 0原文

我试图理解这段代码:

d=edge(d,'canny',.6);
figure,
imshow(d,[])

ds = bwareaopen(d,40);
figure,
imshow(ds,[])

iout = d1;
BW=ds;

iout(:,:,1) = iout;
iout(:,:,2) = iout(:,:,1);
iout(:,:,3) = iout(:,:,1);
iout(:,:,2) = min(iout(:,:,2) + BW, 1.0);
iout(:,:,3) = min(iout(:,:,3) + BW, 1.0);

我知道 d 是图像,并且应用了精明的检测器,并且忽略了 40 个像素。图像是灰度的,并且轮廓被添加到图像中。

您能解释一下接下来的几行吗?这里使用了什么原理/算法?我遇到了麻烦,尤其是代码的轮廓检测部分。

I am trying to understand this code:

d=edge(d,'canny',.6);
figure,
imshow(d,[])

ds = bwareaopen(d,40);
figure,
imshow(ds,[])

iout = d1;
BW=ds;

iout(:,:,1) = iout;
iout(:,:,2) = iout(:,:,1);
iout(:,:,3) = iout(:,:,1);
iout(:,:,2) = min(iout(:,:,2) + BW, 1.0);
iout(:,:,3) = min(iout(:,:,3) + BW, 1.0);

I understand that d is the image and canny detector is applied and 40 pixels are neglected. The image is gray scale and contour is added to the image.

Can you please explain the next lines? What principle/algorithm is used here? I am having trouble especially with the contour detection portion of the code.

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

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

发布评论

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

评论(1

痴意少年 2024-11-11 07:10:11

假设变量 d1 存储了原始 灰度强度图像 进行操作,然后最后 5 行将该灰度图像转换为 3-D RGB 图像 iout 看起来与原始灰度图像相同,只是轮廓将覆盖在青色图像上。

下面是一个使用 MATLAB 图像处理工具箱

d1 = double(imread('cameraman.tif'))./255;  % Load the image, scale from 0 to 1
subplot(2, 2, 1); imshow(d1); title('d1');  % Plot the original image
d = edge(d1, 'canny', .6);                  % Perform Canny edge detection
subplot(2, 2, 2); imshow(d); title('d');    % Plot the edges
ds = bwareaopen(d, 40);                     % Remove small edge objects
subplot(2, 2, 3); imshow(ds); title('ds');  % Plot the remaining edges
iout = d1;
BW = ds;
iout(:, :, 1) = iout;                           % Initialize red color plane
iout(:, :, 2) = iout(:, :, 1);                  % Initialize green color plane
iout(:, :, 3) = iout(:, :, 1);                  % Initialize blue color plane
iout(:, :, 2) = min(iout(:, :, 2) + BW, 1.0);   % Add edges to green color plane
iout(:, :, 3) = min(iout(:, :, 3) + BW, 1.0);   % Add edges to blue color plane
subplot(2, 2, 4); imshow(iout); title('iout');  % Plot the resulting image

这是上面代码创建的图:

输入图像描述">

图像iout的创建与边缘检测算法无关。这只是显示前面步骤中找到的边缘的简单方法。二维灰度强度图像无法显示颜色,因此如果您想向图像添加彩色轮廓线,您必须首先将其转换为可以显示颜色的格式:或者 索引图像 (根据我的经验,这有点难处理)或三维 RGB 图像(第三维表示每个像素的红色、绿色和蓝色分量)。

在三维空间中将灰度图像复制 3 次,我们得到一个 3D RGB 图像,该图像最初仍包含灰色(每个像素等量的红色、绿色和蓝色)。然而,通过修改每个颜色平面的某些像素,我们可以向图像添加颜色。通过将逻辑边缘掩码 BW(边缘为其他位置且其他位置为零的边缘掩码)添加到绿色和蓝色平面,找到轮廓的那些像素将显示为青色。对函数 min 的调用确保添加图像的结果永远不会导致像素颜色值超过值 1.0,这是双精度 3-D RGB 图像元素应具有的最大值。

还应该注意的是,创建 3D RGB 图像的代码可以简化为以下内容:

iout = d1;
iout(:, :, 2) = min(d1+ds, 1.0);
iout(:, :, 3) = min(d1+ds, 1.0);

Assuming that the variable d1 stores what is likely a double precision representation (values between 0 and 1) of the original grayscale intensity image that is operated on, then the last 5 lines will turn that grayscale image into a 3-D RGB image iout that looks the same as the original grayscale image except that the contours will be overlaid on the image in cyan.

Here's an example, using the image 'cameraman.tif' that is included with the MATLAB Image Processing Toolbox:

d1 = double(imread('cameraman.tif'))./255;  % Load the image, scale from 0 to 1
subplot(2, 2, 1); imshow(d1); title('d1');  % Plot the original image
d = edge(d1, 'canny', .6);                  % Perform Canny edge detection
subplot(2, 2, 2); imshow(d); title('d');    % Plot the edges
ds = bwareaopen(d, 40);                     % Remove small edge objects
subplot(2, 2, 3); imshow(ds); title('ds');  % Plot the remaining edges
iout = d1;
BW = ds;
iout(:, :, 1) = iout;                           % Initialize red color plane
iout(:, :, 2) = iout(:, :, 1);                  % Initialize green color plane
iout(:, :, 3) = iout(:, :, 1);                  % Initialize blue color plane
iout(:, :, 2) = min(iout(:, :, 2) + BW, 1.0);   % Add edges to green color plane
iout(:, :, 3) = min(iout(:, :, 3) + BW, 1.0);   % Add edges to blue color plane
subplot(2, 2, 4); imshow(iout); title('iout');  % Plot the resulting image

And here is the figure the above code creates:

enter image description here

How it works...

The creation of the image iout has nothing to do with the edge detection algorithm. It's simply an easy way to display the edges found in the previous steps. A 2-D grayscale intensity image can't display color, so if you want to add colored contour lines to the image you have to first convert it to a format that will let you show color: either an indexed image (which is a little harder to deal with, in my experience) or a 3-D RGB image (the third dimension represents the red, green, and blue color components of each pixel).

Replicating the grayscale image 3 times in the third dimension gives us a 3-D RGB image that initially still contains gray colors (equal amounts of red, green, and blue per pixel). However, by modifying certain pixels of each color plane we can add color to the image. By adding the logical edge mask BW (ones where edges are and zeroes elsewhere) to the green and blue color planes, those pixels where the contours were found will appear cyan. The call to the function min ensures that the result of adding the images never causes a pixel color value to exceed the value 1.0, which is the maximum value an element should have for a double-precision 3-D RGB image.

It should also be noted that the code for creating the 3-D RGB image can be simplified to the following:

iout = d1;
iout(:, :, 2) = min(d1+ds, 1.0);
iout(:, :, 3) = min(d1+ds, 1.0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文