Search code examples
c++opencvaffinetransform

Using OpenCVs findTransformECC to find the affine transformation applied with OpenCVs warpAffine does not give the same warp matrix back like used


I am currently trying to do a kind of selftest of the OpenCVs implementation of the function findtranfromECC (http://docs.opencv.org/3.0-beta/modules/video/doc/motion_analysis_and_object_tracking.html#findtransformecc ).

To do so I create a warp-matrix by myself and do the affine transformation with the function warpAffine given by OpenCV. After the affine Transform I use the input of the affine Transform and the warped output in the TransfromECC function. I hoped I would get back the same matrix like I used in the affine transform, bud sadly it differs a lot - it is a completly different one.

In the code example I put at the end of the post I got the following matrix for making the affine Transform:

[0.850332161003909, 0.1778601204261232, 0]
[-0.06752637272097255, 0.3701713812908899, 712.799877929688]

But the calculated Matrix by findTransformECC is:

[1.0151283, -0.0033983635, -5.6531301]
[-0.023056569, 1.038756, -8.7541409]

I also compared the eigen-Values:

For doing the affine Transform:

[127021.1755113913]
[0]

Calulated Warp-Matrix eigenvalues:

[2.945044]
[0]

Does anyone has made the same experience or might now what causes this error?

I appreciate everyones help

Point2f srcTri[3];
Point2f dstTri[3];
float scale = (float)1 / (float)255;

//Matri for first affine transform
Mat warp_mat(2, 3, CV_32FC1); 
//Matrix for back affine transform
Mat warp_mat2 = Mat::eye(2, 3, CV_32FC1);


Mat src, warp_dst, warp_rotate_dst;


//Get Image from Image class and convert it to 8 bit which is used by 
//findtransfromsEcc
this->DemosaicedDestination.convertTo(src, CV_8UC1, scale, 0);


//container for warp destination
warp_dst = Mat::zeros(src.rows, src.cols, CV_8UC1);


//Points for warp matrix
srcTri[0] = Point2f(0, 0);
srcTri[1] = Point2f(src.cols - 1, 0);
srcTri[2] = Point2f(0, src.rows - 1);

dstTri[0] = Point2f(src.cols*0.0, src.rows*0.33);
dstTri[1] = Point2f(src.cols*0.85, src.rows*0.25);
dstTri[2] = Point2f(src.cols*0.15, src.rows*0.7);


//get the affine transformation warp mat
warp_mat = getAffineTransform(srcTri, dstTri);
//Do the affine transformation
warpAffine(src, warp_dst, warp_mat, warp_dst.size());

//show affine transformation
imshow("src", src);
waitKey(0);

cout << "warp matrix used: " << warp_mat << endl;
namedWindow("warped to ", WINDOW_NORMAL);
imshow("warped to",warp_dst);

TermCriteria criteria(TermCriteria::COUNT + TermCriteria::EPS, 2500, 1e-1);
try{
    this->cc = findTransformECC(warp_dst, src, warp_mat2, MOTION_AFFINE, criteria);
}
catch (Exception& e){
    const char* err_msg = e.what();
    cout << err_msg;
    return false;
}


//Do the affine transformation back
warpAffine(warp_dst, src, warp_mat2, src.size());


//show backwarded afine transform
namedWindow("back warped transform", WINDOW_NORMAL);
imshow("back warped transform", src);
waitKey(0);


//show calculated matrix, should be the same was warp matrix used 
cout << "calculated warp matrix 1 " << warp_mat2 << endl;

Solution

  • Inversing the matrix with the given OpenCV function gave a correct solution.