2016-06-10 149 views
3

在MatLab中,我有一個二進制圖像,我正在填充一個洞。問題在於該地區大部分(但不是全部)關閉。是否有任何現有的視覺處理功能可以做到這一點?我是否必須編寫自己的算法?填寫未完全關閉的二進制圖像的區域

原件/所需

enter image description here enter image description here

另一個單獨的問題是,我無法檢測在二進制圖像薄尾狀結構。我需要刪除這些類型的結構,而無需移除它所連接的較大的物體。是否有任何現有的視覺處理功能可以做到這一點?我是否必須編寫自己的算法?

原件/所需

Original Image Desired Image

+0

任何這樣的算法將砍掉腿爲好。 – karakfa

+0

那會好起來的。我只需要主體區域 – user3315340

回答

4

在第一個例子中,可以使用通過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'); 

enter image description here 同樣的,你的第二組圖像,你可以使用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)); 

enter image description here

更新

如果你想要的「封閉」的中心孔的重心上面的圖片,你可以得到一個面具這只是這個開口由減去closedfilled

% 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)]; 

enter image description here

+0

它的工作原理!非常感謝。我一直在努力解決這個問題3個小時了! – user3315340

+0

@ user3315340如果它適合您,請考慮將其標記爲幫助他人解決類似問題的解決方案。 – Suever

+0

另一個快速問題。是否可以找到「封閉圖像」的黑色封閉圓的質心座標? – user3315340