我知道在OpenCV中我们可以通过getAffineTransform()
得到两组点的仿射变换。计算给定两组点的相似变换矩阵?
但是getRotationMatrix2D()
只支持预先计算的天使和尺度。
如何计算给定两组点的相似变换矩阵?
我知道在OpenCV中我们可以通过getAffineTransform()
得到两组点的仿射变换。计算给定两组点的相似变换矩阵?
但是getRotationMatrix2D()
只支持预先计算的天使和尺度。
如何计算给定两组点的相似变换矩阵?
有cv::estimateRigidTransform
。您可以选择具有6个自由度(旋转,平移,缩放,剪切)或部分仿射(旋转,平移,均匀缩放)的完全仿射变换,它具有5个自由度。
你可以计算的相似度由两个vector<Point>
P1和P2与代码this answer变换:
cv::Mat R = cv::estimateRigidTransform(p1,p2,false);
// extend rigid transformation to use perspectiveTransform:
cv::Mat H = cv::Mat(3,3,R.type());
H.at<double>(0,0) = R.at<double>(0,0);
H.at<double>(0,1) = R.at<double>(0,1);
H.at<double>(0,2) = R.at<double>(0,2);
H.at<double>(1,0) = R.at<double>(1,0);
H.at<double>(1,1) = R.at<double>(1,1);
H.at<double>(1,2) = R.at<double>(1,2);
H.at<double>(2,0) = 0.0;
H.at<double>(2,1) = 0.0;
H.at<double>(2,2) = 1.0;
// compute perspectiveTransform on p1
std::vector<cv::Point2f> result;
cv::perspectiveTransform(p1,result,H)
//warp image with transform
cv::Mat warped;
cv::warpPerspective(src,warped,H,src.size());
我没有尝试,但指的答案它应该工作的罚款。
不知何故,在我使用的opencv版本中使用cv::estimateRigidTransform
时出现了一些问题,所以我写了一个只适用于两点的函数(对我来说这足够了,我相信它会更快)。
cv::Mat getSimilarityTransform(const cv::Point2f src[], const cv::Point2f dst[])
{
double src_d_y = src[0].y - src[1].y;
double src_d_x = src[0].x - src[1].x;
double src_dis = sqrt(pow(src_d_y, 2) + pow(src_d_x, 2));
double dst_d_y = dst[0].y - dst[1].y;
double dst_d_x = dst[0].x - dst[1].x;
double dst_dis = sqrt(pow(dst_d_y, 2) + pow(dst_d_x, 2));
double scale = dst_dis/src_dis;
// angle between two line segments
// ref: http://stackoverflow.com/questions/3365171/calculating-the-angle-between-two-lines-without-having-to-calculate-the-slope
double angle = atan2(src_d_y, src_d_x) - atan2(dst_d_y, dst_d_x);
double alpha = cos(angle)*scale;
double beta = sin(angle)*scale;
cv::Mat M(2, 3, CV_64F);
double* m = M.ptr<double>();
m[0] = alpha;
m[1] = beta;
// tx = x' -alpha*x -beta*y
// average of two points
m[2] = (dst[0].x - alpha*src[0].x - beta*src[0].y + dst[1].x - alpha*src[1].x - beta*src[1].y)/2;
m[3] = -beta;
m[4] = alpha;
// ty = y' +beta*x -alpha*y
// average of two points
m[5] = (dst[0].y + beta*src[0].x - alpha*src[0].y + dst[1].y + beta*src[1].x - alpha*src[1].y)/2;
return M;
}
有点自相矛盾的是,他们选择调用它是刚性的,当图6个4 DOF变换是恰恰不是刚性。 –
非常感谢您的回答,为什么它有5个自由度而不是4个?根据[他们的公式](http://docs.opencv.org/2.4/_images/math/0a22facbc11cdd0f9b8d4658e0c145da2cb8730b.png)只有4个参数 – dontloo
是啊我很困惑。我知道理论上第五个参数是定向的。但在那里称它为刚性的,所以e应该等于1(保持oriantation)。 – PSchn