I = imread('eight.tif');
imshow(I)
BW = roipoly(I);
BW1 = not(BW);
J = roifill(I,BW1); imshow(J)
BWs = edge(J, 'sobel', (graythresh(I) * .1));
figure, imshow(BWs), title('binary gradient mask');
se90 = strel('line', 3, 90);
se0 = strel('line', 3, 0);
BWsdil = imdilate(BWs, [se90 se0]);
figure, imshow(BWsdil), title('dilated gradient mask');
BWdfill = imfill(BWsdil, 'holes');
figure, imshow(BWdfill);
title('binary image with filled holes');
BWnobord = imclearborder(BWdfill, 4);
figure, imshow(BWnobord), title('cleared border image');
seD = strel('diamond',1);
BWfinal = imerode(BWnobord,seD);
BWfinal = imerode(BWfinal,seD);
figure, imshow(BWfinal), title('segmented image');
BWoutline = bwperim(BWfinal);
Segout = I;
Segout(BWoutline) = 255;
figure, imshow(Segout), title('outlined original image');
[i,j] = find(BWfinal);
i1 = unique(i);
j1 = unique(j);
diameter = max(i)-min(i)
center_i = floor(mean(i1))
center_j = floor(mean(j1))
Additionally, another demonstration you can look into is the following MathWorks FileExchange submission:
http://www.mathworks.com/matlabcentral/fileexchange/25157-image-segmentation-tutorial
Best Answer