Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to rotate by 90 degrees an image using OpenCV?

What is the best way (in c/c++) to rotate an IplImage/cv::Mat by 90 degrees? I would assume that there must be something better than transforming it using a matrix, but I can't seem to find anything other than that in the API and online.

like image 880
Ben H Avatar asked Feb 14 '10 00:02

Ben H


People also ask

How do you rotate a picture 90 degrees in python?

To rotate an image by an angle with a python pillow, you can use the rotate() method on the Image object. The rotate() method is used to rotate the image in a counter-clockwise direction.


2 Answers

As of OpenCV3.2, life just got a bit easier, you can now rotate an image in a single line of code:

cv::rotate(image, image, cv::ROTATE_90_CLOCKWISE); 

For the direction you can choose any of the following:

ROTATE_90_CLOCKWISE ROTATE_180 ROTATE_90_COUNTERCLOCKWISE 
like image 187
Morris Franken Avatar answered Oct 29 '22 21:10

Morris Franken


Rotation is a composition of a transpose and a flip.

R_{+90} = F_x \circ T

R_{-90} = F_y \circ T

Which in OpenCV can be written like this (Python example below):

img = cv.LoadImage("path_to_image.jpg") timg = cv.CreateImage((img.height,img.width), img.depth, img.channels) # transposed image  # rotate counter-clockwise cv.Transpose(img,timg) cv.Flip(timg,timg,flipMode=0) cv.SaveImage("rotated_counter_clockwise.jpg", timg)  # rotate clockwise cv.Transpose(img,timg) cv.Flip(timg,timg,flipMode=1) cv.SaveImage("rotated_clockwise.jpg", timg) 
like image 24
sastanin Avatar answered Oct 29 '22 22:10

sastanin