11 Jul 2023

Building a Basic Image Processing Tool with Python and OpenCV

In the field of computer vision, image processing plays a vital role in analyzing and manipulating digital images. OpenCV (Open Source Computer Vision) is a powerful library that provides various functions and algorithms for image processing tasks. In this blog, we will explore how to build a basic image processing tool using Python and OpenCV. By the end of this tutorial, you will be able to perform essential operations like image resizing, cropping, rotating, and applying filters to images.


Prerequisites

Before we dive into the implementation, make sure you have the following prerequisites installed on your system:

Setting up the Environment

To begin, open your favorite Python IDE (Integrated Development Environment) or a text editor to write your Python code. Ensure that you have the necessary packages installed by executing the following command in your terminal or command prompt:

pip install opencv-python numpy

Once the installation is complete, we can start building our image processing tool.

Importing the Required Libraries

Let's start by importing the necessary libraries for our project:

import cv2
import numpy as np

Loading and Displaying an Image

To work with images, we need to load them into our program. Assuming you have an image file in the same directory as your Python script, we can load the image using the  cv2.imread()  function. Let's write the code to load and display an image:

image_path = 'path_to_your_image.jpg'  # Replace with the actual path to your image
image = cv2.imread(image_path)

cv2.imshow('Original Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Make sure to replace 'path_to_your_image.jpg'  with the actual path to your image file. The  cv2.imshow()  function is used to display the image, and cv2.waitKey(0)  waits until a key is pressed to close the image window.

Resizing an Image

Resizing an image is a common operation in image processing. It allows us to scale an image to a desired width and height. Here's how we can resize an image:

resized_image = cv2.resize(image, (new_width, new_height))

cv2.imshow('Resized Image', resized_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Replace  new_width  and  new_height with the desired dimensions for your resized image.

Cropping an Image

Cropping an image involves selecting a region of interest (ROI) and extracting only that portion from the original image. Let's see how to crop an image:

x, y, width, height = 100, 100, 300, 200  # Define the region of interest (ROI)

cropped_image = image[y:y+height, x:x+width]

cv2.imshow('Cropped Image', cropped_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Modify the values of xwidth , and  height  to define the ROI according to your requirements.

Rotating an Image

Rotating an image allows us to change its orientation. Here's an example of how to rotate an image by a specified angle:

angle = 45  # Specify the rotation angle

rows, cols = image.shape[:2]
rotation_matrix = cv2.getRotationMatrix2D((cols / 2, rows / 2), angle, 1)
rotated_image = cv2.warpAffine(image, rotation_matrix, (cols, rows))

cv2.imshow('Rotated Image', rotated_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Adjust the value of  angle  to the desired rotation angle in degrees.

Applying Filters to an Image

Filters are widely used in image processing to enhance or modify images. Let's apply a grayscale filter and a Gaussian blur filter to our image:

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)  # Convert image to grayscale

blurred_image = cv2.GaussianBlur(image, (kernel_size, kernel_size), 0)  # Apply Gaussian blur

cv2.imshow('Grayscale Image', gray_image)
cv2.imshow('Blurred Image', blurred_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Set the value of kernel_size  to determine the size of the Gaussian kernel used for blurring.

Conclusion

Congratulations! You have successfully built a basic image processing tool using Python and OpenCV. We covered essential operations such as loading and displaying images, resizing, cropping, rotating, and applying filters. Image processing is a vast field with numerous advanced techniques, but this tutorial serves as a solid foundation for your future explorations. Feel free to experiment and expand upon the concepts discussed here to further enhance your image processing skills. Happy coding!

Remember to save your Python script and images in the same directory, or adjust the paths accordingly when loading images.