In this section, we will learn how we can perform a rotation by using OpenCV and the rotation matrix, M. A rotation matrix is a matrix that is used to perform a rotation in Euclidean space. It rotates points in the xy plane counterclockwise through an angle, 𝜃, around the origin.
Now we will implement image rotation using OpenCV:
- We will first import the matplotlib (mpimg and pyplot), numpy, and openCV libraries:
In[1]: import cv2
In[2]: import numpy as np
In[3]: import matplotlib.image as mpimg
In[4]: from matplotlib import pyplot as plt
In[5]: %matplotlib inline
- Next, we will read the input image:
In[5]: image = cv2.imread('test_image.jpg')
In[6]: cv2.imshow('Original Image', image)
In[7]: cv2.waitKey()
In[8]: cv2.destroyAllWindows()
The input image looks like this:
Fig 4.55: Input image
- The height and width of the image are as follows:
In[9]: height, width = image.shape[:2]...
In[10]: height
579
In[11]: width
530