# How to calculate distance in centimeters using IMDISTLINE?

11 次查看（过去 30 天）
karishma2013-10-4

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?

### 采纳的回答

Image Analyst 2013-10-4

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.
##### 3 个评论显示隐藏 2更早的评论
Image Analyst 2014-11-20
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.

### 更多回答（2 个）

Roche de Guzman 2021-1-30
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 个评论显示隐藏 无
Image Analyst 2021-1-30
I like how you put in so many comments. I wish everyone would do that.

Matt J 2013-10-4
Multiply by the length (in cm) of one pixel.
##### 9 个评论显示隐藏 8更早的评论
leylin fatqiyah 2015-2-12
this is my image https://www.mathworks.com/matlabcentral/answers/177673-how-to-measuring-curve-line-of-an-image
thank you

### Community Treasure Hunt

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

Start Hunting!

Translated by