填充二值图像中未完全闭合的区域

Fill in a region of a binary image which is not completely closed

在 MatLab 中,我有一个二进制图像,我正在尝试填充一个洞。问题是该区域大部分(但并非完全)关闭。是否有任何现有的视觉处理功能可以做到这一点?我必须自己写算法吗?

原始/期望

另一个单独的问题是我无法检测二值图像中的薄 tail-like 结构。我需要删除这些类型的结构而不删除它所附加的更大的 body。是否有任何现有的视觉处理功能可以做到这一点?我必须自己写算法吗?

原始/期望

在第一个例子中,您可以使用imclose to perform a dilation followed by an erosion to close those edges. Then you can follow up with imfill来完全填写。

img = imread('http://i.stack.imgur.com/Pt3nl.png');
img = img(:,:,1) > 0;

% You can play with the structured element (2nd input) size
closed = imclose(img, strel('disk', 13));
filled = imfill(closed, 'holes');

同样,对于第二组图像,您可以使用 imopen(先腐蚀后膨胀)去除尾部。

img = imread('http://i.stack.imgur.com/yj32n.png');
img = img(:,:,1);

% You can play with the structured element (2nd input) size
% Increase this number if you want to remove the legs and more of the tail
opened = imopen(img, strel('disk', 7));

更新

如果你想要上图"closed"中心开口的质心,你可以通过filled减去closed得到一个掩码就是这个开口。

% Find pixels that were in the filled region but not the closed region
hole = filled - closed;

% Then compute the centroid of this
[r,c] = find(hole);
centroid = [mean(r), mean(c)];