Image Morphing in Matlab

I want to use the math morphology function in MATLAB to find the can.png border of an image. Input image:

enter image description here

I want to get a border like:

enter image description here

I tried different combinations and parameters using strel, imerode, imdilate but the result is not good enough (far from expectation)

One of my trial codes:

a = imread ('can.png');
b = im2bw(a);

SE = strel('rectangle', [10 50 ]) ;
i2 = imdilate(b,SE);

figure(1); imshow(i2);

p = ones(4);
c = b - imerode(b,p);

figure(2); imshow(c);

      

Output:

enter image description here

Can anyone help me how to create the expected image (black background with thin border for the jar, please?) Thanks a lot.

+3


source to share


2 answers


Bunarize with its morphological gradient, then dilate with elementary SE, fill in the holes and finally get its border (trivial given the current image). It doesn't require any magic arbitrary threshold.



enter image description hereenter image description here

+5


source


im = imread('can.png');

% turn image to BW
imb = (im < 220);

% fill the holes in the image
imf = imfill(imb, 'holes');

% find the edge
ed = edge(imf);

      

Resulting image:



enter image description here

+4


source







All Articles