Recently, I found myself in a situation where I wanted to perform image processing tasks, only to realize that my software subscription had expired. However, a lightbulb moment occurred when I remembered the powerful capabilities of the OpenCV library in Python. OpenCV provides a comprehensive set of tools and algorithms for image processing, allowing you to easily manipulate and enhance images. This blog post will explore practical examples of leveraging OpenCV to perform various image processing techniques, even without a fancy software subscription.
Setting Up the Environment
Before we dive into the code, make sure you have OpenCV installed on your system. You can install it using the following command:
pip install opencv-python
import cv2 import os # Specify the path to the original image image_path = 'sqlimage.jpg' # Create a folder to store the processed images output_folder = 'processed_images' os.makedirs(output_folder, exist_ok=True) # Load the image image = cv2.imread(image_path) # Example 1: Extract the red channel red_channel = image.copy() red_channel[:, :, 0] = 0 # Set blue channel to 0 red_channel[:, :, 1] = 0 # Set green channel to 0 # Save the red channel image cv2.imwrite(os.path.join(output_folder, 'red_channel.jpg'), red_channel) # Example 2: Convert the image to grayscale gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Save the grayscale image cv2.imwrite(os.path.join(output_folder, 'gray_image.jpg'), gray_image) # Example 3: Apply a Gaussian blur blurred_image = cv2.GaussianBlur(image, (15, 15), 0) # Save the blurred image cv2.imwrite(os.path.join(output_folder, 'blurred_image.jpg'), blurred_image) # Example 4: Perform edge detection edges = cv2.Canny(gray_image, 100, 200) # Save the edge image cv2.imwrite(os.path.join(output_folder, 'edges.jpg'), edges) # Example 5: Apply color inversion inverted_image = cv2.bitwise_not(image) # Save the inverted image cv2.imwrite(os.path.join(output_folder, 'inverted_image.jpg'), inverted_image)
Code Walkthrough
Let’s walk through the code to understand how each image processing technique is implemented.
- Loading the Image:
We start by specifying the path to the original image and creating a folder to store the processed images.
- Example 1: Extracting the Red Channel:
In this example, we extract the red channel from the original image. We create a copy of the image and set the blue and green channels to 0, effectively isolating the red channel. The resulting image is then saved as “red_channel.jpg” in the output folder.
- Example 2: Converting to Grayscale:
Here, we convert the original image to grayscale using theÂcv2.cvtColor()
 function. The grayscale image reduces the image to a single channel, representing the intensity of each pixel. The grayscale image is saved as “gray_image.jpg” in the output folder.
- Example 3: Applying Gaussian Blur:
Gaussian blur is a common technique used to reduce noise and smooth out an image. We apply a Gaussian blur filter to the original image usingÂcv2.GaussianBlur()
. The result is a blurred image with reduced high-frequency details. The blurred image is saved as “blurred_image.jpg” in the output folder.
- Example 4: Performing Edge Detection:
Edge detection is a fundamental step in image analysis and computer vision. We perform edge detection on the grayscale image obtained in Example 2 usingÂcv2.Canny()
. The Canny edge detection algorithm detects sharp intensity transitions in the image, highlighting the edges. The resulting edge image is saved as “edges.jpg” in the output folder.
- Example 5: Applying Color Inversion:
Color inversion is a simple yet effective technique to create visual contrast. We invert the colors of the original image by applying bitwise NOT usingÂcv2.bitwise_not()
. The inverted image is saved as “inverted_image.jpg” in the output folder.
Well, I hope you enjoyed this offbeat blog post! You can follow me on  X (twitter) and YouTube.
Reference:Â Pinal Dave (https://blog.sqlauthority.com)