使用Matlab计算二进制图像的轮廓 [英] Compute contour of a binary image using matlab
问题描述
我正在尝试计算二进制图像的轮廓.目前,我通过循环识别图像中的第一个非零像素和最后一个非零像素.有没有更好的办法?我遇到了几个功能:
I am trying to compute contour of a binary image. Currently i identify the first non zero and the last non zero pixel in the image through looping. Is there a better way? i have encountered few functions:
imcontour(I)
bwtraceboundary(bw,P,fstep,conn,n,dir)
但是第一个不返回轮廓的x和y坐标.第二个功能需要我无法提供的种子点.图像示例如下所示.谢谢.
But the first doesn't return the x and y coordinates of the contour. The second function requires a seed point which i cannot provide. An example of the image is shown below. Thanks.
推荐答案
@rayryeng已经提供了正确的答案.作为另一种方法(可能是bwperim
在内部执行此操作),可以通过计算膨胀图像和侵蚀图像之间的差异来获得binary
图像的边界.
@rayryeng have already provided the correct answer. As another approach (might be that bwperim
performs this operations internally) boundaries of a binary
image can be obtained by calculating the difference between the dilated and the eroded image.
对于给定的图像:
im = im2bw(imread('http://i.stack.imgur.com/yAZ5L.png'));
和给定的二进制结构元素:
and a given binary structural element:
selem = ones(3,3); %// square, 8-Negihbours
% selem = [0 1 0; 1 0 1; 0 1 0]; %// cross, 4-Neighbours
对象的轮廓可以提取为:
The contour of the object can be extracted as:
out = imerode(im, selem) ~= imdilate(im, selem);
但是,这里的边界要比使用bwperim
的边界厚 ,因为像素在对象的内部和外部都被遮蔽了.
Here, however, the boundary is thicker than using bwperim
, as the pixels are masked in both inside and outside of the object.
这篇关于使用Matlab计算二进制图像的轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!