OpenCv Image Rotation

OpenCV Image Rotation in Python

We will learn how to rotate an image using python. This Technique is very helpful in data generation for computer vision tasks. Make sure you have OpenCV library installed in your python environment.

Take an input image as shown below. Import libraries 

Fig 1: Input image
 
import numpy as np
import cv2, os, glob
from matplotlib import pyplot as plt
 
Here os and glob are used to find image path if required and matplot library is used for visualization.
Read the file in python using cv2.imread('File path') command. The size of image can be large and to create standard we need to resize image to fix size so that we control the window size. Use cv2.resize(image, shape). Now use cv2.getRotationMatrix2D function and then affine transform to correctly rotate image.

rot  = cv2.getRotationMatrix2D(center, angle1.0)
rotated_image = cv2.warpAffine(image, rot, image.shape[1::-1], flags=cv2.INTER_LINEAR)

Here Center is referred to center of rotation of image and angle is the degree by which we want to  rotate an image. Note that any random angle can be given to the function, it need not be a integer. Here I have attached a Gif of rotated images.




Comments

Popular posts from this blog

Opencv python object detection Tutorial (part 1)

Background Subtraction OpenCv Python

Convolutional network based Classification