1

I am writing some code that rotates an OpenCV Mat by a specified angle. Using the following code, I have successfully rotated the Mat around its center, but due to the fact that the image is not square, the center of the rotated image is not the center of the new image, and part of the image falls outside of the new Mat.

Point2f src_center(source.cols/2.0F, source.rows/2.0F);
Mat rot_mat = getRotationMatrix2D(src_center, angle, 1.0);

Mat final;

final.create(calculateHeight(source.cols, source.rows, angle), calculateWidth(source.cols, source.rows, angle), CV_32FC1);
warpAffine(source, final, rot_mat, final.size());

Note: calculateHeight and calculateWidth are methods that simply determine the size of the new Mat based on the dimensions of the original Mat and the angle that it is being rotated.

Example: Original image rotates to: rotated image

How do I rotate the Mat in such a way that the resulting rotated Mat is centered within the boundaries?

KSletmoe
  • 977
  • 9
  • 23
  • @rold2007 This question was asked 2 years before the question you linked. The above question does have what I was looking for at the time, but it is the duplicate. – KSletmoe Feb 14 '15 at 03:13
  • Yes, I noticed before but the other way around isn't possible as your question doesn't have an accepted answer. I don't know how this is supposed to be handled so I thought a moderator would know, hence the duplicate flag. – rold2007 Feb 14 '15 at 06:11

1 Answers1

0

Just copy the image you want to rotate to the center of a big enough new image and rotate the new image.

nhkode
  • 1,286
  • 1
  • 9
  • 5