π‘ Problem Formulation: Rotating an image is a common image processing task that involves turning the image around a given point at a specified angle. For instance, you may have an image that was taken at a slight angle, and you want to straighten the horizon line. The desired output would be a corrected image that has been rotated to align horizontally or to any given angle as per requirement.
Method 1: Rotate with cv2.getRotationMatrix2D and cv2.warpAffine
This method involves calculating a rotation matrix using cv2.getRotationMatrix2D
, which takes the center of rotation, the rotation angle in degrees, and a scale factor as arguments. The matrix is then supplied to cv2.warpAffine
to perform the actual rotation.
Here’s an example:
import cv2 image = cv2.imread('input.jpg') height, width = image.shape[:2] center = (width/2, height/2) angle = 45 scale = 1.0 rotation_matrix = cv2.getRotationMatrix2D(center, angle, scale) rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height)) cv2.imwrite('rotated_image.jpg', rotated_image)
The output of this code snippet will be ‘rotated_image.jpg’, which is the input image rotated by 45 degrees around its center.
This code begins by reading the input image and calculating its center. It defines the desired rotation angle and scale factor. The rotation matrix is computed and passed along with the image to cv2.warpAffine
, which then outputs the rotated image.
Method 2: Rotate Using cv2.rotate
The cv2.rotate
function offers a straightforward way to rotate an image by 90, 180, or 270 degrees without needing to calculate a rotation matrix explicitly.
Here’s an example:
import cv2 image = cv2.imread('input.jpg') rotated_image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE) cv2.imwrite('rotated_image.jpg', rotated_image)
The output is ‘rotated_image.jpg’, with the input image rotated 90 degrees clockwise.
In this snippet, the image is loaded and then rotated using cv2.rotate
with a predefined rotation code cv2.ROTATE_90_CLOCKWISE
. This results in a 90-degree clockwise rotation, simplifying rotations at right angles.
Method 3: Manual Rotation via Numpy
Rather than using OpenCV functions, this method manipulates the image array directly with Numpy to achieve the rotation.
Here’s an example:
import cv2 import numpy as np image = cv2.imread('input.jpg') rotated_image = np.rot90(image) cv2.imwrite('rotated_image.jpg', rotated_image)
This outputs ‘rotated_image.jpg’, which is the original image rotated 90 degrees counter-clockwise.
The code leverages Numpy’s np.rot90
method, which rotates the image 90 degrees counter-clockwise. The rotated array is then written back to ‘rotated_image.jpg’. This is useful for situations where 90-degree increments suffice and avoids OpenCV specific functions.
Method 4: Custom Rotation Function
For a custom rotation, one can write a function that utilizes the above steps but allows for more flexible input, such as an arbitrary rotation angle or a reshaped output image.
Here’s an example:
import cv2 import numpy as np def rotate_image(image, angle): height, width = image.shape[:2] center = (width/2, height/2) scale = 1.0 rotation_matrix = cv2.getRotationMatrix2D(center, angle, scale) cos = np.abs(rotation_matrix[0, 0]) sin = np.abs(rotation_matrix[0, 1]) new_width = int((height * sin) + (width * cos)) new_height = int((height * cos) + (width * sin)) rotation_matrix[0, 2] += (new_width / 2) - center[0] rotation_matrix[1, 2] += (new_height / 2) - center[1] return cv2.warpAffine(image, rotation_matrix, (new_width, new_height)) image = cv2.imread('input.jpg') angle = 30 rotated_image = rotate_image(image, angle) cv2.imwrite('rotated_image.jpg', rotated_image)
This results in ‘rotated_image.jpg’, which is the input image rotated by an arbitrary angle, corrected for any loss of corners.
This function generalizes the rotation process and accounts for the size change to ensure that no corners of the image are lost. It calculates the new dimensions to accommodate the entire rotated image and adjusts the rotation matrix accordingly.
Bonus One-Liner Method 5: Using imutils.rotate
The imutils library provides a convenient method rotate
which allows rotating an image with automatic resizing to ensure the whole image stays in view.
Here’s an example:
import cv2 from imutils import rotate image = cv2.imread('input.jpg') rotated_image = rotate(image, angle=45) cv2.imwrite('rotated_image.jpg', rotated_image)
The resulting ‘rotated_image.jpg’ will be rotated by 45 degrees with all parts of the image visible.
The rotate
function from imutils offers a quick and easy way to rotate images with automatic boundary adjustments. This one-liner can be very handy for simple rotations without the need for explicit matrix calculations.
Summary/Discussion
- Method 1: Rotate with cv2.getRotationMatrix2D and cv2.warpAffine. Offers precise control over the center and scale of rotation. Can result in parts of the image being cut off if not resized properly.
- Method 2: Rotate Using cv2.rotate. Simplified method for right-angle rotations. Limited to increments of 90 degrees and does not support arbitrary angles.
- Method 3: Manual Rotation via Numpy. Good for quick, 90-degree rotations using array manipulation. Not suitable for non-right-angle rotations.
- Method 4: Custom Rotation Function. Highly customizable and ensures the rotated image retains all its parts. More complex than other methods and may require additional adjustments depending on the image.
- Method 5: Using imutils.rotate. Extremely simple and ensures full image visibility. Does not give control over center or scale and requires an additional library.