If I have distance in pixel using this formula:
sqrt((x1-x2).^2+.(y1-y2)^2))
How can I converted it to CM?
If I have distance in pixel using this formula:
sqrt((x1-x2).^2+.(y1-y2)^2))
How can I converted it to CM?
You can't convert pixels to cm if you don't know the DPI (or, in general, the resolution) of the device on which such pixels are rendered.
Supposing you have the DPIs of your device, then you can simply do pixel / DPI * 2.54.
<thinking>
px cm in cm cm
px : ---- * ---- = px * ---- * ---- = in * ---- = cm
in in px in in
...ok, the units are right :) </thinking>
pixel and CM are different measures. A pixel is environment dependent. Computer screens have different resolutions and sizes. For example on my screen 100px is approximatively 2.7cm What is the situation?
Call the 'ScreenPixelsPerInch' property:
dpix=sqrt((x1-x2)^2+(y1-y2)^2); %# calculate distance in pixels
pixperinch=get(0,'ScreenPixelsPerInch'); %# find resolution of your display
dinch=dpix/pixperinch; %# convert to inches from pixels
dcm=dinch*2.54; %# convert to cm from inches
Or for the one-liner approach:
dcm=sqrt((x1-x2)^2+(y1-y2)^2))/get(0,'ScreenPixelsPerInch')*2.54;
EDIT: You may want to check if your system gets the screen size and dpi correct. If you know the size and aspect ratio and resolution of your monitor, this is easily done. For example, my setup:
Widescreen 16:9 23" Monitor at 1920x1080p
h^2+w^2=23^2 and h/w=9/16
gives
h=11.28 and w=20.05 inches
get(0,'Screensize')
returns [1 1 1920 1080]
and get(0,'ScreenPixelsPerInch')
returns 96
So 1920/20.05 = 95.76
and 1080/11.28 = 95.74
Maybe I got lucky, but this method worked for me.
What is a 'pixel' in your application?
If you mean screen pixels, look at the other answers. If you mean image pixels, you need to know the physical size of the image (height, width), and then you can calculate how wide a pixel is.
For example, if your image is 10x10 cm, and 512x512 pixels, one pixel is 10/512 cm wide. Multiply 10/512 by the distance in pixels, and you get the distance in centimeters.