如果我使用这个公式有像素距离:
sqrt((x1-x2).^2+.(y1-y2)^2))
如何将其转换为 CM?
调用“ScreenPixelsPerInch”属性:
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。如果您知道显示器的尺寸、纵横比和分辨率,这很容易做到。例如,我的设置:
1920x1080p 宽屏 16:9 23" 显示器
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
也许我很幸运,但这种方法对我有用。
您的应用程序中的“像素”是什么?
如果您的意思是屏幕像素,请查看其他答案。如果是指图像像素,则需要知道图像的物理尺寸(高度、宽度),然后才能计算出一个像素的宽度。
例如,如果您的图像为 10x10 厘米和 512x512 像素,则一个像素为 10/512 厘米宽。以像素为单位的距离乘以 10/512,得到以厘米为单位的距离。
如果您不知道渲染此类像素的设备的 DPI(或通常的分辨率),则无法将像素转换为厘米。
假设您有设备的 DPI,那么您可以简单地执行像素 / DPI * 2.54。
<思考>
px cm in cm cm
px : ---- * ---- = px * ---- * ---- = in * ---- = cm
in in px in in
...好的,单位是正确的:) </thinking>
像素和CM是不同的度量。像素取决于环境。电脑屏幕有不同的分辨率和大小。比如我的屏幕100px大约是2.7cm是什么情况?