Ask Your Question

Revision history [back]

strange warpAffine precision issue

I am trying to use OpenCVs warpAffine to transform an image. I use the bilinear interpolation option (set by default). When using a very small transformation (e.g., translation of 0.01 pixel), the transformed image is exactly identical to the original. This is problematic since I need a big precision as further computations are performed on the transformed image.

Example:

Mat img_test = (Mat_<double>(5,5) << 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0);
Mat tfm; Mat transf_img_test;
tfm = Mat::eye(2,3,CV_64F);
tfm.at<double>(0,2) = 0.01;
warpAffine( img_test, transf_img_test, tfm, img_test.size(), INTER_LINEAR);
cout << "Original = " << img_test << endl;
cout << "Transformed = " << transf_img_test << endl;

The two images are exactly identical:

Original = [0, 0, 0, 0, 0;
        0, 0, 1, 0, 0;
        0, 0, 1, 0, 0;
        0, 0, 1, 0, 0;
        0, 0, 0, 0, 0]
Transformed = [0, 0, 0, 0, 0;
           0, 0, 1, 0, 0;
           0, 0, 1, 0, 0;
           0, 0, 1, 0, 0;
           0, 0, 0, 0, 0]

Note that when I set

tfm.at<double>(0,2) = 0.5

instead, warpAffine correctly transforms (and interpolates) the image.