How can I visually isolate and measure a round object within my MATLAB image?

6 views (last 30 days)
How can I visually isolate and measure a round object within my MATLAB image?
Do you have an example that shows how to do the following in MATLAB:
1. Select a round object in the image
2. Fill the rest of image with a background color
3. Draw an outline around the object
4. Find the diameter and center of the object

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 4 May 2023
Edited: MathWorks Support Team on 4 May 2023
Here is an example on how to visually isolate and measure a round object within an image in MATLAB:
I = imread('eight.tif');
imshow(I)
BW = roipoly(I);
% manually select region here
BW1 = not(BW);
J = roifill(I,BW1); imshow(J)
% using ipexsegcell demo
% type 'ipexsegcell' from the MATLAB command prompt
% to view documentation for this demo
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');
% find diameter and center of the selected coin
[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:
https://www.mathworks.com/matlabcentral/fileexchange/25157-image-segmentation-tutorial

More Answers (1)

Image Analyst
Image Analyst on 20 Nov 2016
Edited: MathWorks Support Team on 27 Apr 2023
I do not recommend the accepted answer. See my Image Processing Tutorial for a much better and simpler method. https://www.mathworks.com/matlabcentral/fileexchange/25157-image-segmentation-tutorial You can simply threshold and call regionprops. There is no need for all that unnecessary edge detection and morphology stuff.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!