In this section, we will learn about image translation. Image translation involves shifting an object's position in the x and/or y direction. OpenCV uses a translational matrix, T, as follows:
Now, we will perform image translation:
- 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
- Then we read in the input image:
In[6]: image = cv2.imread('test_image.jpg')
In[7]: cv2.imshow('Original Image', image)
In[8]: cv2.waitKey()
In[9]: cv2.destroyAllWindows()
The input image looks like this:
Fig 4.57: Input image
- The height and width of the image are as follows:
In[10]: height, width = image.shape[:2]
In[11]: height
579
In[12]: width
530
The translation matrix is defined as follows:
In[13]: Translational_Matrix = np.float32([[1, 0, 120],
...