- Python opencv rotate image 90
- Scaling
- Translation
- Rotation
- How to rotate an image in OpenCV Python?
- Syntax
- Steps
- Input Image
- Example
- Output
- Example
- Output
- How to Rotate Image 90, 180, 270 in OpenCV Python?
- OpenCV Python – Rotate Image
- Syntax of cv2: rotate image
- Examples
- 1. Rotate image 90 degrees, 180 degrees, and 270 degrees.
- Original Image
- 90-degree rotation anti-clockwise
- 180-degree rotation anti-clockwise
- 270-degree rotation anti-clockwise
- Conclusion
- Rotate image with OpenCV in Python
- Syntax of cv2.rotate()
- 1. Rotate image by 90 degrees clockwise
- 2. Rotate image by 180 degrees
- 3. Rotate image by 270 degrees clockwise
- Summary
Python opencv rotate image 90
OpenCV provides two transformation functions, cv.warpAffine and cv.warpPerspective, with which you can perform all kinds of transformations. cv.warpAffine takes a 2×3 transformation matrix while cv.warpPerspective takes a 3×3 transformation matrix as input.
Scaling
Scaling is just resizing of the image. OpenCV comes with a function cv.resize() for this purpose. The size of the image can be specified manually, or you can specify the scaling factor. Different interpolation methods are used. Preferable interpolation methods are cv.INTER_AREA for shrinking and cv.INTER_CUBIC (slow) & cv.INTER_LINEAR for zooming. By default, the interpolation method cv.INTER_LINEAR is used for all resizing purposes. You can resize an input image with either of following methods:
Translation
Translation is the shifting of an object’s location. If you know the shift in the (x,y) direction and let it be \((t_x,t_y)\), you can create the transformation matrix \(\textbf\) as follows:
\[M = \begin 1 & 0 & t_x \\ 0 & 1 & t_y \end\]
You can take make it into a Numpy array of type np.float32 and pass it into the cv.warpAffine() function. See the below example for a shift of (100,50):
The third argument of the cv.warpAffine() function is the size of the output image, which should be in the form of **(width, height)**. Remember width = number of columns, and height = number of rows.
Rotation
Rotation of an image for an angle \(\theta\) is achieved by the transformation matrix of the form
\[M = \begin cos\theta & -sin\theta \\ sin\theta & cos\theta \end\]
But OpenCV provides scaled rotation with adjustable center of rotation so that you can rotate at any location you prefer. The modified transformation matrix is given by
\[\begin \alpha & \beta & (1- \alpha ) \cdot center.x — \beta \cdot center.y \\ — \beta & \alpha & \beta \cdot center.x + (1- \alpha ) \cdot center.y \end\]
\[\begin \alpha = scale \cdot \cos \theta , \\ \beta = scale \cdot \sin \theta \end\]
To find this transformation matrix, OpenCV provides a function, cv.getRotationMatrix2D. Check out the below example which rotates the image by 90 degree with respect to center without any scaling.
How to rotate an image in OpenCV Python?
OpenCV provides us the function cv.rotate() to rotate an image (numpy array) in multiples of 90 degrees. This function rotates an image in three possible ways: 90, 180, and 270 degrees clockwise. We use the following syntax −
Syntax
rotateCode is a rotate flag specifying how to rotate the array. The three rotate flags are as below −
Steps
To rotate an input image, you could follow the steps given below −
- Import the required libraries OpenCV and matplotlib. Make sure you have already installed them.
- Read the input image using cv2.imread() method. Specify the full path of the image.
- Rotate the input image using cv2.rotate() function. Pass the desired rotateCode to the function as a parameter. We can pass cv2.ROTATE_180, cv2.ROTATE_90_CLOCKWISE or cv2.ROTATE_90_COUNTERCLOCKWISE as a parameter.
- Display the rotated image.
Let’s see the examples to rotate an input image.
Input Image
We will use the following image as an input file in the examples below −
Example
In this example, we rotate the input image by 180 degrees clockwise.
# import required libraries import cv2 # load the input image img = cv2.imread('leaf.jpg') # rotate the image by 180 degree clockwise img_cw_180 = cv2.rotate(img, cv2.ROTATE_180) # display the rotated image cv2.imshow("Image rotated by 180 degree", img_cw_180) cv2.waitKey(0) cv2.destroyAllWindows()
Output
When you execute the above program, it will produce the following output window −
Notice that the input image is rotated 180 degrees clockwise.
Let’s see other rotations available in OpenCV.
Example
In this example, we show how to rotate the input image by 90, 180, and 270 degrees.
# import required libraries import cv2 import matplotlib.pyplot as plt # load the input image img = cv2.imread('leaf.jpg') # convert the image to grayscale img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # rotate the image by 90 degree clockwise img_cw_90 = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE) # rotate the image by 270 degree clockwise or 90 degree counterclockwise img_ccw_90 = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE) # rotate the image by 180 degree clockwise img_cw_180 = cv2.rotate(img, cv2.ROTATE_180) # display all the images plt.subplot(221), plt.imshow(img, 'gray'), plt.title('Original Image'), plt.axis('off') plt.subplot(222), plt.imshow(img_cw_90,'gray'), plt.title('(90 degree clockwise)'), plt.axis('off') plt.subplot(223), plt.imshow(img_cw_180, 'gray'), plt.title('180 degree'), plt.axis('off') plt.subplot(224), plt.imshow(img_ccw_90, 'gray'), plt.title('270 degree clockwise/\n 90 degree counter clockwise'), plt.axis('off') plt.show()
Output
The above program, on execution, will produce the following output window −
How to Rotate Image 90, 180, 270 in OpenCV Python?
In this OpenCV tutorial, we will learn how to rotate an image to 90, 180 and 270 degrees using OpenCV Python using cv2.getRotationMatrix2D() and cv2.warpAffine() functions, with an example.
OpenCV Python – Rotate Image
We can rotate an image using OpenCV to any degree.
To rotate an image using OpenCV Python, first calculate the affine matrix that does the affine transformation (linear mapping of pixels), then warp the input image with the affine matrix.
Syntax of cv2: rotate image
M = cv2.getRotationMatrix2D(center, angle, scale) rotated = cv2.warpAffine(img, M, (w, h))
- center: center of the image (the point about which rotation has to happen)
- angle: angle by which image has to be rotated in the anti-clockwise direction.
- rotated: ndarray that holds the rotated image data
- scale: 1.0 mean, the shape is preserved. Other value scales the image by the value provided.
Note: Please observe that the dimensions of the resulting image are provided same as that of the original image. When you are rotating by 90 or 270 and would to affect the height and width as well. swap height with width and width with height.
Examples
1. Rotate image 90 degrees, 180 degrees, and 270 degrees.
In this example, we will read an image, and then rotate it different angles liek 90 degrees, 180 degrees and 270 degrees. For each rotation with given degrees, we will save the image.
rotate-image.py
import cv2 # read image as grey scale img = cv2.imread('/home/arjun/Desktop/logos/python.png') # get image height, width (h, w) = img.shape[:2 ] # calculate the center of the image center = (w / 2, h / 2) angle90 = 90 angle180 = 180 angle270 = 270 scale = 1.0 # Perform the counter clockwise rotation holding at the center # 90 degrees M = cv2.getRotationMatrix2D(center, angle90, scale) rotated90 = cv2.warpAffine(img, M, (h, w)) # 180 degrees M = cv2.getRotationMatrix2D(center, angle180, scale) rotated180 = cv2.warpAffine(img, M, (w, h)) # 270 degrees M = cv2.getRotationMatrix2D(center, angle270, scale) rotated270 = cv2.warpAffine(img, M, (h, w)) cv2.imshow('Original Image',img) cv2.waitKey(0) # waits until a key is pressed cv2.destroyAllWindows() # destroys the window showing image cv2.imshow('Image rotated by 90 degrees',rotated90) cv2.waitKey(0) # waits until a key is pressed cv2.destroyAllWindows() # destroys the window showing image cv2.imshow('Image rotated by 180 degrees',rotated180) cv2.waitKey(0) # waits until a key is pressed cv2.destroyAllWindows() # destroys the window showing image cv2.imshow('Image rotated by 270 degrees',rotated270) cv2.waitKey(0) # waits until a key is pressed cv2.destroyAllWindows() # destroys the window showing image
Original Image
90-degree rotation anti-clockwise
180-degree rotation anti-clockwise
270-degree rotation anti-clockwise
Conclusion
In this OpenCV Tutorial, we have learned how to rotate an image by 90, 180 and 270 degrees.
Rotate image with OpenCV in Python
To rotate an image by 90 degrees, 180 degrees, or 270 degrees, with OpenCV in Python, you can use cv2.rotate() function.
In this tutorial, you will learn how to use cv2.rotate() function to rotate a given image by a specific angle.
Syntax of cv2.rotate()
The syntax of cv2.rotate() function is
1. Rotate image by 90 degrees clockwise
In the following program, we take an image test_image_house.jpg, rotate this image by 90 degrees, then save the rotated image to rotated_image.jpg.
Python Program
import cv2 # Load the image img = cv2.imread('test_image_house.jpg') # Rotate the image by 90 degrees clockwise rotated_img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE) # Save rotated image cv2.imwrite('rotated_image.jpg', rotated_img)
rotated_image.jpg
2. Rotate image by 180 degrees
In the following program, we rotate the image test_image_house.jpg by 180 degrees. Since, it is 180 degrees, both clockwise or counterclockwise would be same.
Python Program
import cv2 # Load the image img = cv2.imread('test_image_house.jpg') # Rotate the image by 180 degrees rotated_img = cv2.rotate(img, cv2.ROTATE_180) # Save rotated image cv2.imwrite('rotated_image.jpg', rotated_img)
rotated_image.jpg
3. Rotate image by 270 degrees clockwise
In the following program, we rotate the image test_image_house.jpg by 270 degrees clockwise, or 90 degrees counterclockwise.
Python Program
import cv2 # Load the image img = cv2.imread('test_image_house.jpg') # Rotate the image by 90 degrees counterclockwise rotated_img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE) # Save rotated image cv2.imwrite('rotated_image.jpg', rotated_img)
rotated_image.jpg
Summary
In this Python OpenCV Tutorial, we have seen how to rotate an image by 90 degrees, 180 degrees, or 270 degrees using cv2.rotate() function.