在第一个示例中,您可以使用imclose 执行膨胀,然后执行腐蚀以闭合这些边缘。然后你可以跟进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));
更新
如果你想要上面“封闭”图像的中心开口的质心,你可以通过从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)];