JavaCV or OpenCV Image Rotation

Viewed 1538

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?

4 Answers

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

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

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