为什么把单位矩阵等同于OpenCV中的变换矩阵?

Why equate an identity matrix to a transformation matrix in OpenCV

本文关键字:变换 等同于 单位 为什么 OpenCV      更新时间:2023-10-16

在opencv和c++中,如果我之前已经找到了2个图像之间的变换矩阵,为什么我需要这样做

Mat affineTransform=getAffineTransform(coordinates_1.data(),coordinates_2.data()) ;
Mat perspectiveTransform=Mat::eye(3,3,CV_64FC1);
for(unsigned int y=0; y<2; ++y){
    for(unsigned int x=0; x<3; ++x){
        perspectiveTransform.at<double>(y,x) = affineTransform.at<double>(y,x);
    }

而不是直接对图像应用变换矩阵。我理解Mat::eye()的意思,但是为什么要经历这些呢?

注意: originaltransformationmatrix 是一个Mat对象,找到的变换矩阵是一个3 × 3矩阵

仿射变换有如下形式:

(a, b, c)
(d, e, f)

对点(x,y)进行如下变换:

x_new = a*x + b*y + c;
y_new = d*x + e*y + f;

透视图转换有以下形式:

(a, b, c)
(d, e, f)
(g, h, 1)

对点(x,y)进行如下变换:

z = g*x + h*y + 1;
x_new = (a*x + b*y + c)/z;
y_new = (d*x + e*y + f)/z;

这意味着如果你想定义只做仿射变换的透视图变换,它应该是:

(a, b, c)
(d, e, f)
(0, 0, 1)

这正是你的代码所做的。首先创建矩阵:

(1, 0, 0)
(0, 1, 0)
(0, 0, 1)

然后用仿射变换的行代替前两行。顺便说一下,它可以用不需要循环的更简洁的方式完成:

perspectiveTransform(Rect(0,0,3,2)) = affineTransform.clone();