Hole Filling in Binary Image

by

 

This code implement hole filling of an object in matlab.

holeFilling.m
a=[ 0 0 0 0 0 0 0
    0 0 1 1 0 0 0
    0 1 0 0 1 0 0
    0 1 0 0 1 0 0
    0 0 1 0 1 0 0
    0 0 1 0 1 0 0
    0 1 0 0 0 1 0
    0 1 0 0 0 1 0
    0 1 1 1 1 0 0
    0 0 0 0 0 0 0];
[r c]=size(a);
acmp=bitcmp(a,1);
xo=[0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 1 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0];
b=[0 1 0
   1 1 1
   0 1 0];
 x2=imdilate(xo,b);
    for i=1:r
        for j=1:c
            if(x2(i,j)==acmp(i,j))
            x2(i,j)=acmp(i,j);
            else
                x2(i,j)=0;
            end
        
        end
    end
while(~isequal(xo,x2))
   xo=x2;
   x2=imdilate(xo,b);
    for i=1:r
        for j=1:c
            if(x2(i,j)==acmp(i,j))
            x2(i,j)=acmp(i,j);
            else
                x2(i,j)=0;
            end
        
        end
    end
end
x2=x2+a;
subplot(2,1,1)
imshow(a);
subplot(2,1,2)
imshow(x2);

Contact us