Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert pixel to cm

Tags:

pixel

matlab

If I have distance in pixel using this formula:

sqrt((x1-x2).^2+.(y1-y2)^2))

How can I converted it to CM?

like image 414
Jessy Avatar asked Dec 09 '22 15:12

Jessy


1 Answers

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.

like image 136
Doresoom Avatar answered Dec 22 '22 11:12

Doresoom