To perform image transformation such as Translation, Scaling, Shearing, Reflection, Rotation and Cropping using OpenCV and Python.
Anaconda - Python 3.7
# Install OpenCV library
!pip install opencv-python-headless
import cv2
import numpy as np
from matplotlib import pyplot as plt
# Function to display images
def show_image(image):
plt.figure(figsize=(6, 6))
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.show()
# Load an image from URL or file path
image_url = '1.jpg'
image = cv2.imread(image_url)
# Define translation matrix
tx = 50 # Translation along x-axis
ty = 30 # Translation along y-axis
translation_matrix = np.float32([[1, 0, tx], [0, 1, ty]]) # Create translation matrix
# Apply translation to the image
translated_image = cv2.warpAffine(image, translation_matrix, (image.shape[1], image.shape[0]))
# Display original and translated images
print("Original Image:")
show_image(image)
print("Translated Image:")
show_image(translated_image)
# Load an image from URL or file path
image_url = '2.jpg' # Replace with your image URL or file path
image = cv2.imread(image_url)
# Define scale factors
scale_x = 1.5 # Scaling factor along x-axis
scale_y = 1.5 # Scaling factor along y-axis
# Apply scaling to the image
scaled_image = cv2.resize(image, None, fx=scale_x, fy=scale_y, interpolation=cv2.INTER_LINEAR)
# Display original and scaled images
print("Original Image:")
show_image(image)
print("Scaled Image:")
show_image(scaled_image)
# Load an image from URL or file path
image_url = '3.jpg' # Replace with your image URL or file path
image = cv2.imread(image_url)
# Define shear parameters
shear_factor_x = 0.5 # Shear factor along x-axis
shear_factor_y = 0.2 # Shear factor along y-axis
# Define shear matrix
shear_matrix = np.float32([[1, shear_factor_x, 0], [shear_factor_y, 1, 0]])
# Apply shear to the image
sheared_image = cv2.warpAffine(image, shear_matrix, (image.shape[1], image.shape[0]))
# Display original and sheared images
print("Original Image:")
show_image(image)
print("Sheared Image:")
show_image(sheared_image)
# Load an image from URL or file path
image_url = '4.jpg' # Replace with your image URL or file path
image = cv2.imread(image_url)
# Reflect the image horizontally
reflected_image_horizontal = cv2.flip(image, 1)
# Reflect the image vertically
reflected_image_vertical = cv2.flip(image, 0)
# Reflect the image both horizontally and vertically
reflected_image_both = cv2.flip(image, -1)
# Display original and reflected images
show_image(image)
print("↑ Original Image")
show_image(reflected_image_horizontal)
print("↑ Reflected Horizontally")
show_image(image)
print("↑ Original Image")
show_image(reflected_image_vertical)
print("↑ Reflected Vertically")
show_image(image)
print("↑ Original Image")
show_image(reflected_image_both)
print("↑ Reflected Both")
# Load an image from URL or file path
image_url = '5.jpg' # Replace with your image URL or file path
image = cv2.imread(image_url)
# Define rotation angle in degrees
angle = 45
# Get image height and width
height, width = image.shape[:2]
# Calculate rotation matrix
rotation_matrix = cv2.getRotationMatrix2D((width / 2, height / 2), angle, 1)
# Perform image rotation
rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height))
# Display original and rotated images
print("Original Image:")
show_image(image)
print("Rotated Image:")
show_image(rotated_image)
# Load an image from URL or file path
image_url = '6.jpg' # Replace with your image URL or file path
image = cv2.imread(image_url)
# Define cropping coordinates (x, y, width, height)
x = 100 # Starting x-coordinate
y = 50 # Starting y-coordinate
width = 200 # Width of the cropped region
height = 150 # Height of the cropped region
# Perform image cropping
cropped_image = image[y:y+height, x:x+width]
# Display original and cropped images
print("Original Image:")
show_image(image)
print("Cropped Image:")
show_image(cropped_image)