Easiest way to rotate by 90 degrees an image using OpenCV?
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
Rotate an image by 90 degrees using Eigen from OpenCV Matrix in C++
I guess that the function warpAffine
is faster. At least you should compare to check.
There is an example here:
https://docs.opencv.org/master/dd/d52/tutorial_js_geometric_transformations.html.
The same kind of functions are available with cuda:
https://docs.opencv.org/master/db/d29/group__cudawarping.html
EDIT:warpAffine
in OpenCV can actually use the ippiWarpAffine*
function from the Intel Performance Primitives library. This is probably the fastest performance that could get. The cuda version is expected to be faster if you can run your software on a platform with an nvidia gpu. The performance depends on the type of data that you use. If you can use 8bit unsigned images you can be much faster.
EDIT 2:
After the comment saying that warpAffine is slower I ran a few tests and it can sometimes be faster. However, when compare to the numpy's rotate there is nothing comparable, even a cv2.flip or cv2.transpose are way slower. Therefore I would recommend to look into this recommendation on Intel's developer zone which is to use ippiRotate and ippiMirror functions to perform 90 rotations. If you are really interested into getting the best performance out of an Intel cpu, that would be my guess. Also take care about the multithreading, some functions can be multithreaded in IPP. In the end this depend if you look for a solution to rotate a single large image or multiple ones, of the type of data, the number of channels. With IPP at least you use the best function for your type of data.
Hereafter a few trials in python to compare with numpy's rot90
function. Of course the results can change with the parameters but still there is a large difference with numpy. It is also not obvious from my trials that cv2.rotate is so faster.
100x np.rot90 time : 0.001626729965209961
100x cv2.rotate time : 0.21501994132995605
100x cv2.transpose time : 0.18512678146362305
100x cv2.remap time : 0.6473801136016846
100x cv2.warpAffine time : 0.11946868896484375
import cv2
import numpy as np
import time
img = np.random.randint(0, 255, (1000, 1000, 3)).astype(np.uint8)
##################################
start = time.time()
for i in range(100):
rotated = np.rot90(img)
end = time.time()
print("100x np.rot90 time :", end - start)
##################################
start = time.time()
for i in range(100):
rotated = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
end = time.time()
print("100x cv2.rotate time :", end - start)
##################################
start = time.time()
for i in range(100):
rotated = cv2.transpose(img, 1)
end = time.time()
print("100x cv2.transpose time :", end - start)
##################################
mapx, mapy = np.meshgrid(np.arange(0, img.shape[1]), np.arange(0, img.shape[0]))
mapx = mapx.transpose()
mapy = mapy.transpose()
start = time.time()
for i in range(100):
rotated = cv2.remap(img, mapx.astype(np.float32), mapy.astype(np.float32), cv2.INTER_NEAREST)
end = time.time()
print("100x cv2.remap time :", end - start)
##################################
rows = img.shape[0]
cols = img.shape[1]
M = cv2.getRotationMatrix2D((rows / 2, cols / 2), 90, 1)
M[0, 2] = 0
M[1, 2] = cols
start = time.time()
for i in range(100):
rotated = cv2.warpAffine(img, M, (rows, cols), flags=cv2.INTER_NEAREST)
end = time.time()
print("100x cv2.warpAffine time :", end - start)
I hope this helps!
Image rotation(by 90 degress) in OpenCV
It's not the most efficient way, but it's so easy
cv::transpose(img, res);
cv::flip(res, res, 1);
Rotate Opencv Matrix by 90, 180, 270 degrees
Use warpAffine.:
Try:
Point2f src_center(source.cols/2.0F, source.rows/2.0F);
Mat rot_mat = getRotationMatrix2D(src_center, angle, 1.0);
Mat dst;
warpAffine(source, dst, rot_mat, source.size());
dst
is the final image
Rotate image by 90, 180 or 270 degrees
This is the first result when you Google it and none of these solutions really answer the question or is correct or succinct.
Core.rotate(Mat src, Mat dst, Core.ROTATE_90_CLOCKWISE); //ROTATE_180 or ROTATE_90_COUNTERCLOCKWISE
Related Topics
Empirically Determine Value Category of C++11 Expression
Simulating Key Press Events in MAC Os X
Why Does Visual Studio 2013 Error on C4996
Populate an Array Using Constexpr at Compile-Time
Math to Convert Seconds Since 1970 into Date and Vice Versa
Why Do We Need to Use 'Int Main' and Not 'Void Main' in C++
Converting Std::String to Std::Vector<Char>
Prefix/Postfix Increment Operators
How to Automatically Register a Class on Creation
Multiple Definition Error Including C++ Header File with Inline Code from Multiple Sources
When Is a Function Try Block Useful
Difference in Behavior While Using Dynamic_Cast with Reference and Pointers
Weird Behaviour with Const_Cast