Using the nice addons ofxFern for augmented reality I get 4 corners points as 2d coordinates, I am trying to find the 3d orientation as euler angles in order to use 3d assets with the AR tracking.
I have something almost working using cv::solvePnpRansac, sometime the rotation works but is not very accurate, maybe I am not using solvePnpRansac correctly?
ofPoint getOrientation(ofPoint pts[4]) {
ofxCv::Calibration calibration;
calibration.load("kinect-color.yml");
vector<cv::Point2f> cornersp;
cornersp.push_back(cv::Point2f(pts[0].x, pts[0].y));
cornersp.push_back(cv::Point2f(pts[1].x, pts[1].y));
cornersp.push_back(cv::Point2f(pts[2].x, pts[2].y));
cornersp.push_back(cv::Point2f(pts[3].x, pts[3].y));
float pageWidth = (ofDist(pts[1].x, pts[1].y, pts[2].x, pts[2].y) + ofDist(pts[0].x, pts[0].y, pts[3].x, pts[3].y)) / 2;
float pageHeight = (ofDist(pts[0].x, pts[0].y, pts[1].x, pts[1].y) + ofDist(pts[2].x, pts[2].y, pts[3].x, pts[3].y)) / 2;
vector<cv::Point3f> objp;
objp.push_back(cv::Point3f(0, pageHeight,0));
objp.push_back(cv::Point3f(0, 0, 0));
objp.push_back(cv::Point3f(pageWidth, 0, 0));
objp.push_back(cv::Point3f(pageWidth, pageHeight, 0));
cv::Mat cameraMatrix = calibration.getDistortedIntrinsics().getCameraMatrix();
cv::Mat distortion = calibration.getDistCoeffs();
cv::Mat translation;
cv::Mat rotation;
cv::solvePnPRansac(objp, cornersp, cameraMatrix, distortion, rotation, translation, false);
ofPoint euler(ofRadToDeg(rotation.at<double>(0)), ofRadToDeg(rotation.at<double>(1)), ofRadToDeg(rotation.at<double>(2)));
return euler;
}
I read ‘coplanar posit’ could also do the job, maybe there are other suggestion?