I want to measure the diameter of the circle using image.

15 views (last 30 days)
I want to measure the diameter of the circle. Please let me know the respective coding for the below attached image.
I used the below code for finding the diameter:
a = imread('I:\testing\abrupt\60.jpg');
c = rgb2gray(a);
b = imbinarize(c);
imshow(b);
%measuring
h = imdistline(gca);
api =iptgetapi(h);
%
pause();
%
dist =api.getDistance();
u = menu('Choose measuring unit','Pixels','Centim','Millimeters');
if (u==1)
fprintf('The length of the object is: %0.2f Pixels\n',dist);
elseif (u==2)
dist_cm = dist*0.2645;
fprintf('The length of the object is: %0.2f Centim\n',dist_cm);
else
dist_m = (dist)*0.2645*10;
fprintf('The length of the object is: %0.2f Millimeters\n',dist_mm);
end
  5 Comments

Sign in to comment.

Accepted Answer

DGM
DGM on 1 Mar 2022
Edited: DGM on 1 Mar 2022
This should be a start
a = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/910160/21.jpg');
c = rgb2gray(a);
b = imbinarize(c);
b = imfill(bwareafilt(~b,1),'holes'); % clean up the image
imshow(b);
% when you have an irregular shape, it depends what you want when you say "diameter"
S = regionprops(b,'minferetproperties','maxferetproperties','equivdiameter');
% show what the options look like
S.EquivDiameter % the equivalent diameter of a circle with the same area
ans = 961.0452
S.MinFeretDiameter % the minimum diameter
ans = 948
S.MaxFeretDiameter % the maximum diameter
ans = 987.3196
% let's say we pick one
dist = S.EquivDiameter;
% i can't demo the menu in-browser, so i'm just bypassing it
%u = menu('Choose measuring unit','Pixels','Centim','Millimeters');
u = 3;
if (u==1)
fprintf('The length of the object is: %0.2f Pixels\n',dist);
elseif (u==2)
dist_cm = dist*2.5/1395;
fprintf('The length of the object is: %0.2f Centim\n',dist_cm);
else
dist_mm = dist*25/1395;
fprintf('The length of the object is: %0.2f Millimeters\n',dist_mm);
end
The length of the object is: 17.22 Millimeters
Note that I changed the scaling factor. Since I doubt the blob is 2.5m in diameter, I just approximated the above scaling factor on the assumption that the glass is a standard 25mm microscope slide. The accuracy of that approximation can stand to be improved.
Once you decide which diameter metric you want, the other cases can be omitted from the call to regionprops().
  4 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!