如果我使用以下公式得到像素距离:
sqrt((x1-x2).^2+.(y1-y2)^2))
我怎样将其转换为厘米?
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
或者使用一行代码的方式:
dcm=sqrt((x1-x2)^2+(y1-y2)^2))/get(0,'ScreenPixelsPerInch')*2.54;
编辑:您可能希望检查一下系统是否正确获取了屏幕大小和dpi。如果您知道显示器的尺寸、长宽比和分辨率,这很容易实现。例如,我的设置:
宽屏16:9 23英寸显示器,分辨率为1920x1080p。
h^2+w^2=23^2 and h/w=9/16
提供
h=11.28 and w=20.05 inches
get(0,'Screensize')
返回 [1 1 1920 1080]
get(0,'ScreenPixelsPerInch')
返回 96
因此,1920/20.05 = 95.76
,1080/11.28 = 95.74
也许我很幸运,但这种方法对我有效。
如果您不知道呈现这些像素的设备的DPI(或通常的分辨率),则无法将像素转换为厘米。
假设您已经知道了您的设备的DPI,则可以简单地进行像素数除以DPI,然后乘以2.54。
<思考中>
px cm in cm cm
px : ---- * ---- = px * ---- * ---- = in * ---- = cm
in in px in in
好的,单位是正确的 :) </思考>
像素和厘米是不同的度量单位。像素是与环境相关的。计算机屏幕具有不同的分辨率和尺寸。例如,在我的屏幕上,100像素大约是2.7厘米。情况如何?