
im = imread('test.jpg'); 
im = rgb2gray(im);
figure, imshow(im);

SE = strel('dis', 7);

%% binary morphology

I = im2bw(im,graythresh(im));      
figure, imshow(I);

J = imerode(I, SE);
BW1 = I - J;    % internal edges  

K = imdilate(I, SE);
BW2 = K - I;    % external edges

BW = K - J;     % basic gradient

figure, imshow(J);
figure, imshow(K);

figure, imshow(BW1);
figure, imshow(BW2);
figure, imshow(BW);


%% gray-scale morphology

X = imerode(im, SE);
Y = imdilate(im, SE);

internal = im - X;
external = Y - im;
basic = Y - X;

figure, imshow(X);
figure, imshow(Y);

figure, imshow(internal);
figure, imshow(external);
figure, imshow(basic);







 
