This question already has an answer here:
- Rotate image by 90, 180 or 270 degrees 9 answers
I'm capturing image from webcam and I need to rotate it by right angle. I found myself theese functions:
getRotationMatrix2D
- to create rotation matrix (whatever it is)transform
- transform one matrix to another by rotation matrix
But, I don't get anything but black area. This is my code:
if(rotate_button.click%4>0) {
double angle = (rotate_button.click%4)*90; //button increments its click by 1 per click
Mat transform_m = getRotationMatrix2D(Point(cam_frame_width/2, cam_frame_height/2), angle, 1); //Creating rotation matrix
Mat current_frame;
transform(cam_frame, current_frame, transform_m); //Transforming captured image into a new one
cam_frame = Mat((int)current_frame.cols,(int)current_frame.rows, cam_frame_type) = Scalar(0,128,0); //resizing captured matrix, so I can copy the resized one on it
current_frame.copyTo(cam_frame); //Copy resized to original
}
Outputs just black screen.
@Abhishek Thakur's answer only works well for rotating the image by 180 degrees. It does not handle the rotation by 90 degrees because
getRotationMatrix2D
is incorrect, andwarpAffline
is incorrect.Here's the code that rotates an image by 90 degrees:
Edit: Another approach to rotate images by 90,180 or 270 degrees involves doing matrix
transpose
and thenflip
. This method is probably faster.The above code works just fine, but introduces numerical error in the image due to the matrix computations being done in floating point and the warpAffine interpolation.
For a 90deg increment rotation I prefer to use the following (in python/opencv python)
Since OpenCV images in Python are 2d Numpy Arrays.
90 deg.
theImage = numpy.rot90( theImage, 1 )
270 deg.
theImage = numpy.rot90( theImage, 3 )
Note: I only tested this on gray scale images of shape ( X, Y ). If you have a color (or other multi-separation) image you might need to reshape it first to make sure that the rotation works along the correct axis.
The above answers are too complex and hog your CPU. Your question was not arbitrary rotation, but 'Rotate Opencv Matrix by 90, 180, 270 degrees'.
UPDATE 30 JUN 2017:
This functionality is supported by OpenCV, but not documented: https://github.com/opencv/opencv/blob/master/modules/core/include/opencv2/core.hpp#L1041
with
Original Answer & Arbitrary degree rotation:
You can also do this by using flip and transpose operation, ie for 90CW:
etc. Figure out the other commands yourself (thinking=learning) by introducing yourself with the transpose and flip operation form the Docs.
So you call it like this, and note the matrix is passed by reference.
Rotate by arbitrary degrees While I'm at it, here is how to rotate by an arbitrary degree, which i expect to be 50x more expensive. Note that rotation in this manner will include black padding, and edges will be rotated to oustide of the image's original size.
Calling this for a rotation of 10.5 degrees then is obviously:
I find it remarkable that these kind of extremely basic functions are not part of OpenCV, while OpenCV does have native things like face detection (that's not really maintained with questionable performance). Remarkable.
Cheers
Use warpAffine.:
Try:
dst
is the final image