Uploaded by sferkaese

CV Python Codes - Image Processing

advertisement
Geometric Transformations
1.
Translation:
import cv2
import numpy as np
# Load the image
image = cv2.imread('input_image.jpg')
# Define the translation matrix
tx = 50 # Translation in the x-direction
ty = 30 # Translation in the y-direction
translation_matrix = np.float32([[1, 0, tx], [0, 1, ty]])
# Perform the translation using the warpAffine function
translated_image = cv2.warpAffine(image, translation_matrix, (image.shape[1],
image.shape[0]))
# Display the original and translated images
cv2.imshow('Original Image', image)
cv2.imshow('Translated Image', translated_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
2.
Scaling
import cv2
# Load the image
image = cv2.imread('input_image.jpg')
# Define the target width in pixels and height in pixels
target_width = 800
target_height = 600
# Resize the image using cv2.resize()
resized_image = cv2.resize(image, (target_width, target_height),
interpolation=cv2.INTER_LINEAR)
#Interpolation gives the smoothness of the image, while resizing (Bilinear
Interpolation)
# Display the original and resized images
cv2.imshow('Original Image', image)
cv2.imshow('Resized Image', resized_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
3.
Rotation:
import cv2
# Load the image
image = cv2.imread('input_image.jpg')
# Define the rotation angle in degrees
angle = 45
# Get the center of the image
height, width = image.shape[:2]
#[:2] means slicing the first two elements like height and width
center = (width / 2, height / 2)
# Create the rotation matrix
rotation_matrix = cv2.getRotationMatrix2D(center, angle, 1.0)
# A scale value of 1.0 means no scaling will be applied; the image will retain
its original size after rotation.
# Apply the rotation using cv2.warpAffine()
rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height))
# Display the original and rotated images
cv2.imshow('Original Image', image)
cv2.imshow('Rotated Image', rotated_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
4.
Shearing:
import cv2
import numpy as np
# Load the image
image = cv2.imread('input_image.jpg')
# Define the shearing factor
shear_factor = 0.3 # Adjust this value to control the shearing amount
#if shear_factor is set to 0.3, it means that each row will be displaced to the
right by 30% of its height. Similarly, if you use a negative shear_factor of
-0.3, each row will be displaced to the left by 30% of its height.
# Define the shearing matrix
shear_matrix = np.array([[1, shear_factor, 0],
[0, 1, 0]], dtype=np.float32)
# Apply the shearing using cv2.warpAffine()
sheared_image = cv2.warpAffine(image, shear_matrix, (image.shape[1],
image.shape[0]))
# Display the original and sheared images
cv2.imshow('Original Image', image)
cv2.imshow('Sheared Image', sheared_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
5.
Sobel Filter
import cv2
# Load the image in grayscale
image = cv2.imread('input_image.jpg', cv2.IMREAD_GRAYSCALE)
# Apply Sobel operator to get gradients in the x and y directions
sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)
sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3)
# Convert the gradients back to uint8 format
sobel_x = cv2.convertScaleAbs(sobel_x)
sobel_y = cv2.convertScaleAbs(sobel_y)
# Combine the gradients using the magnitude formula
gradient_magnitude = cv2.addWeighted(sobel_x, 0.5, sobel_y, 0.5, 0)
# Display the original image and gradient magnitude
cv2.imshow('Original Image', image)
cv2.imshow('Gradient Magnitude', gradient_magnitude)
cv2.waitKey(0)
cv2.destroyAllWindows()
6.
7.
Canny Filter:
import cv2
# Load the image in grayscale
image = cv2.imread('input_image.jpg', cv2.IMREAD_GRAYSCALE)
# Apply Canny edge detection
canny_edges = cv2.Canny(image, threshold1=100, threshold2=200)
# Display the original image and Canny edge detection results
cv2.imshow('Original Image', image)
cv2.imshow('Canny Edges', canny_edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
Download