Image cropping is a type of image transformation. In this section, we will crop an image using OpenCV:
- Firstly, we will 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[6]: image = cv2.imread('Test_auto_image.jpg')
In[7]: cv2.imshow('Original Image', image)
In[8]: cv2.waitKey()
In[9]: cv2.destroyAllWindows()
The input image is as follows:
Fig 4.66: Input image
The height and width of the image are as follows:
In[10]: height, width = image.shape[:2]
In[11]: height
800
In[12]: width
1200
- Next, we perform cropping with the following code. The top-left coordinates of the desired cropped area are w0 and h0:
In[13]: w0 = int(width * 0.5)
In[14]: h0 = int(height * 0.5)
- The bottom-right coordinates...