25 Jun 2023

Python Image Processing with Opencv and Libraries

Python is a popular programming language that has gained widespread acceptance for its simplicity, flexibility, and versatility. It is used extensively for data analysis, machine learning, web development, and image processing. Image processing is a field that has seen tremendous growth in recent years, with numerous libraries and tools available to developers. In this blog, we will explore how to use Python for image processing using the OpenCV library and other libraries.

OpenCV (Open Source Computer Vision Library) is a popular library for computer vision applications. It is used for tasks such as object recognition, image enhancement, and machine learning. OpenCV was first developed in 1999 by Intel and has since been used in a wide variety of applications.

Image Processing with OpenCV

OpenCV is a powerful library for image processing in Python. It provides a wide range of functions for image manipulation, including image filtering, transformation, and segmentation. Here is a step-by-step guide to using OpenCV for image processing in Python:

Install OpenCV

The first step is to install OpenCV. OpenCV can be installed using pip, a Python package manager. Open a terminal window and enter the following command to install OpenCV:

pip install opencv-python

Load an Image

Once OpenCV is installed, you can load an image into Python using the imread function. This function takes the file name of the image as an argument and returns an array of pixel values.

import cv2
image = cv2.imread('image.jpg')

Display the Image

You can display the image using the imshow function. This function takes the name of the window and the image array as arguments.

cv2.imshow('image', image)
cv2.waitKey(0)

The waitKey function is used to wait for a key press. When the user presses a key, the window is closed.

Convert to Grayscale

To convert the image to grayscale, you can use the cvtColor function. This function takes the image array and the color conversion code as arguments.

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

Apply Filters

OpenCV provides a wide range of filters that can be applied to images. Some of the most commonly used filters include Gaussian blur, median blur, and edge detection. Here is an example of how to apply a Gaussian blur filter to an image:

blurred_image = cv2.GaussianBlur(image, (5, 5), 0)

Image Thresholding

Thresholding is a technique used to segment an image into different regions based on their pixel values. It is commonly used to detect edges in an image. OpenCV provides several thresholding functions, including binary thresholding, adaptive thresholding, and Otsu's thresholding. Here is an example of how to perform binary thresholding on an image:

ret, thresh_image = cv2.threshold(gray_image, 127, 255, cv2.THRESH_BINARY)

Image Segmentation

Image segmentation is the process of dividing an image into multiple segments or regions based on certain criteria, such as color, texture, or brightness. OpenCV provides several segmentation functions, including watershed segmentation and region growing. Here is an example of how to perform watershed segmentation on an image:

import numpy as np
from matplotlib import pyplot as plt

gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

# noise removal
kernel = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)

# sure background area
sure_bg = cv2.dilate(opening,kernel,iterations=3)

# sure foreground area
dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)
ret, sure_fg = cv2.threshold(dist_transform,0.7*dist_transform.max(),255,0)

# unknown region
sure_fg = np.uint8(sure_fg)
unknown = cv2.subtract(sure_bg,sure_fg)

# marker labelling
ret, markers = cv2.connectedComponents(sure_fg)

# add one to all labels so that sure background is not 0, but 1
markers = markers+1

# Now, mark the region of unknown with zero
markers[unknown==255] = 0

markers = cv2.watershed(image,markers)
image[markers == -1] = [255,0,0]

plt.imshow(image)
plt.show()

Other Libraries for Image Processing in Python

In addition to OpenCV, there are several other libraries available for image processing in Python. Some of the most commonly used libraries include:

Pillow

Pillow is a fork of the Python Imaging Library (PIL) that provides image processing capabilities. It supports a wide range of image formats and provides functions for image filtering, enhancement, and manipulation.

scikit-image

scikit-image is an image processing library that provides a wide range of functions for image segmentation, filtering, and feature extraction. It is built on top of NumPy and SciPy and is designed to be easy to use and extend.

NumPy

NumPy is a popular library for scientific computing in Python. It provides functions for working with arrays and matrices, which are commonly used in image processing. NumPy is often used in combination with other image processing libraries, such as OpenCV and scikit-image.

Conclusion

Python provides a wide range of tools and libraries for image processing. OpenCV is a powerful library for computer vision applications that provides a wide range of functions for image manipulation. Other libraries, such as Pillow, scikit-image, and NumPy, provide additional functionality for image processing. Whether you are working on a simple image manipulation task or a complex computer vision application, Python has the tools and libraries you need to get the job done.