MATLAB中的轮廓检测 [英] Contour detection in MATLAB

查看:779
本文介绍了MATLAB中的轮廓检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解这段代码:

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

ds = bwareaopen(d,40);
数字,
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 是图像和应用canny检测器,忽略40个像素。图像为灰度,轮廓添加到图像中。



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

解决方案

假设变量 d1 存储原始(根据我的经验,这有点难以处理)或3-D RGB图像(第三维表示每个像素的红色,绿色和蓝色分量)。



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



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

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


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.

解决方案

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:

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);

这篇关于MATLAB中的轮廓检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆