im = imread('grains.jpg'); im2 = rgb2gray(im); level = graythresh(im2); BW = im2bw(im2,level); imshow(BW) se = strel('disk',7); %% morphological operations a_erode = imerode(BW,se); figure , imshow(a_erode) , title('eroded image') a_dilate = imdilate(BW, se); figure , imshow(a_dilate) , title('dilated image') a_open = imopen(BW, se); figure , imshow(a_open) , title('opened image') a_closed = imclose(BW,se); figure , imshow(a_closed) , title('closed image') I = imread('coins.png'); imshow(I) level = graythresh(I); BW = im2bw(I, level); imshow(BW) BW2 = imfill(BW, 'holes'); imshow(BW2)