2

我正在尝试在 openCV 中以亚像素精度进行平移、旋转和缩放。

这是使用亚像素精度进行翻译的链接:

在 C++ 中通过 OpenCV 以非常小的步骤翻译图像

所以,我需要对缩放和旋转做同样的事情,例如,当我将图像旋转 2 度时它可以工作,但是当我将它旋转 1 度或低于 1 度时它不起作用,并且旋转 2 度的结果相当于旋转2.1度!

在提供的链接中提到我可以通过更改 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
      };

这是一个旋转示例:源图像如下:

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

我正在相对于图像中心进行旋转。

旋转 1 度,结果与源图像相同!

对于旋转 2 度,结果与旋转 2.1 度(不应该如此)相同!

这是一个代码:

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;
}

当我检查提供的翻译代码时,它以 1e-7 精度工作,但是当我在 1e-8 列中进行翻译时,它给了我源图像而不是翻译它(这是不正确的),当我在matlab 它给了我以下正确的结果(我希望在 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 

同样,当我将图像旋转 1 度时,结果应该如下所示(这是我在 matlab 中得到的,并且希望在 c++ 中得到相同的结果)(我必须删除一些数字(降低精度),这样矩阵可以放在这里)

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

现在我的问题是如何提高 openCV 中的亚像素精度?或者是否有任何想法或代码可以按亚像素精度进行图像缩放和旋转(如上述链接中为图像翻译提供的代码)?

我会很感激任何想法。

4

0 回答 0