Fill area between two connected components in MATLAB

前端 未结 3 2001
我寻月下人不归
我寻月下人不归 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:47

    The problem is how to distinguish the holes from the digits. A possible ad hoc solution is filtering them by the area of the pixels inside.

    function SolveSoProblem()
    
        I = imread('http://i.stack.imgur.com/SUvif.png');
    
        %Fill all the holes 
        F = imfill(I,'holes');
    
        %Find all the small ones,and mark their edges in the image
        bw = bwlabel(I);
        rp = regionprops(bw,'FilledArea','PixelIdxList');
        indexesOfHoles = [rp.FilledArea]<150;   
        pixelsNotToFill = vertcat(rp(indexesOfHoles).PixelIdxList); 
        F(pixelsNotToFill) = 0;
        figure;imshow(F);
    
        %Remove the inner area
        bw1 = bwlabel(F,4);
        rp = regionprops(bw1,'FilledArea','PixelIdxList');
        indexesOfHoles1 = [rp.FilledArea]<150;
        pixelListToRemove = vertcat(rp(indexesOfHoles1).PixelIdxList);
        F(pixelListToRemove) = 0;
    
        figure;imshow(F);
    end
    

    After step(1):

    enter image description here

    After step(2):

    enter image description here

提交回复
热议问题