2

I am trying to do translation, rotation and scaling with subpixel precision in openCV.

This is the link for doing translation with subpixel pecision:

Translate image by very small step by OpenCV in C++

So, I need to do the same for scaling and rotation, as an example when I rotate image by 2 degree it works but when I rotate it by 1 or below 1 degree it doesn't work, and the result of rotation by 2 degree is the same as rotation by 2.1 degree!

In the provided link it is mentioned that I could increase the precision by changing the following code inside imgproc.hpp

enum InterpolationMasks {
       INTER_BITS      = 5,
       INTER_BITS2     = INTER_BITS * 2,
       INTER_TAB_SIZE  = 1 << INTER_BITS,
       INTER_TAB_SIZE2 = INTER_TAB_SIZE * INTER_TAB_SIZE
      };

Here is an example for rotation: Source image is the following:

0  0   0  0
0 255 255 0
0 255 255 0
0  0   0  0

I am doing rotation with respect to image center.

For rotation by 1 degree the result is same as source image!

And for rotation by 2 degree the result is same as rotation by 2.1 degree (which it should not be)!

Here is a code:

Mat rotateImage(Mat sourceImage, double rotationDegree){

    Mat rotationMatrix, rotatedImage;
    double rowOfImgCenter, colOfImgCenter;
    rowOfImgCenter = scaledImage.rows / 2.0 - 0.5;
    colOfImgCenter = scaledImage.cols / 2.0 - 0.5;
    Point2d imageCenter(colOfImgCenter, rowOfImgCenter);
    rotationMatrix = getRotationMatrix2D(imageCenter, rotationDegree, 1.0);
    warpAffine(scaledImage, rotatedImage, rotationMatrix, scaledImage.size(), INTER_AREA);

    return rotatedImage;
}

When I check the provided translation code, it works with 1e-7 precision but when I do the translation in column by 1e-8 it gives me the source image instead of translating it (which is not correct), when I do the code in matlab it gives me the following result which is correct (I want to have a same result with good accuracy in c++):

0          0                       0                 0
0   2.5499999745e+02              255       2.549999976508843e-06
0   2.5499999745e+02              255       2.549999976508843e-06    
0          0                       0                 0 

Also for rotation when I rotate the image by 1 degree the result should look like this (this is what I've got in matlab and want the same result in c++) (I had to remove some numbers (reduce the accuracy) such that the matrix could fit in here)

0                   0               1.1557313           0
1.1557313    2.5497614e+02          2.549761e+02        0
0            2.5497614e+02          2.549761e+02     1.1557313       
0            1.1557313                  0               0

Now my question is that how can I increase the subpixel precision in openCV? or Are there any ideas or codes to do the image scaling and rotation by subpixel precision (like the code provided for image translation in the mentioned link)?

I would appreciate any ideas.

Community
  • 1
  • 1
BugReader
  • 91
  • 2
  • 7
  • What 's your wanted result after roration of your sample image? – Micka Nov 13 '16 at 11:17
  • @Micka: I checked the code for translation more deeply and notice that when I translate the image by 0.00000001 (1e-8) it gives me the source image again, which it should not! and should give me the following image: 0, 0, 0, 0; 0 , 2.5499999745e+02, 255, 2.549999976508843e-06; 0, 2.5499999745e+02, 255, 2.549999976508843e-06; 0, 0, 0, 0 – BugReader Nov 13 '16 at 11:35
  • did you convert your image to float/double? uint8 obviously cant give you 254.99x values – Micka Nov 13 '16 at 11:42
  • @Micka: All of my images are in CV_64F, and all the values are in double, also I set the precision in "cout" to be cout.precision(17); which means it could show numbers with high precision. – BugReader Nov 13 '16 at 12:02

0 Answers0