Rotating Images
cv2.rotate() method is used to rotate a 2D array in multiples of 90 degrees. The function cv::rotate rotates the array in three different ways.
Example: Python OpenCV Rotate Image
Python3
# Python program to explain cv2.rotate() method # importing cv2 import cv2 # path path = 'geeks.png' # Reading an image in default mode src = cv2.imread(path) # Window name in which image is displayed window_name = 'Image' # Using cv2.rotate() method # Using cv2.ROTATE_90_CLOCKWISE rotate # by 90 degrees clockwise image = cv2.rotate(src, cv2.cv2.ROTATE_90_CLOCKWISE) # Displaying the image cv2.imshow(window_name, image) cv2.waitKey( 0 ) |
Output:
The above functions restrict us to rotate the image in the multiple of 90 degrees only. We can also rotate the image to any angle by defining the rotation matrix listing rotation point, degree of rotation, and the scaling factor.
Example: Python OpenCV Rotate Image by any Angle
Python3
import cv2 import numpy as np FILE_NAME = 'geeks.png' # Read image from the disk. img = cv2.imread(FILE_NAME) # Shape of image in terms of pixels. (rows, cols) = img.shape[: 2 ] # getRotationMatrix2D creates a matrix needed # for transformation. We want matrix for rotation # w.r.t center to 45 degree without scaling. M = cv2.getRotationMatrix2D((cols / 2 , rows / 2 ), 45 , 1 ) res = cv2.warpAffine(img, M, (cols, rows)) cv2.imshow( "w3wiki" , res) cv2.waitKey( 0 ) cv2.destroyAllWindows() |
Output:
Getting Started with Python OpenCV
Computer Vision is one of the techniques from which we can understand images and videos and can extract information from them. It is a subset of artificial intelligence that collects information from digital images or videos.
Python OpenCV is the most popular computer vision library. By using it, one can process images and videos to identify objects, faces, or even handwriting of a human. When it is integrated with various libraries, such as NumPy, python is capable of processing the OpenCV array structure for analysis.
In this article, we will discuss Python OpenCV in detail along with some common operations like resizing, cropping, reading, saving images, etc with the help of good examples.