I am working on opencv , halcon. I am trying to implement halcon's gen_image_to_world_plane_map
algorithm.
I read some articles about homography martix
, and I tried to calculate using intrinsic
and extrinsic
camera matrices.
Unfortunately, result is wrong.
- https://towardsdatascience.com/estimating-a-homography-matrix-522c70ec4b2c
- Compute Homography Matrix based on intrinsic and extrinsic camera parameters
Mat cameraMatrix, distCoeffs; vector<Mat> rvecs, tvecs;
cv::calibrateCamera(objectPoints, imagePoints, img.size(), cameraMatrix, distCoeffs, rvecs, tvecs);
Mat R;
Rodrigues(rvecs[0], R);
Mat T = tvecs[0];
Mat P(3, 4, CV_64F);
R.copyTo(P(cv::Rect(0, 0, 3, 3)));
//T.copyTo(P(cv::Rect(3, 0, 1, 3)));
T.copyTo(P(cv::Rect(2, 0, 1, 3)));
Mat P1 = P.colRange(0, 3);
Mat H = cameraMatrix * P1;
H /= H.at<double>(2, 2);
cv::Mat rectified;
cv::warpPerspective(img, rectified, H, img.size());