import numpy as np
import cv2
import matplotlib.pyplot as plt
Reading Image
# read the input image
= cv2.imread("../images/trees.jpg")
img # convert from BGR to RGB so we can plot using matplotlib
= cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img # disable x & y axis
'off')
plt.axis(# show the image
plt.imshow(img) plt.show()
Translation of image
# get the image shape
print(img.shape)
= img.shape
rows, cols, dim # transformation matrix for translation
= np.float32([[1, 0, 500],
M 0, 1, 500],
[0, 0, 1]])
[# apply a perspective transformation to the image
= cv2.warpPerspective(img, M, (cols, rows))
translated_img # disable x & y axis
'off')
plt.axis(# show the resulting image
plt.imshow(translated_img) plt.show()
(1606, 2222, 3)
Scaling of Image
# get the image shape
print(img.shape)
= img.shape
rows, cols, dim #transformation matrix for Scaling
= np.float32([[1.5, 0 , 0],
M 0, 1.8, 0],
[0, 0, 1]])
[# apply a perspective transformation to the image
= cv2.warpPerspective(img,M,(cols,rows))
scaled_img # disable x & y axis
'off')
plt.axis(# show the resulting image
plt.imshow(scaled_img) plt.show()
(1606, 2222, 3)
Shearing of Image
# get the image shape
print(img.shape)
= img.shape
rows, cols, dim # transformation matrix for Shearing
# shearing applied to x-axis
= np.float32([[1, 0.5, 0],
M 0, 1 , 0],
[0, 0 , 1]])
[# shearing applied to y-axis
# M = np.float32([[1, 0, 0],
# [0.5, 1, 0],
# [0, 0, 1]])
# apply a perspective transformation to the image
= cv2.warpPerspective(img,M,(cols,rows))
sheared_img # disable x & y axis
'off')
plt.axis(# show the resulting image
plt.imshow(sheared_img) plt.show()
(1606, 2222, 3)
Reflection of Image
# get the image shape
= img.shape
rows, cols, dim # transformation matrix for x-axis reflection
= np.float32([[1, 0, 0 ],
M1 0, -1, rows],
[0, 0, 1 ]])
[# transformation matrix for y-axis reflection
= np.float32([[-1, 0, cols],
M2 0, 1, 0 ],
[ 0, 0, 1 ]])
[ # apply a perspective transformation to the image
= cv2.warpPerspective(img,M1,(cols,rows))
h_reflected_img = cv2.warpPerspective(img,M2,(cols,rows))
v_reflected_img # disable x & y axis
'off')
plt.axis(# show the resulting image
=np.hstack((h_reflected_img,v_reflected_img))
combined
'X-axis reflection and Y-axis reflection')
plt.title(
plt.imshow(combined) plt.show()
Rotation of Image
# get the image shape
= img.shape
rows, cols, dim #angle from degree to radian
= np.radians(10)
angle #transformation matrix for Rotation
= np.float32([[np.cos(angle), -(np.sin(angle)), 0],
M 0],
[np.sin(angle), np.cos(angle), 0, 0, 1]])
[# apply a perspective transformation to the image
= cv2.warpPerspective(img, M, (int(cols),int(rows)))
rotated_img # disable x & y axis
'off')
plt.axis(# show the resulting image
plt.imshow(rotated_img) plt.show()
Cropping of Image
# get 200 pixels from 100 to 300 on both x-axis & y-axis
# change that if you will, just make sure you don't exceed cols & rows
= img[100:300, 100:300]
cropped_img # disable x & y axis
'off')
plt.axis(# show the resulting image
plt.imshow(cropped_img) plt.show()
Center Crop of Image
# get the image width & height
= img.shape[1], img.shape[0]
width, height
# get the image cropped width & height
= 500, 600
crop_width , crop_height
= int(crop_width/2) , int(crop_height/2)
half_crop_width , half_crop_height
= int(width/2), int(height/2)
mid_x, mid_y
=img[mid_y-half_crop_height : mid_y+half_crop_height, mid_x-half_crop_width : mid_x+half_crop_width]
center_cropped_img
# disable x & y axis
'off')
plt.axis(# show the resulting image
plt.imshow(center_cropped_img) plt.show()