void TransportpointforH(Point2f &inpoint, Point2f &outpoint, Mat H) {
cv::Mat_<double> mat_point(3, 1);
mat_point(0, 0) = inpoint.x;
mat_point(1, 0) = inpoint.y;
mat_point(2, 0) = 1;
Mat mat_tmp = H * mat_point;
double a1 = mat_tmp.at<double>(0, 0);
double a2 = mat_tmp.at<double>(1, 0);
double a3 = mat_tmp.at<double>(2, 0);
outpoint.x = a1 / a3;
outpoint.y = a2 / a3;
}
findHomography: 计算多个二维点对之间的最优单映射变换矩阵 H(3行x3列) ,使用最小均方误差或者RANSAC方法
函数功能:找到两个平面之间的转换矩阵。
Mat cv::findHomography ( InputArray srcPoints,
InputArray dstPoints,
int method = 0,
double ransacReprojThreshold = 3,
OutputArray mask = noArray(),
const int maxIters = 2000,
const double confidence = 0.995
)
具体实现
vector<Point2f> imgp,groundp;
imgp.push_back(Point2f(1260,864));
imgp.push_back(Point2f(1165,784));
imgp.push_back(Point2f(630,852));
imgp.push_back(Point2f(733,773));
groundp.push_back(Point2f(0.9,3.32));
groundp.push_back(Point2f(0.9,4.82));
groundp.push_back(Point2f(-1.1,3.32));
groundp.push_back(Point2f(-1.1,4.85));
Mat H = findHomography(imgp, groundp);
Point2f pout;
TransportpointforH(Point2f(733, 773), pout, H);
cout << pout <<endl;
|