c++ - 在opencv c++中旋转回图像

标签 c++ image opencv image-rotation

我使用这段代码在 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);

加载图像后: enter image description here

您可以像之前那样旋转它:

// 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());

enter image description here

然后计算逆变换矩阵并回卷:

// 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));

enter image description here

完整代码供引用:

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

关于c++ - 在opencv c++中旋转回图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44023705/

相关文章:

c++ - 在 C++ 中,类型名称的 typeid 总是在编译时求值吗?

image - OpenCL 工作组是否同时执行?

c++ - 在 OpenCV 中创建随机彩色图像

javascript - 带有图像的圆 Angular 字段应动态扩展

c - 开放CV!如何捕捉视频?

c++ - 在 Windows 7 上使用 CMake 安装 OpenCV 3.1.0,opencv_ffmpeg.dll 无效哈希

python - RabbitMQ 基础发布

c++ - 如何制作一个将函数包装在 noexcept 可检测、可调用对象中的类模板,以用作 std::unique_ptr 自定义删除器?

c++ - 在 Rails 应用程序中在线编译 C++

javascript - 通过 Javascript 确定图像文件大小 + 尺寸?