在OpenCV中组合两个仿射变换矩阵

时间:2022-07-27 23:36:40

I have two 2x3 Matrices A and B - each one is for affine Transformation. I need to combine A and B into a thrid Matrix C which will combine the affine transformation from A and B into one Matrix.

我有两个2x3矩阵A和B - 每个都用于仿射变换。我需要将A和B组合成一个thrid Matrix C,它将A和B的仿射变换组合成一个矩阵。

How do I need to multiply them?

我怎么需要乘以它们?

AB or BA ?

AB还是BA?

The difference is that either A or B gets transposed or does it make any difference at all?

区别在于A或B是换位还是有任何区别?

I read a further solution is to use 3x3 matrices and copy into the first two rows and only use the first two rows in the result. But it comes down to the same question if it is AB or BA.

我读到另一个解决方案是使用3x3矩阵并复制到前两行,并且只使用结果中的前两行。但如果它是AB或BA,它归结为同样的问题。

Furthermore, is there an easy way to implement this in OpenCV or do I need to implement every step as described above?

此外,有没有一种简单的方法可以在OpenCV中实现它,还是需要实现上述每个步骤?

1 个解决方案

#1


1  

The following function combines this two matrices:

以下函数结合了这两个矩阵:

  Mat AffineTransform::concatenateMatrix(Mat first, Mat second){

        Mat mul1 = Mat::eye(3, 3, CV_64F);
        Mat mul2 = Mat::eye(3, 3, CV_64F);
        Mat x_;
        Mat temp_inv_;
        Mat mul_r;
        first.convertTo(temp_inv_, CV_64F);
        second.convertTo(x_, CV_64F);

        temp_inv_.row(0).copyTo(mul1.row(0));
        temp_inv_.row(1).copyTo(mul1.row(1));

        x_.row(1).copyTo(mul2.row(1));
        x_.row(0).copyTo(mul2.row(0));

        try{
            mul_r = mul1*mul2;
        }
        catch (Exception& e){
            const char* err_msg = e.what();
            cout << err_msg;
        }

        mul1.release();
        mul2.release();
        temp_inv_.release();

        return mul_r;
}

#1


1  

The following function combines this two matrices:

以下函数结合了这两个矩阵:

  Mat AffineTransform::concatenateMatrix(Mat first, Mat second){

        Mat mul1 = Mat::eye(3, 3, CV_64F);
        Mat mul2 = Mat::eye(3, 3, CV_64F);
        Mat x_;
        Mat temp_inv_;
        Mat mul_r;
        first.convertTo(temp_inv_, CV_64F);
        second.convertTo(x_, CV_64F);

        temp_inv_.row(0).copyTo(mul1.row(0));
        temp_inv_.row(1).copyTo(mul1.row(1));

        x_.row(1).copyTo(mul2.row(1));
        x_.row(0).copyTo(mul2.row(0));

        try{
            mul_r = mul1*mul2;
        }
        catch (Exception& e){
            const char* err_msg = e.what();
            cout << err_msg;
        }

        mul1.release();
        mul2.release();
        temp_inv_.release();

        return mul_r;
}