OpenCV+OpenGL单应矩阵转换模型视图矩阵后投影位置错误求助
OpenCV单应性矩阵转OpenGL模型视图矩阵投影偏移问题
我有一个程序需要使用OpenCV和OpenGL将3D模型投影到场景中。首先我通过cv2.findHomography()得到3×3单应性矩阵,再调用cv2.decomposeHomographyMat()求解旋转矩阵和平移向量,该接口会分别返回4组可能的旋转、平移结果,我逐一测试后发现4组结果对应的投影点差异极小,几乎一致。
随后我使用单应矩阵分解得到的旋转、平移向量计算OpenGL投影所需的模型视图矩阵,但最终得到的投影位置不正确。这里的位置不正确指的是模型没有投影到预期位置,而是偏移到了其他位置。
相关代码如下:
... homography_matrix, _ = cv2.findHomography(set_point1, set_point2,cv2.RANSAC,5) # 单应性矩阵取值如下 [[ 6.49469641e-01 3.05514095e-02 2.22136754e+02] [-1.44896635e-02 5.80832563e-01 1.22989887e+02] [ 9.15442047e-05 -6.03875028e-05 1.00000000e+00]] # 计算模型视图矩阵 num, rvec, tvec, _ = cv2.decomposeHomographyMat(homography_matrix, calibration_matrix) rmtx = np.array(rvec) tmtx = np.array(tvec) # 旋转向量 (array([[ 0.9975257 , 0.06259469, -0.03200593], [-0.0590899 , 0.99316086, 0.10069705], [ 0.03809014, -0.09855667, 0.99440219]]), array([[ 0.9975257 , 0.06259469, -0.03200593], [-0.0590899 , 0.99316086, 0.10069705], [ 0.03809014, -0.09855667, 0.99440219]]), array([[ 0.97572152, 0.09882652, 0.19545034], [-0.08918155, 0.99435029, -0.05756872], [-0.20003542, 0.03874048, 0.97902248]]), array([[ 0.97572152, 0.09882652, 0.19545034], [-0.08918155, 0.99435029, -0.05756872], [-0.20003542, 0.03874048, 0.97902248]])) # 平移向量 (array([[ 0.33814957], [-0.10127911], [ 0.6865044 ]]), array([[-0.33814957], [ 0.10127911], [-0.6865044 ]]), array([[0.11881547], [0.0624517 ], [0.76017995]]), array([[-0.11881547], [-0.0624517 ], [-0.76017995]])) view_matrix = np.array([[rmtx[0][0][0],rmtx[0][0][1],rmtx[0][0][2], tmtx[0][0]], [rmtx[0][1][0],rmtx[0][1][1],rmtx[0][1][2], tmtx[0][1]], [rmtx[0][2][0],rmtx[0][2][1],rmtx[0][2][2], tmtx[0][2]], [0.0 ,0.0 ,0.0 ,1.0 ]]) view_matrix = np.transpose(view_matrix) ... glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(45, (width / height), 0.1, 50.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glTranslatef(0.0,0.0,-10.0) glMultMatrixd(view_matrix) glRotatef(angle,0,1,0)
输出示例:附投影效果对比图
内容的提问来源于stack exchange,提问作者Yew
相关产品推荐
相关产品推荐

