How to Rotate an Image in OpenCV Python?
OpenCV is a widely used computer vision library for various applications, including image and video processing. One of the essential functions of image processing is rotating an image. In this article, we shall discuss how to rotate an image in OpenCV using Python.
Understanding Rotation in Image Processing
Rotation in image processing is the process of rotating an image around a specific point. Precisely, the rotation moves all pixels around the center point of an image by the specified angle in degrees, radians, or another representation. Rotation can be performed in either clockwise or anticlockwise direction.
Rotating an Image in OpenCV Python
OpenCV Python provides many functions for image processing, including rotation. The cv2.rotate()
function is one of the methods used to rotate an image in OpenCV Python.
The cv2.rotate()
function takes two arguments: the input image and the rotation type. The rotation type is an integer value that represents the rotation direction and angle. The available rotation types are:
cv2.ROTATE_90_CLOCKWISE
: Rotate the image 90 degrees clockwise.cv2.ROTATE_180
: Rotate the image 180 degrees.cv2.ROTATE_90_COUNTERCLOCKWISE
: Rotate the image 90 degrees counterclockwise.
The following code snippet shows how to use the cv2.rotate()
function to rotate an image.
import cv2
# Load an Input Image
input_image = cv2.imread('input_image.jpg')
# Rotate the Image by 90 Degrees Clockwise
rotated_image = cv2.rotate(input_image, cv2.ROTATE_90_CLOCKWISE)
# Show the Resulting Image
cv2.imshow('Rotated Image', rotated_image)
cv2.waitKey(0) # Press any key to exit
In the code above, we first load the input image using the cv2.imread()
function and store it in the input_image
variable. Then, we rotate the image 90 degrees clockwise using the cv2.rotate()
function with a cv2.ROTATE_90_CLOCKWISE
argument representing the rotation type. Finally, we display the rotated image using the cv2.imshow()
function and wait until a key is pressed to exit.
Rotating an Image at Any Angle
In addition to the predefined rotation types, OpenCV in Python can rotate an image at any arbitrary angle using the following steps:
- Determine the image’s center point.
- Calculate the rotation matrix using the
cv2.getRotationMatrix2D()
function, which takes the center point, angle, and scale as arguments. - Apply the rotation matrix to the image using the
cv2.warpAffine()
function.
The following sample code illustrates the above steps, where we rotate an image at an angle of 30 degrees.
import cv2
import numpy as np
# Load Image
input_image = cv2.imread('input_image.jpg')
# Get Image Height and Width
(height, width) = input_image.shape[:2]
# Define Center and angle of Rotation
center = (width // 2, height // 2)
angle = 30
scale = 1.0
# Calculate the Rotation Matrix
rotation_matrix = cv2.getRotationMatrix2D(center, angle, scale)
# Apply the Rotation Matrix to the Image
rotated_image = cv2.warpAffine(input_image, rotation_matrix, (width, height))
# Show the Resulting Image
cv2.imshow('Rotated Image', rotated_image)
cv2.waitKey(0) # Press any key to exit
In the code above, we first load the input image using the cv2.imread()
function and store it in the input_image
variable. Then, we get the image’s dimensions using the shape
function.
Next, we define the center point of the image by dividing the width and height by two using the //
operator. We also define the rotation angle and scale, which are 30 degrees and 1.0, respectively.
We then calculate the rotation matrix using the cv2.getRotationMatrix2D()
function, which takes the center point, angle, and scale as arguments.
Lastly, we apply the rotation matrix to the image using the cv2.warpAffine()
function and show the resulting image using the cv2.imshow()
function.
Conclusion
In conclusion, this article has discussed how to rotate an image in OpenCV Python. We have learned that rotation in image processing is the process of rotating an image around a specific point, and OpenCV provides the cv2.rotate()
function to perform predefined rotations. We have also seen how to rotate an image at any arbitrary angle using the cv2.getRotationMatrix2D()
function andthe cv2.warpAffine()
function. With these methods, you can effectively rotate an image in any direction and produce various image effects.
Image rotation is a fundamental operation in image processing and computer vision. It can be useful for various applications, such as image registration, image alignment, and image augmentation. OpenCV Python provides robust methods to rotate an image with ease, and the code samples above should help you get started.
In summary, with OpenCV Python, you can easily manipulate and process your images to create unique results. Don’t be afraid to experiment and explore its vast capabilities.