4

My objective is to rotate an image by a certain angle (e.g. 30 degrees). One possible way of rotating by 90 degrees in OpenCV is given by tenta4 but unfortunately, it only performs 90-degree flips.

Another possible way is a method "SkewGrayImage" given in JavaCV samples where it performs "small angle rotations" that appear to work for rotations of up to approximately 45 - 50 degrees but not for any other higher values.

So - my issues is, is there a proper way/method in OpenCV or JavaCV to actually perform an angular rotation of images or objects?

Psi-Ed
  • 683
  • 1
  • 9
  • 22
  • 1
    See [here](https://docs.opencv.org/3.3.1/d4/d61/tutorial_warp_affine.html) for a C++ example. – Catree Nov 21 '17 at 11:00

4 Answers4

4

Meta has explained how to compute a rotation matrix with respect to the center of the image and then to perform a rotation as follows:

    Mat rotated_image;
    warpAffine(src, rotated_image, rot_mat, src.size());
2

there is an operation that's called warp, and it is able to just rotate, but also to do some other transformations on the image.

Some useful links are here

Hope it helps ;)

sop
  • 3,445
  • 8
  • 41
  • 84
2

A more detailed answer for IplImage rotation is given by Martin based on Mat variables which can then be converted and returned as an IplImage as follows:

Mat source = imread(argv[1], CV_LOAD_IMAGE_COLOR);
Mat rotation_matrix = getRotationMatrix2D(src_center, angle, 1.0);
Mat destinationMat;
warpAffine(source, destinationMat, rotation_matrix, source.size());
IplImage iplframe = IplImage(destinationMat);
Amelia J
  • 21
  • 2
1

Hope this helps! Worked for me with JavaCV.

Mat raw = ... // your raw mat

// Create your "new" Mat and the center of your Raw Mat
Mat result = new Mat(raw.size(), [your Image Type]); // my Img type was CV_8U
Point2f rawCenter = new Point2f(raw.cols() / 2.0F, raw.rows() / 2.0F);

// Scale and Rotation of new Mat
double scale = 1.0;
int rotation = -5;

// Rotation Matrix
Mat rotationMatrix = getRotationMatrix2D(rawCenter, rotation, scale);

// Rotate 
warpAffine(raw, result, rotationMatrix, raw.size());
aignerjo
  • 21
  • 4