OpenCV: use solvePnP to determine homography

非 Y 不嫁゛ 提交于 2019-12-03 15:54:36

Your K matrix seems appropriate, but this might not be sufficient to achieve good accuracy with real images. I think instead of giving a priori reasonable values (in particular for the optical center pixel & lens distortion coefficients), you should calibrate your camera using the calibrateCamera function (documentation link, tutorials). However, I don't think the problem you are mentionning is caused by that.

I think your problem comes from the definition of P_rec.

First, be aware that if you use newZ = -1000.0, you are actually translating the camera by 1000 meters (not millimeters).

Second, you have to be very careful what 3D points you are considering and where you want them to be projected in the image:

  1. As you used XYZ_gcp in the solvePnP function, this means that you are using these coordinates as 3D points.

  2. As you used XYZ_gcp in the getPerspectiveTransform function, this means that you are also using these as 2D coordinates. Notice that, strictly speaking, you cannot do this because getPerspectiveTransform expects two 4x2 arrays (not a 4x2 and a 4x3), but I'll assume you dropped the third coordinates which are always 0.

Hence, your P_rec should be defined so that [x; y; 1] = P_rec * [x; y; 0; 1]. Therefore, P_rec should be defined as follows:

P_rec = [ [1 0 0 0] [0 1 0 0] [0 0 0 1] ].
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!