How do I measure distances in an Image

22 views (last 30 days)
I want to measure the maximum distance in my ROI image along the x and y axis by writting code.
This is my ROI image
I want to know the maximum distance along the x and y axis for example
Thank you

Answers (2)

Image Analyst
Image Analyst on 4 Feb 2023
Use bwferet. Let me know if you can't figure it out on your own.
help bwferet
BWFERET Measure Feret diameters and angles of image regions. OUT = BWFERET(I) measures the maximum Feret Properties of each component (object) in the image. I can be a binary image, connected component or labeled matrix. OUT = BWFERET(BW, PROPERTIES) measures a set of Feret properties of each connected component (object) in the binary image BW, which is a logical array. OUT = BWFERET(CC, PROPERTIES) measures a set of Feret properties of each connected component (object) in CC, which is a struct returned by BWCONNCOMP. OUT = BWFERET(L, PROPERTIES) measures a set of Feret properties of each labeled component (object) in the label matrix L. Positive integer elements of L correspond to different regions. For example, the set of elements of L equal to 1 corresponds to region 1; the set of elements of L equal to 2 corresponds to region 2; and so on. [OUT, L] = BWFERET(...) measures a set of Feret properties of each component (object) in the binary image, connected component or labeled matrix. This also returns the corresponding label matrix L such that the first value in the table OUT corresponds to the labeled region 1, the second value with the labeled region 2 and so on. PROPERTIES can be an array of strings, a single character vector, a cell array of character vectors, or 'all'. If PROPERTIES is set to 'all' it returns all the Feret properties mentioned below. If no argument is provided, all maximum Feret properties are given as outputs. The set of valid measurement strings or character vectors includes 'MaxFeretProperties', 'MinFeretProperties' and 'all'. 'MaxFeretProperties' - Outputs all the properties related to maximum Feret Diameter. These properties are: MaxDiameter - Maximum Feret diameter length. MaxAngle - Angle of maximum Feret diameter with respect to X axis in degrees. The value lies between 180 to -180 degrees. MaxCoordinates - Endpoint coordinates of maximum Feret diameter. 'MinFeretProperties' - Outputs all the properties related to minimum Feret Diameter. These properties are: MinDiameter - Minimum Feret diameter length. MinAngle - Angle of minimum Feret diameter with respect to X axis in degrees. The value lies between 180 to -180 degrees. MinCoordinates - Endpoint coordinates of minimum Feret diameter. Class Support ------------- If the first input is BW, BW must be a logical array and it should be 2D. If the first input is CC, CC must be a structure returned by BWCONNCOMP. If the first input is L, L must be real, nonsparse and 2D. L can have any numeric class. The output OUT is returned as a table. Example 1 --------- % Calculate the minimum Feret diameter for objects in image I = imread('toyobjects.png'); bw = imbinarize(I, 'adaptive'); % Retain the two biggest objects in the image bw = bwareafilt(bw, 2); bw = imfill(bw, 'holes'); % Calculate the Feret properties of the objects in the image along with % the label matrix [out, L] = bwferet(bw, 'MinFeretProperties'); Example 2 --------- % Plot the maximum Feret diameter for objects in the image % Read an image I = imread('toyobjects.png'); % Binarize the image B = imbinarize(I, 'adaptive'); % Fill in the holes in the binary image B = imfill(B, 'holes'); % Show the image h = imshow(B) ax = h.Parent; % Convert to connected component struct using bwconncomp C = bwconncomp(B); % Calculate Feret Properties F = bwferet(C, 'MaxFeretProperties'); hold on % Display maximum Feret Diameters with their values for each object imdistline(ax, F.MaxCoordinates{1}(:,1), F.MaxCoordinates{1}(:,2)); imdistline(ax, F.MaxCoordinates{2}(:,1), F.MaxCoordinates{2}(:,2)); imdistline(ax, F.MaxCoordinates{3}(:,1), F.MaxCoordinates{3}(:,2)); imdistline(ax, F.MaxCoordinates{4}(:,1), F.MaxCoordinates{4}(:,2)); See also BWCONNCOMP, BWLABEL, BWLABELN, LABELMATRIX, REGIONPROPS. Documentation for bwferet doc bwferet
Alternatively you could use bwboundaries followed by pdist2 and max
  1 Comment
Warisa Theerathamtada
Warisa Theerathamtada on 5 Feb 2023
Thank you sir, I've just found out that function. I will try and tell my result for you. Thank you

Sign in to comment.


DGM
DGM on 4 Feb 2023
Edited: DGM on 5 Feb 2023
You say you want the maximum distance along the x and y axis, but what you drew is something else. So which do you want?
You can use regionprops() to get major/minor diameters of an equivalent ellipse. Otherwise, depending on your interpretation, you could get the bounding box geometry or feret properties using regionprops().
% cropped from a screenshot
inpict = imread('image.png')>128;
% get properties
S = regionprops(inpict,'MajoraxisLength','MinoraxisLength','Orientation',...
'MinFeretProperties','MaxFeretProperties','BoundingBox');
% width and height of grid-aligned bounding box
S.BoundingBox(3:4)
ans = 1×2
47 70
% diameters of an equivalent ellipse and their angles
% these diameters are at 90deg to each other
[S.MinorAxisLength S.MajorAxisLength]
ans = 1×2
42.8568 74.9145
S.Orientation + [90 0]
ans = 1×2
164.8426 74.8426
% min/max Feret diameters and their angles
% these do not necessarily occur at 90deg to each other
[S.MinFeretDiameter S.MaxFeretDiameter]
ans = 1×2
42.2930 73.6817
180-[S.MinFeretAngle S.MaxFeretAngle]
ans = 1×2
167.7995 71.8110
The Feret properties can also be obtained using bwferet(), as @Image Analyst mentions.
  4 Comments
Warisa Theerathamtada
Warisa Theerathamtada on 5 Feb 2023
Thank you sir. Sorry for what I said was not clear. I need the maximum range of the object but I dont sure that my way (maximum range) it work .
In my intention for doing this I need to find the size of pill by the coin is the calibrating
I takes picture (coin and pill) and crop it to find roi and find the range (that I ask)
I know the perimeter, radius of coin ,so I think I can use this for calibrate the non-linear about this pic if I know the something of distance the roi.
if you have new way for doing this please explain for me Thank you.
Image Analyst
Image Analyst on 5 Feb 2023
OK, what a bad image! You can get the mm per pixel in the middle of the image because you can look at the width of the coin there. However your camera is not directly overhead for some reason. That means that the spatial calibration in the vertical direction changes depending on what line you're on. And, very unfortunately you have the pill being aligned vertically. So you can get the width of the pill, but the length of it will be only approximate. You can use the same mm per pixel if you (incorrectly) assume it's the same no matter what line you're on, but of course the vertical length of the pill will be only approximate. If you want a more accurate measurement you should have the camera directly overhead.

Sign in to comment.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!