Image Processing with Python and OpenCV
Muhammad Dawood
On a journey to unlock the potential of data-driven insights. Day Trader | FX & Commodity Markets | Technical Analysis & Risk Management Expert| Researcher | Inquisitive!
Python, OpenCV & NumPy
Today, we embark on a thrilling journey into image processing using OpenCV, a powerful library for computer vision tasks. I will unravel the secrets behind reading, resizing, blurring, and sharpening images.
Reading and Displaying Images
With just a few lines of code, we can read and display an image like a work of art. Take a look at this snippet:
import cv2
# Read an image
image = cv2.imread('bird.jpg')
# Display the image
cv2.imshow('Original Image', image)
cv2.waitKey(0)
In summary, this code snippet is a basic example of reading and displaying an image using OpenCV. It's a foundational step in any image processing pipeline, allowing you to inspect the input image before proceeding with further manipulations.
Resizing Images
Imagine having the power to reshape your visual creations at will. Enter the realm of image resizing! Behold this enchanting code snippet:
import cv2
def resize_image(image, width, height):
# Resize the input image to the specified width and height
resized_image = cv2.resize(image, (width, height))
return resized_image
# Example usage:
# Set the path of the image file
image_path = 'bird.jpg'
# Read the image using OpenCV
image = cv2.imread(image_path)
# Resize the image to a width of 1280 pixels and a height of 960 pixels
resized_image = resize_image(image, 1280, 960)
# Display the resized image in a graphical window titled 'Resized Image'
cv2.imshow('Resized Image', resized_image)
# Wait for a key press to close the window
cv2.waitKey(0)
# Close all OpenCV windows
cv2.destroyAllWindows()
This code demonstrates a simple image resizing process using OpenCV, providing a glimpse into the world of image manipulation in Python.
领英推荐
Blur the image
Sometimes, a touch of mystery is needed. Enter the captivating world of image blurring:
import cv2
def blur_image(image):
# Define a list of kernel sizes for blurring
kernels = [13, 5, 9, 131]
# Iterate over each kernel size
for k in kernels:
# Apply blur to the image using the current kernel size
blurred_image = cv2.blur(image, (k, k))
# Display the blurred image in a window titled with the kernel size
cv2.imshow(str(k), blurred_image)
# Wait for a key press to close the displayed images
cv2.waitKey(0)
# Example usage:
# Set the path of the image file
image_path = 'bird.jpg'
# Read the image using OpenCV
image = cv2.imread(image_path)
# Check if the image was successfully loaded
if image is not None:
# Call the blur_image function with the loaded image
blur_image(image)
else:
# Display an error message if the image is not found
print(f"Error: Image not found at path {image_path}")
This code provides a hands-on demonstration of image blurring using various kernel sizes in OpenCV, offering insights into the versatility of image processing techniques.
Sharpening Images
Now, let's add a touch of sharpness to our artistic endeavours:
import cv2
import numpy as np
def sharpen_image(image):
# Define a sharpening kernel
kernel = np.array([[0, -1, 0],
[-1, 5, -1],
[0, -1, 0]])
# Apply the sharpening kernel to the image using filter2D
sharpened_image = cv2.filter2D(image, -1, kernel)
# Display the sharpened image in a window titled 'Sharpened Image'
cv2.imshow('Sharpened Image', sharpened_image)
# Wait for a key press to close the displayed image
cv2.waitKey(0)
# Example usage:
# Set the path of the image file
image_path = 'bird.jpg'
# Read the image using OpenCV
image = cv2.imread(image_path)
# Check if the image was successfully loaded
if image is not None:
# Call the sharpen_image function with the loaded image
sharpen_image(image)
else:
# Display an error message if the image is not found
print(f"Error: Image not found at path {image_path}")
This code provides a practical illustration of image sharpening using a specified kernel in OpenCV, demonstrating the transformative power of image processing techniques.