I use this code to rotate my image in 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);
It works correctly. Now I want to rotate that image back to the original image. When I use the code below, I see the locations of objects in the image are not the same as in the original image. Why is this and how can I rotate my image back?
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());
Instead of recomputing the transformation matrix, why don't you simply take its inverse?
// Invert the affine transformation
Mat rotInv;
cv::invertAffineTransform(rot, rotInv);
You can rotate it as you're already doing:
// 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());
And then compute the inverse transformation matrix and warp back:
// 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));
Full code for reference:
#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;
}
If you add the WARP_INVERSE_MAP
flag when calling warpAffine()
, it will apply the inverse transform matrix, getting you back to your original image. For example, you should be able to replace the last line of code in your question with something like the following:
warpAffine(RotatedRGB, Rotatedback, rot, RGBsrc.size(), INTER_LINEAR|WARP_INVERSE_MAP);
Note: I haven't tested this code, but I have used WARP_INVERSE_MAP
to do the same thing with warpPerspective()
and it worked perfectly.
Documentation: https://docs.opencv.org/3.4/da/d54/group__imgproc__transform.html#ga0203d9ee5fcd28d40dbc4a1ea4451983
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.