To perform image transformation such as Translation, Scaling, Shearing, Reflection, Rotation and Cropping using OpenCV and Python.
Anaconda - Python 3.7
Import the required libraries and read the original image.
Translate the image.
Scale the image.
Shear the image.
Find reflection of image.
Rotate the image.
Crop the image.
Display all the Transformed images.
Developed By: Syed Abdul Wasih H
Register Number:21221240057
import numpy as np
import cv2
import matplotlib.pyplot as plt
img=cv2.imread("1.jpeg")
img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
plt.axis('off')
plt.imshow(img)
plt.show()
rows,cols,dim=img.shape
M=np.float32([[1,0,20],
[0,1,50],
[0,0,1]])
translated_img=cv2.warpPerspective(img,M,(cols,rows))
plt.axis('off')
plt.imshow(translated_img)
plt.show()
M=np.float32([[1.5,0,0],
[0,2,0],
[0,0,1]])
scaled_img=cv2.warpPerspective(img,M,(cols,rows))
plt.axis('off')
plt.imshow(scaled_img)
plt.show()
M_x=np.float32([[1,0.2,0],
[0,1,0],
[0,0,1]])
M_y=np.float32([[1,0,0],
[0.4,1,0],
[0,0,1]])
sheared_img_xaxis=cv2.warpPerspective(img,M_x,(cols,rows))
sheared_img_yaxis=cv2.warpPerspective(img,M_y,(cols,rows))
plt.axis('off')
plt.imshow(sheared_img_xaxis)
plt.show()
plt.axis('off')
plt.imshow(sheared_img_yaxis)
plt.show()
M_x=np.float32([[1,0,0],
[0,-1,rows],
[0,0,1]])
M_y=np.float32([[-1,0,cols],
[0,1,0],
[0,0,1]])
reflected_img_xaxis=cv2.warpPerspective(img,M_x,(cols,rows))
reflected_img_yaxis=cv2.warpPerspective(img,M_y,(cols,rows))
plt.axis('off')
plt.imshow(reflected_img_xaxis)
plt.show()
plt.axis('off')
plt.imshow(reflected_img_yaxis)
plt.show()
angle=np.radians(45)
M=np.float32([[np.cos(angle),-(np.sin(angle)),0],
[np.sin(angle),np.cos(angle),0],
[0,0,1]])
rotated_img=cv2.warpPerspective(img,M,(cols,rows))
plt.axis('off')
plt.imshow(rotated_img)
plt.show()
cropped_img=img[20:150,60:230]
plt.axis('off')
plt.imshow(cropped_img)
plt.show()
Thus the different image transformations such as Translation, Scaling, Shearing, Reflection, Rotation and Cropping are done using OpenCV and python programming.