Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine two affine Transformations Matrices in OpenCV

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.

How do I need to multiply them?

AB or BA ?

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

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.

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

like image 927
Kev1n91 Avatar asked Oct 28 '16 13:10

Kev1n91


People also ask

How do you combine two affine transforms?

Concatenation combines two affine transformation matrices by multiplying them together. You might perform several concatenations in order to create a single affine transform that contains the cumulative effects of several transformations.

How do you combine translation and rotation matrix?

A rotation matrix and a translation matrix can be combined into a single matrix as follows, where the r's in the upper-left 3-by-3 matrix form a rotation and p, q and r form a translation vector. This matrix represents rotations followed by a translation.

What is CV warpAffine?

Code We use the function cv::warpAffine for that purpose. Applies a Rotation to the image after being transformed. This rotation is with respect to the image center. Waits until the user exits the program.


1 Answers

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;
}
like image 196
Kev1n91 Avatar answered Sep 18 '22 12:09

Kev1n91