matlabimage-processingimage-segmentationimage-morphology

Fill gaps in binary leaf image occured from segmentation preserving leaf teeth shape


after leaf segmentation i got the following binary image: enter image description here Is there a way to fill the gaps caused by the similiarity of the veins with the background? I've tried to use imclose, or imdilate etc but it affects teeth shape. I can't find out how to fill these gaps without affecting teeth shape.


Solution

  • You may try bwfill(I, 'hols'), with out without imclose:

    I = imbinarize(rgb2gray(imread('leaf.jpg')));
    I = I(3:end-4, 1:end-8); %Remove white frame
    J = imclose(I, ones(2)); %Minor affect the teeth shape (result looks better with imclose).
    K = bwfill(J, 'hols'); %Fill the black hols
    

    Result:
    enter image description here


    In case you want to fill the "vein gaps", you can try the following approach:

    I = imbinarize(rgb2gray(imread('leaf.jpg')));
    I = I(3:end-4, 1:end-8); %Remove white frame
    I = bwfill(I, 'hols'); %Fill small black hols.
    J = imerode(imdilate(I, strel('disk',5)), strel('disk',10)); %Dilate with radius 5 and erode with 10
    T = (I == 0) & (J == 1); %Create mask with 1 where I is black and J is white "vein mask".
    K = I;
    K(T) = 1; %Fill "vein mask" in I with white.
    K = bwfill(K, 'hols'); %Fill small black hols (fill tiny holds left).
    

    Result:
    enter image description here