Updated April 6, 2023
Introduction to OpenCV rotate image.
The following article provides an outline for OpenCV rotate image. The CV rotate() method is one of the functions present within the Open CV library that are readily available for the users to utilize. The open CV is a library present in the Python programming language, which consists of endings present Python that has been designed to solve or compute problems associated with computer vision. The open CV rotate() method is utilised to rotate a two-dimensional image at various angles congruent with 90-degree angles. When we write a code that utilizes the [cv::rotate] function, it helps in rotating an array in three different orientations, which the user has entered.
Syntax of Open CV rotate():
The following syntax is used in order to write a code for using the Open CV rotate() method:
cv2.cv.rotate( src, rotateCode [, dst]) *
Parameters of Open CV rotate():
The following parameter is used in order to write a code for using the Open CV rotate() method:
- src: It is the image that has been provided by the user of the coder, which has to be modified in accordance with its colour and space utilisation to rotate the image completely
- rotateCode: The parameter is used in order to specify the order in which the image (input in terms of an array) which the coder has used has to be rotated
- dst: It is the image that is obtained after the modification due to the operation of the Open CV rotate() method. The image obtained as output is of exactly the same size and image-centred depth compared to the original image. This parameter is optional and may not be entered by the user.
Return Value: The resultant image is modified due to the operation of the Open CV rotate() method, after which being converted into the specified angle as entered by the user or coder.
How to rotate image Using the Open CV rotate() Function?
- In order for using the Open CV library, one must firstly have the pre-requisites of OpenCV 3.4 version or a more updated version, the Python 3.6 or above updated version, the Numpy library, a source file from where the input will be taken in the form of Image, source from Webcam or Video input.
- The images first loaded into the computer name the images read, and the parameters of the colour definition and the height width and depth of the image is measured. The image centre is evaluated, and affine transformation is belated for the image, which is obtained with respect to the indexed points calculated according to the image entered.
- After this, the rotation is applied to the image with respect to the image centre and the rotation angle the programmer has specified it in accordance with the need for the problem that has been presented.
Examples of OpenCV rotate image.
Given below are the examples of OpenCV rotate image:
Example #1
Python program to illustrate the use of the cv2.rotate() method.
Code:
# importing cv2 library to operate the rotate function
import cv2
# path specified for choosing the user defined image to be rotated
path1 = r'C:\Users\user\Desktop\educba.png'
# default mode being utilized to read the image
src1 = cv2.imread(path1)
# Window name is being defined in which the image has to be displayed
window_name1 = 'Image'
# Using the cv2.rotate() method to rotate the image in the defined path
# Using cv2.ROTATE_90_CLOCKWISE rotate the image by 90 degrees clockwise
image1 = cv2.rotate(src1, cv2.cv2.ROTATE_90_CLOCKWISE)
# The rotated image is being displayed
cv2.imshow(window_name11, image)
cv2.waitKey(0)
Output:
Example #2
Code:
import cv2
# Using the cv2.rotate() method to rotate the image in the defined path
# Using cv2.ROTATE_90_CLOCKWISE rotate the image by 90 degrees clockwise
Image2 = cv2.rotate(src2, cv2.cv2.ROTATE_90_CLOCKWISE)
# The rotated image is being displayed
cv2.imshow(window_name2, Image2)
cv2.waitKey(0)
# Python program to illustrate the use of cv2.rotate() method
# importing cv2 library to operate the rotate function
import cv2
# path specified for choosing the user defined image to be rotated
path2 = r'C:\Users\user\Desktop\educba.png'
# default mode being utilized to read the image
src2 = cv2.imread(path2)
print(type(path2))
print(img.shape)
print ("Original Image which has been entered by the coder")
# (225, 400, 3)
# <class 'numpy.ndarray'>
# Window name is being defined in which the image has to be displayed
window_name1 = 'Image'
img_rotate_90_clockwise_2 = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
cv2.imwrite('C:\Users\user\Desktop\educba.png', img_rotate_90_clockwise)
print ("The image which is rotated at 90 degree or the default setting for the rotate method")
# True
img_rotate_90_counterclockwise_2 = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
cv2.imwrite('C:\Users\user\Desktop\educba.png', img_rotate_90_counterclockwise)
print (“The counter rotated image at 90 degrees which is a mirror image of the default rotation”)
# True
img_rotate_180_2 = cv2.rotate(img, cv2.ROTATE_180)
cv2.imwrite('C:\Users\user\Desktop\educba.png', img_rotate_180)
print ("The image rotated at 180 degree which a mirror image of the original image")
# True
Output:
An original image which the coder has entered.
The image which is rotated at 90 degree or the default setting for the rotate method.
The counter rotated image at 90 degrees which is a mirror image of the default rotation.
The image rotated at 180 degree, which a mirror image of the original image.
Conclusion
Although the rotation of images seems to be a complicated operation to be implemented through coding, it is still one of the most commonly needed patients, especially when dealing with problems specific to image processing and transmission. The open CV library remarkably stands out by providing built already existing functions that the developers can use in order to process and rotate the images that have to be operated upon. As the manual function for rotating images can be long and repetitive to include owing to the fact that it is a very commonly needed function, it can increase the verbosity of the code. Therefore, the Open CV rotate() methods reduce the verbosity and increase the processing time and run around time father programs developed, including the pre-built function.
Recommended Articles
We hope that this EDUCBA information on “OpenCV rotate image” was beneficial to you. You can view EDUCBA’s recommended articles for more information.