How to calculate distance in centimeters using IMDISTLINE?

i want to find out height of a sleeping infant. for that i want to find out the distance in cm. using 'imdistline' i'm getting the distance in pixels but how to convert that in cm?

 Accepted Answer

Put a ruler, a tape measure, a standard sheet of paper, or anything else of known length into the scene. Then snap a picture of it and use imdistline() to find the length in pixels. For example, let's say you laid down a sheet of A4 paper and measured it's distance as 2100 pixels. Knowing that it is 21 cm long, you get a spatial calibration factor of 21/2100, which you multiply your distances in pixels by to get the distance in cm. Multiply by that calibration factor squared to convert the area in pixels to the area in square centimeters.
calibrationFactor = 21/2100;
distanceInCm = distanceInPixels * calibrationFactor;
areaInSquareCm = areaInPixels * calibrationFactor ^ 2;
It's in the FAQ.
See my demo attached below for a really nice demo.

5 Comments

I would like to use imdistline to measure distances in medical images where the aspect ratio is non-uniform (for example the x dimension is 0.2 mm but the y dimension is 0.3 mm). Is this possible.... or should I just create a line object & re-invent the wheel/imdistline?
Thanks!
Dan
Yes. You can do it. Just convert x in pixels to "real" x unit with its calibration factor. Then do the same for y with its calibration factor. Then use the Pythagorean theorem with the real x and real y.
hi.. how can i find distance in pixels?
@Kanwarpal evidently you have not tried my demo attached above, because that will do it.

Sign in to comment.

More Answers (2)

global h; h = imdistline; % activates the fx for drawing and measuring line
setLabelTextFormatter(h,'measure'); % message
addNewPositionCallback(h,@ShowDistance); % new position callback
%% Custom fx
function ShowDistance(pos)
global h xc yc; % sets line handles and x and y conversion factors to global
X = pos(:,1)*xc; Y = pos(:,2)*yc; % converts x and y positions from px to target unit
d = norm(diff([X Y])); % distance in target unit
setLabelTextFormatter(h,[num2str(d,'%.1f') ' target unit']); % updates measurement
end

1 Comment

I like how you put in so many comments. I wish everyone would do that.

Sign in to comment.

Multiply by the length (in cm) of one pixel.

9 Comments

how can i know the length of a pixel in cm?
You must measure it somehow. E.g., if there is an object in the background of the image of known size, you might calibrate based on that. Although, you would probably have to account for depth differences between the object and the infant.
karishma, did you not see the FAQ entry explanation, with code, that I gave? Does that not explain it clearly enough?
Run the attached demo if you want to see it in action.
is imdistline only used to measure a line? what if i wanna measure a curve from an image? such as an arc??
It's only used for a line. You can use imline() or roipolyold() to get coordinates of a polygon. Or you can use imfreehand(). I've attached a demo of imfreehand(), though not for drawing a curve and measuring the length of it.
thaks for the respons. i'll try it. i have another questions,, what about imellips? can half of the ellips is used to measuring the curve line? thank you
I don't see how. POST YOUR IMAGE IN A NEW QUESTION , not here , and perhaps I'd change my mind or give you other suggestions.
this is my image https://www.mathworks.com/matlabcentral/answers/177673-how-to-measuring-curve-line-of-an-image
thank you

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!