在openCV c ++中以非常小的步长旋转图像

时间:2016-11-13 22:35:34

标签: c++ image opencv rotation transformation

我正在尝试将图像旋转1度或低于1度的小角度,

考虑这是我的源图像,我试图在 MATLAB OpenCV(C ++)中将其旋转1度:< / p>

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

当我在 MATLAB 中旋转1度时,这就是结果:

0                   0                  2.223835069639144   0
2.223835069639553   252.7942370468069  2.527942370468065   0
0                   252.7942370468065  252.794237046807    2.223835069639648
0                   2.223835069639184  0                   0

这是 MATLAB

中的代码
sourceImage = [0 0 0 0; 0 255.0 255.0 0; 0 255.0 255.0 0; 0 0 0 0];
rotationDegree = 1.0;

shearInX_Y1 = (cosd(rotationDegree)-1)/sind(rotationDegree);
shearInX_Y2 = sind(rotationDegree);

transformationMatrix = [(1 + shearInX_Y1*shearInX_Y2), (2*shearInX_Y1 + ...
    ((shearInX_Y1)^2)*shearInX_Y2), 0; (shearInX_Y2), (1 + shearInX_Y1*shearInX_Y2), 0; 0, 0, 1];

tform = affine2d(transformationMatrix);
imref = imref2d(size(sourceImage));
imref.XWorldLimits = imref.XWorldLimits-mean(imref.XWorldLimits);
imref.YWorldLimits = imref.YWorldLimits-mean(imref.YWorldLimits);
transformedImg2 = imwarp(sourceImage , imref, tform, 'OutputView', imref, 'Interp', 'bilinear');

transformedImg2 

Matlab中的transformationMatrix(我们的旋转矩阵)是:

transformationMatrix =

 9.998476951563913e-01    -1.745240643728003e-02                         0
 1.745240643728351e-02     9.998476951563913e-01                         0
                     0                         0     1.000000000000000e+00

“tform.T”的内容是:

9.998476951563913e-01    -1.745240643728003e-02      0
1.745240643728351e-02     9.998476951563913e-01      0
0                         0                          1

OpenCV中“rotationMatrix”的内容是:

9.998476951563913e-01    1.745240643728351e-02      -0.008650050796837391
-1.745240643728351e-02     9.998476951563913e-01     0.008802355640446121    

但是当我在 OpenCV(C ++)中进行1度旋转时,结果如下: (与源图像相同!)这意味着openCV存在小旋转的问题!

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

这是我在OpenCV(C ++)中用于旋转的代码:(旋转是针对图像中心完成的)

#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

# define INTERPOLATION_METHOD INTER_CUBIC
// INTER_AREA
// INTER_LINEAR
// INTER_NEAREST

Mat rotateImage(Mat sourceImage, double rotationDegree);    

int main(){

     Mat sourceImage = Mat::zeros(4, 4, CV_64F);
     sourceImage.at<double>(1, 1) = 255.0;
     sourceImage.at<double>(1, 2) = 255.0;
     sourceImage.at<double>(2, 1) = 255.0;
     sourceImage.at<double>(2, 2) = 255.0;

    double rotationDegree = 1.0;
    Mat rotatedImage = rotateImage(sourceImage, rotationDegree);    

    cout << "sourceImage: \n" << sourceImage << endl << endl;
    cout << "rotatedImage : \n" << rotatedImage << endl << endl;

    return 0;
}

Mat rotateImage(Mat sourceImage, double rotationDegree){

    double rowOfImgCenter;
    double colOfImgCenter;
    rowOfImgCenter = sourceImage.rows / 2.0 - 0.5;
    colOfImgCenter = sourceImage.cols / 2.0 - 0.5;
    Point2d imageCenter(colOfImgCenter, rowOfImgCenter);

    Mat rotationMatrix;
    rotationMatrix = getRotationMatrix2D(imageCenter, rotationDegree, 1.0);

    Mat rotatedImage;
    warpAffine(sourceImage, rotatedImage, rotationMatrix, sourceImage.size(), INTERPOLATION_METHOD);

    return rotatedImage;
}

任何想法都会受到赞赏。

0 个答案:

没有答案