簡體   English   中英

在opencv c ++中向后旋轉圖像

[英]Rotate back an image in opencv c++

我使用此代碼在OpenCV中旋轉我的圖像:

// get rotation matrix for rotating the image around its center
Point2f center22(RGBsrc.cols/2.0, RGBsrc.rows/2.0);
Mat rot = getRotationMatrix2D(center22, Angle, 1.0);
// determine bounding rectangle
Rect bbox = RotatedRect(center22,RGBsrc.size(),Angle).boundingRect();
// adjust transformation matrix
rot.at<double>(0,2) += bbox.width/2.0 - center22.x;
rot.at<double>(1,2) += bbox.height/2.0 - center22.y;
Mat dst;
warpAffine(RGBsrc, dst, rot, bbox.size());
imshow("rotated_im", dst);

它工作正常。 現在我想將該圖像旋轉回原始圖像。 當我使用下面的代碼時,我看到圖像中對象的位置與原始圖像中的位置不同。 這是為什么以及如何將圖像旋轉回來?

Point2f center22(RGBsrc.cols/2.0, RGBsrc.rows/2.0);
Mat rot2 = getRotationMatrix2D(center22, -Angle, 1.0);
Rect bbox2 = RotatedRect(center22,RGBsrc.size(), -Angle).boundingRect();
rot2.at<double>(0,2) += bbox2.width/2.0 - center22.x;
rot2.at<double>(1,2) += bbox2.height/2.0 - center22.y;
Mat Rotatedback;
warpAffine(RotatedRGB, Rotatedback, rot2, bbox2.size());

而不是重新計算變換矩陣,為什么不簡單地采用它的逆?

// Invert the affine transformation
Mat rotInv;
cv::invertAffineTransform(rot, rotInv);

加載圖片后: 在此輸入圖像描述

您可以像以前一樣旋轉它:

// get rotation matrix for rotating the image around its center
Point2f center22(RGBsrc.cols / 2.0, RGBsrc.rows / 2.0);
Mat rot = getRotationMatrix2D(center22, Angle, 1.0);

// determine bounding rectangle
Rect bbox = RotatedRect(center22, RGBsrc.size(), Angle).boundingRect();

// adjust transformation matrix
rot.at<double>(0, 2) += bbox.width / 2.0 - center22.x;
rot.at<double>(1, 2) += bbox.height / 2.0 - center22.y;
Mat RotatedRGB;
warpAffine(RGBsrc, RotatedRGB, rot, bbox.size());

在此輸入圖像描述

然后計算逆變換矩陣並扭曲:

// Invert the affine transformation
Mat rotInv;
cv::invertAffineTransform(rot, rotInv);

// Get back the original image
Mat Rotatedback;
warpAffine(RotatedRGB, Rotatedback, rotInv, Size(RGBsrc.cols, RGBsrc.rows));

在此輸入圖像描述

完整代碼供參考:

#include <opencv2\opencv.hpp>
using namespace cv;

int main()
{
    Mat3b RGBsrc = imread("path_to_image");
    double Angle = 30.0;

    // get rotation matrix for rotating the image around its center
    Point2f center22(RGBsrc.cols / 2.0, RGBsrc.rows / 2.0);
    Mat rot = getRotationMatrix2D(center22, Angle, 1.0);

    // determine bounding rectangle
    Rect bbox = RotatedRect(center22, RGBsrc.size(), Angle).boundingRect();

    // adjust transformation matrix
    rot.at<double>(0, 2) += bbox.width / 2.0 - center22.x;
    rot.at<double>(1, 2) += bbox.height / 2.0 - center22.y;
    Mat RotatedRGB;
    warpAffine(RGBsrc, RotatedRGB, rot, bbox.size());

    // Invert the affine transformation
    Mat rotInv;
    cv::invertAffineTransform(rot, rotInv);

    // Get back the original image
    Mat Rotatedback;
    warpAffine(RotatedRGB, Rotatedback, rotInv, Size(RGBsrc.cols, RGBsrc.rows));

    imshow("Original", RGBsrc);
    imshow("Rotated", RotatedRGB);
    imshow("Rotated Back", Rotatedback);
    waitKey();

    return 0;
}

如果在調用warpAffine()時添加WARP_INVERSE_MAP標志,它將應用逆變換矩陣,使您返回原始圖像。 例如,您應該能夠使用以下內容替換問題中的最后一行代碼:

warpAffine(RotatedRGB, Rotatedback, rot, RGBsrc.size(), INTER_LINEAR|WARP_INVERSE_MAP);

注意:我沒有測試過這段代碼,但是我使用WARP_INVERSE_MAPwarpPerspective()做了同樣的事情,並且它運行得很好。

文檔: https//docs.opencv.org/3.4/da/d54/group__imgproc__transform.html#ga0203d9ee5fcd28d40dbc4a1ea4451983

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM