像素坐标转换实际坐标python_从像素坐标计算x&y坐标

博主正在使用Python尝试将一个400px*460px图像中的像素坐标转换为厘米单位,他们应用了透视变换但遇到了问题。当沿着y轴移动时,计算出的坐标出现误差。他们已经尝试使用OpenCV的findHomography函数,但结果并不准确。寻求解决方案。
摘要由CSDN通过智能技术生成

嘿嘿!

我有一个平面的图像(从上面拍摄,角度为30°),我想要一个特定像素的x&y坐标,但以厘米为单位(坐标系在曲面中)。

有没有办法存档?

我正在使用python,但如果有任何帮助,我将不胜感激!:)

编辑:

我尝试了同音字法从下面,但我没有安静设法使它工作。她就是我所做的:#two sets of points I used

#src: corners + center of my 400px*460px image

#dst: coordinate system being outside the image

src = np.matrix(((1, 1, 1),(400, 1, 1),(1, 460, 1),(400, 460, 1),(200,230,1)))

dst= np.matrix(((31.6, 7, 1),(14.5, 7, 1),(28.4, 26.3, 1),(17, 26.3, 1),(22.6,18.6,1 )))

#src: random points from the image of my 400px*460px image

#dst: coordinate system being in the actual image

src = np.matrix(((1, 1, 1),(400, 460, 1),(200,1,1), (100, 170, 1), (280, 320, 1),(300, 50, 1)))

dst= np.matrix(((0, 0, 1),(14.6, 19.3, 1),(17.1/2,0,1), (5.0, 9.2, 1), (11.65, 15.3, 1), (12.9, 2.9, 1) ))

H = cv2.findHomography(src,dst,0)[0]

print (H)

for c in range(0,5):

x= c*100

y = 1

print(x,y,np.dot(H,np.array((x,y,1))))

Actual Photo of the setup

正方形是(400px*460px)图片上可见的区域。摄像机位于右边的黑盒子里。X&Y是我的像素坐标。

只要你保持在x轴上,两组数字的结果都是好的。一旦我沿着y轴向下移动,数字就会出错。在

### 像素坐标实际坐标的转换 为了实现从像素坐标实际坐标的转换,通常需要利用相机的内外参数以及已知的空间几何关系。具体来说,这一过程涉及以下几个方面: #### 1. 获取相机内参矩阵 相机内参矩阵包含了焦距、主点位置等信息,这些数据可以通过校准获得。假设已经得到了相机内参矩阵 \( K \),其形式如下所示[^1]: \[K=\begin{bmatrix}f_x & s & c_x\\0& f_y &c_y \\0&0&1\end{bmatrix}\] 其中\( f_x, f_y \)代表沿X轴和Y轴方向上的焦距;\( c_x,c_y \)表示图像中心的位置;而s则用于描述两个轴之间的倾斜度,在大多数情况下可以忽略。 #### 2. 计算外参矩阵 外参矩阵由旋转和平移组成,用来表达摄像机相对于世界坐标系的姿态。设R为3×3大小的旋转矩阵,T是一个三维向量表示平移,则完整的外参矩阵P可写作: \[ P=[RT]\] 这里T应该被理解成是从世界坐标系变换至相机坐标系下的位移矢量。 #### 3. 实现像素坐标实际坐标的映射 有了上述两步准备之后,就可以建立从二维平面内的某一点 (u,v) 到它对应于真实空间中的三维点(X,Y,Z) 的联系了。这个过程涉及到解方程组来求得未知数 X Y Z 。一般会先将齐次坐标形式下得到的结果除以其最后一维分量w以恢复非齐次坐标。 给定一组像素坐标(u,v), 可按照下面的方式计算对应的物理坐标[X,Y,Z]^T: ```python import numpy as np def pixel_to_world(K, R, T, u, v): """ Convert a single point from image coordinates to world coordinates. Parameters: K : Camera intrinsic matrix R : Rotation matrix of the camera pose T : Translation vector of the camera pose u : Image coordinate x-axis value v : Image coordinate y-axis value Returns: A tuple containing three elements which are the corresponding real-world position values along each axis respectively. """ # Inverse intrinsics and extrinsics matrices inv_K = np.linalg.inv(K) RT_inv = np.hstack((R.T,-np.dot(R.T,T))) # Homogeneous representation of input points uv_homg = np.array([u,v,1]) # Transform into normalized device space xyz_cam_normlzd = np.dot(inv_K,uv_homg) # Solve for depth z using triangulation or other methods not covered here # For demonstration purposes only, assume we have some method that gives us this information z = ... # Scale up by computed distance 'z' scaled_xyz_cam = z * xyz_cam_normlzd # Finally transform back into global frame via inverse rotation/translation operation XYZ_wld = np.dot(RT_inv,scaled_xyz_cam).reshape(-1,) return list(XYZ_wld[:3]) ``` 需要注意的是,在这段代码里关于深度值`z`的部分并没有给出具体的算法去估算它——这取决于应用场景的不同可能采用不同的策略,例如立体视觉匹配、结构光编码等等。因此这部分留给开发者自行补充完成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值