投影

空间中的3D位姿相对于相机的变换

solvePnP ^opencv-solvepnp

  • 输入物体特征点,求解物体中心(不一定是质心,只是用于表示其位姿的坐标系原点相对于相机坐标系的变换 solvePnP得出的是用于测量物体的坐标系相对于相机坐标系的变换,即 The cv::solvePnP() returns the rotation and the translation vectors that transform a 3D point expressed in the object coordinate frame to the camera coordinate frame
  • 参数:
    • 物体特征点在三维中的测量objPoints:物体的特征点在某个坐标系下的测量结果,矩阵形状为(n,1,3)或(n,3),元素类型为浮点型
    • 物体特征点在图像中的测量imagePoints:物体的特征点在图片中的位置,矩阵形状为(n,2),元素类型为浮点型,如果是整型的像素位置会报错
    • 相机内参k:3x3的numpy矩阵,形式为
    • 相机畸变distort:5x1的numpy矩阵,如不考虑畸变,可初始化为np.array([.0]*5)
    • 可选参数为
      • `useExtrinsicGuess·
      • 求解方法flags,默认为cv::ITERATIVE,还有cv::P3Pcv::EPNP
  • 变体为solvePnPRansac(),用于提高求解的鲁棒性,处理测量结果异常的点
  • 例程
## a realsense camera
k = np.array([[898.194, 0., 635.698, 0., 898.194, 351.514, 0., 0., 1.]])
distort = np.array([.0]*5)
## object is a 100x100 mm square, find its center
objPoints = np.array([[-50,50,0], [50,50,0], [50,-50,0], [-50,-50,0]], dtype = np.float32).reshape((4,3))
## obtain by clicking on the image
imagePoints = np.array([[642., 248.], [713., 248.], [712., 316.], [643., 317.]])
valid, rvec, tvec = cv2.solvePnP(objPoints, imagePoints, np.reshape(k, (3,3)), np.array(distort))
if valid:
    ht = np.identity(4)
    rot = cv2.Rodrigues(rvec)[0]
    ht[:3, 3] = tvec.reshape(3)
    ht[:3, :3] = rot

二维图像的变换

  • 已知角度、变换中心和变换比例,获得旋转变换getRotationMatrix2D()
    • 注意角度的单位为度数(degree)而不是弧度(radian)