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

4 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
DGM
DGM on 2 Mar 2022
Note that the blob appears to be 12.xx cm in radius. The diameter is somewhere around 25 cm or so. There's a bit of a perspective distortion, but it's small.
a = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/911220/751.jpg');
c = rgb2gray(a);
b = imbinarize(c);
b = imfill(bwareafilt(~b,1),'holes'); % clean up the image
b = imopen(b,strel('disk',10,0)); % get rid of graticule lines
imshow(b);
% these are the (manually) measured sizes of the graticule
% 963px/40cm x
% 986px/40cm y
% in a very crude attempt to compensate for perspective
% i'm just going to tweak the image size
b = imresize(b,[size(b,1) size(b,2)*986/963]);
% now both axes have the same scale of 986px/40cm
objscale = 986/40; % px/cm
% when you have an irregular shape, it depends what you want when you say "diameter"
S = regionprops(b,'minferetproperties','maxferetproperties','equivdiameter');
% let's say we pick one
dist = S.EquivDiameter;
%dist = (S.MaxFeretDiameter + S.MinFeretDiameter)/2;
% i can't demo the menu in-browser, so i'm just bypassing it
%u = menu('Choose measuring unit','Pixels','Centim','Millimeters');
u = 2;
if (u==1)
fprintf('The length of the object is: %0.2f Pixels\n',dist);
elseif (u==2)
dist_cm = dist/objscale;
fprintf('The length of the object is: %0.2f Centim\n',dist_cm);
else
dist_mm = dist/objscale*10;
fprintf('The length of the object is: %0.2f Millimeters\n',dist_mm);
end
The length of the object is: 26.26 Centim
% original estimate (in cm)
origest = mean([12.4 12.25])*2
origest = 24.6500
It's off by a bit, but considering the eccentricity of the blob, I think it's plausible that the original estimate of 24.6cm is an underestimate.

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
DGM
DGM on 8 Dec 2022
Edited: DGM on 8 Dec 2022
As mentioned, the scaling factor was an uninformed assumption that the glass was a standard 25mm microscope slide. The glass is approximately 1395px wide. So we have 25mm/1395px.
As to whether that's actually a 25mm slide, only OP can verify.

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!