To perform image transformation such as Translation, Scaling, Shearing, Reflection, Rotation and Cropping using OpenCV and Python.
Anaconda - Python 3.7
Import numpy module as np and pandas as pd.
Assign the values to variables in the program.
Get the values from the user appropriately.
Continue the program by implementing the codes of required topics.
Thus the program is executed in google colab.
Developed By: KARTHICK P
Register Number:212222100021
import cv2
import numpy as np
from matplotlib import pyplot as plt
def show_image(image):
plt.figure(figsize=(6, 6))
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.show()
image_url = 'rose.jpg'
image = cv2.imread(image_url)
tx = 50
ty = 30
translation_matrix = np.float32([[1, 0, tx], [0, 1, ty]])
translated_image = cv2.warpAffine(image, translation_matrix, (image.shape[1], image.shape[0]))
print("Original Image:")
show_image(image)
print("Translated Image:")
show_image(translated_image)
import numpy as np
import cv2
import matplotlib.pyplot as plt
in_img=cv2.imread("flower1.jpg")
in_img=cv2.cvtColor(in_img,cv2.COLOR_BGR2RGB)
rows,cols,dim=in_img.shape
M=np.float32([[1.5,0 ,0],
[0,1.8,0],
[0,0,1]])
scaled_img=cv2.warpPerspective(in_img, M,(cols,rows))
plt.axis('off')
plt.imshow(scaled_img)
plt.show()
import cv2
import numpy as np
from matplotlib import pyplot as plt
def show_image(image):
plt.figure(figsize=(6, 6))
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.show()
image_url = 'flower3.jpg' # Replace with your image URL or file path
image = cv2.imread(image_url)
shear_factor_x = 0.5
shear_factor_y = 0.2
shear_matrix = np.float32([[1, shear_factor_x, 0], [shear_factor_y, 1, 0]])
sheared_image = cv2.warpAffine(image, shear_matrix, (image.shape[1], image.shape[0]))
print("Original Image:")
show_image(image)
print("Sheared Image:")
show_image(sheared_image)
import cv2
import numpy as np
from matplotlib import pyplot as plt
def show_image(image):
plt.figure(figsize=(6, 6))
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.show()
image_url = 'flower2.jpg'
image = cv2.imread(image_url)
reflected_image_horizontal = cv2.flip(image, 1)
reflected_image_vertical = cv2.flip(image, 0)
reflected_image_both = cv2.flip(image, -1)
print("Original Image:")
show_image(image)
print("Reflected Horizontally:")
show_image(reflected_image_horizontal)
print("Reflected Vertically:")
show_image(reflected_image_vertical)
print("Reflected Both:")
show_image(reflected_image_both)
import cv2
import numpy as np
from matplotlib import pyplot as plt
def show_image(image):
plt.figure(figsize=(6, 6))
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.show()
image_url = 'flower4.jpg' # Replace with your image URL or file path
image = cv2.imread(image_url)
angle = 45
height, width = image.shape[:2]
rotation_matrix = cv2.getRotationMatrix2D((width / 2, height / 2), angle, 1)
rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height))
print("Original Image:")
show_image(image)
print("Rotated Image:")
show_image(rotated_image)
import numpy as np
import cv2
import matplotlib.pyplot as plt
in_img = cv2.imread("flower5.jpg")
in_img = cv2.cvtColor(in_img,cv2.COLOR_BGR2RGB)
plt.imshow(in_img)
plt.show()
cropped_img=in_img[2000:3000 ,1000:2500]
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.