且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Matlab:查找图片中对象的外边缘

更新时间:2023-02-26 15:32:25

要处理与图像边缘相交的轮廓,可以使用 bwperim来代替edge ,但这只是一个变种.

To deal with the contours that intersect with the edge of the image, you can use use bwconncomp to separate the background from the unfillable contours. Then, instead of edge, you can get the outer perimeters only via bwperim, but that is just a variation.

I = imread('asEW3.jpg');
t1=graythresh(I);
k1=im2bw(I,t1);
k1=~k1;
se = strel('disk',1);
k0=imfill(~k1,'holes');            % new
cc = bwconncomp(k0);               % new
k0(cc.PixelIdxList{1})=0;          % new
k1 = imfill(k1,'holes');
cellMask = k1 | k0;                % new
cellContours = bwperim(cellMask);  % new
cellContours2 = edge(cellMask,'canny',[],sqrt(2)); % new
k1=~k1;
bw = edge(k1,'canny',[],sqrt(2));
figure,imshow(bw); title('original')
figure,imshow(cellContours); title('new, bwperim()')
figure,imshow(cellContours2); title('new, edge()')

使用连接的组件似乎有点过大,但是似乎没有一种更容易的方法来区分背景和命中图像边缘的单元格的中心,至少在imfill无法填充时不会如此这些轮廓.

Using connected components seems a bit like overkill, but there doesn't seem to be an easier way to distinguish background from the centers of cells that hit the edge of the image, at least not while imfill is unable to fill those contours.