How to Rotate Image Using OpenCV in Computer Vision
To rotate an image in OpenCV, use
cv2.getRotationMatrix2D to create a rotation matrix and then apply cv2.warpAffine to perform the rotation. This method lets you specify the rotation center, angle in degrees, and scale factor.Syntax
The rotation process in OpenCV involves two main steps:
- Get rotation matrix:
cv2.getRotationMatrix2D(center, angle, scale)creates a matrix to rotate the image around a point. - Apply rotation:
cv2.warpAffine(image, rotation_matrix, output_size)applies the rotation matrix to the image.
Parameters explained:
center: The point (x, y) around which to rotate the image, usually the image center.angle: Rotation angle in degrees. Positive values rotate counter-clockwise.scale: Scaling factor to resize the image during rotation (1.0 means no scaling).output_size: Size (width, height) of the output image.
python
rotation_matrix = cv2.getRotationMatrix2D(center, angle, scale) rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height))
Example
This example loads an image, rotates it 45 degrees around its center without scaling, and displays both original and rotated images.
python
import cv2 # Load image image = cv2.imread('input.jpg') height, width = image.shape[:2] # Define rotation center center = (width // 2, height // 2) angle = 45 # degrees scale = 1.0 # no scaling # Get rotation matrix rotation_matrix = cv2.getRotationMatrix2D(center, angle, scale) # Rotate image rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height)) # Save rotated image cv2.imwrite('rotated_output.jpg', rotated_image) # Optional: Display images (requires GUI support) # cv2.imshow('Original', image) # cv2.imshow('Rotated', rotated_image) # cv2.waitKey(0) # cv2.destroyAllWindows()
Output
Creates a file 'rotated_output.jpg' with the image rotated 45 degrees around its center.
Common Pitfalls
- Wrong rotation center: Not using the image center can cause unexpected cropping or shifting.
- Output size mismatch: Using the original image size for output can crop rotated corners; consider adjusting size to fit rotated image.
- Angle direction confusion: Positive angles rotate counter-clockwise; negative angles rotate clockwise.
- Scaling issues: Forgetting to set scale to 1.0 can resize the image unintentionally.
python
import cv2 image = cv2.imread('input.jpg') height, width = image.shape[:2] # Wrong: rotation center at (0,0) causes shift wrong_center = (0, 0) rotation_matrix_wrong = cv2.getRotationMatrix2D(wrong_center, 45, 1.0) rotated_wrong = cv2.warpAffine(image, rotation_matrix_wrong, (width, height)) cv2.imwrite('rotated_wrong.jpg', rotated_wrong) # Right: rotation center at image center center = (width // 2, height // 2) rotation_matrix_right = cv2.getRotationMatrix2D(center, 45, 1.0) rotated_right = cv2.warpAffine(image, rotation_matrix_right, (width, height)) cv2.imwrite('rotated_right.jpg', rotated_right)
Output
Creates 'rotated_wrong.jpg' with shifted image and 'rotated_right.jpg' correctly rotated around center.
Quick Reference
Tips for rotating images in OpenCV:
- Use
cv2.getRotationMatrix2Dwith the image center for natural rotation. - Set
scale=1.0to keep original size unless resizing is needed. - Adjust output size if you want to avoid cropping rotated corners.
- Remember positive angles rotate counter-clockwise.
Key Takeaways
Use cv2.getRotationMatrix2D with the image center, angle, and scale to create the rotation matrix.
Apply cv2.warpAffine with the rotation matrix and output size to rotate the image.
Positive angles rotate counter-clockwise; negative angles rotate clockwise.
Ensure the rotation center is correct to avoid unwanted image shifts.
Adjust output size to prevent cropping of rotated image corners.