MATLAB 中的轮廓检测

5

我正在尝试理解这段代码:

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是图像,应用了Canny边缘检测算法并且忽略了40个像素。这张图片是灰度的,并且轮廓被添加到了图片中。
请问下面的代码是什么原理/算法?我对代码中的轮廓检测部分感到困惑。
1个回答

10
假设变量d1存储的是双精度表示(介于0和1之间的值)的灰度强度图像,最后5行将把该灰度图像转换为一个3D RGB图像 iout,它看起来与原始灰度图像相同,只是轮廓以青色叠加在图像上。
以下是一个示例,使用MATLAB 图像处理工具箱中包含的图像'cameraman.tif'
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

这是上面代码创建的图形:

enter image description here

它是如何工作的...

图像iout的创建与边缘检测算法无关。它只是一种简单的方法,用于显示之前步骤中找到的边缘。2-D灰度强度图像无法显示颜色,因此,如果要向图像添加彩色轮廓线,必须首先将其转换为可以显示颜色的格式:索引图像(在我的经验中稍微难以处理)或3-D RGB图像(第三个维度表示每个像素的红色、绿色和蓝色颜色分量)。

在第三个维度中将灰度图像复制3次,我们得到一个3-D RGB图像,最初仍然包含灰色颜色(每个像素的红、绿、蓝颜色成分相等)。但是,通过修改每个颜色平面的某些像素,我们可以为图像添加颜色。通过将逻辑边缘掩码BW(边缘出现的地方为1,其他地方为0)添加到绿色和蓝色颜色平面上,那些发现轮廓的像素将呈现青色。函数min的调用确保添加图像的结果永远不会导致像素颜色值超过值1.0,这是双精度3-D RGB图像的最大值。

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

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

非常感谢。这里涉及到什么原则?为什么要转换为RGB?这是一种更好的边缘检测方法还是类似的东西? - user1234
取最小值有什么用?它的目的是什么? - user1234
1
@user689593:我在我的回答中添加了更多细节来回应你的问题。 - gnovice
@gnovice 我的问题略有不同。你看,我们在图像上绘制了由Canny边缘检测器检测到的“edges”,这些边缘属于该图像。因此,如果说“image”的大小为“300x300”,包含检测到的边缘的变量“d”也是一个逻辑数组,大小为“[300x300]”。现在我正在查找相同的“[300x300]”图像的轮廓。现在,结果数组的大小为“[2x7477]”。我该如何将这些轮廓绘制回从中提取它们的“image”上?(您可以在此处查看我所说的“[300x300]”图像)(http://imgur.com/C54VGIl) - Nancy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接