Fill area between two connected components in MATLAB

前端 未结 3 1965
我寻月下人不归
我寻月下人不归 2021-02-06 09:07

I have a binary image that represents a number in MATLAB:

\"image

I\'d like to fill all the d

3条回答
  •  情深已故
    2021-02-06 09:51

    Another possibility is to use the BWBOUNDARIES function, which:

    traces the exterior boundaries of objects, as well as boundaries of holes inside these objects

    That information is contained in the fourth output A, an adjacency matrix that represents the parent-child-hole dependencies.

    %# read binary image
    bw = imread('SUvif.png');
    
    %# find all boundaries
    [B,L,N,A] = bwboundaries(bw, 8, 'holes');
    
    %# exclude inner holes
    [r,~] = find(A(:,N+1:end));        %# find inner boundaries that enclose stuff
    [rr,~] = find(A(:,r));                      %# stuff they enclose
    idx = setdiff(1:numel(B), [r(:);rr(:)]);    %# exclude both
    bw2 = ismember(L,idx);                      %# filled image
    
    %# compare results
    subplot(311), imshow(bw), title('original')
    subplot(312), imshow( imfill(bw,'holes') ), title('imfill')
    subplot(313), imshow(bw2), title('bwboundaries')
    

    enter image description here

提交回复
热议问题