Akshay Gupta — Published On April 30, 2021 and Last Modified On August 8th, 2022
This article was published as a part of the Data Science Blogathon.

## Introduction

As indicated by IDC, digital information will soar up to 175 zettabytes, and the immense piece of this information pictures. Data scientists need to (pre) measure these images before taking care of them into any Artificial Intelligence and deep learning models. They need to do the significant (and at times filthy) work before the pleasant part starts.

To handle a lot of information with effectiveness and speed without bargaining the outcomes Data scientists need to utilize picture preparing instruments for Artificial Intelligence and deep learning tasks.

In this article, I will drill down the most helpful image processing libraries in Python which are being utilized vigorously in Artificial intelligence and deep learning tasks. So let’s get started!

1. OpenCV
2. Scikit-Image
3. Scipy
4. Python Image Library (Pillow/PIL)
5. Matplotlib
6. SimpleITK
7. Numpy
8. Mahotas

## OpenCV

OpenCV is one of the most famous and widely used open-source libraries for computer vision tasks such as image processing, object detection, face detection, image segmentation, face recognition, and many more. Other than this, it can also be used for machine learning tasks. This is developed by Intel in 2002. It is written in C++ but developers have provided Python and Java bindings. It is easy to read and use.

To build computer vision and machine learning models, OpenCV has more than 2500+ algorithms. These algorithms are very much useful to perform various tasks such as face recognition, object detection, and many more. Let’s see some examples where we can perform using OpenCV:

### Gray Scaling

Gray-scaling is a method of converting a 3 channel image eg, RGB, HSV, etc into a single channel image i.e to shades of grey. The final image varies between complete white and black. The importance of Gray-Scaling includes Dimension reduction (converting 3 channels to a single-channel image), Reduce model complexity, etc.

#### Rotate Image

OpenCV helps use to rotate the image by any degree ranges from 0 to 360 degrees.

#### Check the below code to rotate the image by 180 degrees.

```import cv2 as cv
import matplotlib.pyplot as plt
h, w = image.shape[:2]
rot_matrix = cv.getRotationMatrix2D((w/2,h/2), -180, 0.5)
rot_image = cv.warpAffine(img, rot_matrix, (w, h))
plt.imshow(cv.cvtColor(rot_image, cv.COLOR_BGR2RGB))```

OpenCV provides other functionalities as well other than what we have discussed so far. Apart from this, it also helps in Face Detection, Image Segmentation, Feature Extraction, Object Detection, 3-D Reconstruction, and many more.

## Scikit-Image

Scikit-Image is another great open-source image processing library. It is useful in almost any computer vision task. It is among one of the most simple and straightforward libraries. Some parts of this library are written in Cython ( It is a superset of python programming language designed to make python faster as C language). It provides a large number of algorithms which include segmentation, color space manipulation, geometric transformation, filtering, morphology, feature detection, and many more.
Scikit Image uses Numpy arrays as image objects. Let’s see how can we perform active contour operation in the scikit image. Active contour describes the boundaries of shapes in an image.

#### Check the below code for active contour operation:

```import numpy as np
import matplotlib.pyplot as plt
from skimage.color import rgb2gray
from skimage import data
from skimage.filters import gaussian
from skimage.segmentation import active_contour
image = data.astronaut()
# Data for circular boundary
s = np.linspace(0, 2*np.pi, 400)
x = 220 + 100*np.cos(s)
y = 100 + 100*np.sin(s)
init = np.array([x, y]).T
# formation of the active contour
centre = active_contour(gaussian(image, 3),init, alpha=0.015, beta=10, gamma=0.001)
figure, axis = plt.subplots(1, 2, figsize=(7, 7))
ax.imshow(image, cmap=plt.cm.gray)
ax.set_title("Original Image")
ax.imshow(image, cmap=plt.cm.gray)```

## SciPy

SciPy is primarily used for mathematical and scientific computations, but sometimes it can also be used for basic image manipulation and processing tasks using the submodule scipy.ndimage.At the end of the day, images are just multidimensional arrays, SciPy provides a set of functions that are used to operate n-dimensional Numpy operations. SciPy provides some basic image processing operations such as Face Detection, Convolution, Image Segmentation, Reading Images, Feature Extraction, and many more. Along with this, you also perform filtering, draw contour lines on images.

Check the below code for Blurring an image with SciPy:

```from scipy import ndimage, misc
from matplotlib import pyplot as plt
f = misc.face()
b_face = ndimage.gaussian_filter(f, sigma=3)
figure, axis = plt.subplots(1, 2, figsize=(16, 8))```

## Python Image Library (PIL/Pillow)

It is an open-source python library that is used for image processing tasks. It provides special functionalities which are generally not provided by other libraries such as filtering, opening, manipulating, and saving images. This library supports a wide range of file formats, which makes it more efficient. PIL also supports functions such as Image processing, Image Display, and Image Archives. Let’s see Image Enhancement using PIL/Pillow.

## Matplotlib

Matplotlib is primarily used for 2D visualizations such as scatter plots, bar graphs, histograms, and many more, but we can also use it for image processing. It is effective to get information out of an image. It doesn’t support all file formats.

Check the below image after background color change operation:

## SimpleITK

It is also called Insight Segmentation and Registration Toolkit. It is an open-source library that is used for Image Registration and Image Segmentation. Libraries like OpenCV consider the image as an array but this library considers images as a set of points on a region in space. Check the below example:

Image Segmentation

## Numpy

It is an open-source python library that is used for numerical analysis. It contains a matrix and multi-dimensional arrays as data structures. But NumPy can also use for image processing tasks such as image cropping, manipulating pixels, and masking of pixel values.

Check the below image to extract green/red/blue channels from the image:

## Mahotas

It is another open-source python library for computer vision and image processing. It was designed for biometric informatics. It provides many algorithms which are written in C++ for speed with a good python interface. It read and writes images in NumPy arrays.

Check the below image for Template Matching using Mahotas:

## Conclusion

So in this article, we have covered the top 8 image processing libraries in python for machine learning in 2021. I hope you learn something from this blog and it will turn out best for your project. Thanks for reading and your patience. Good luck!

You can check my articles here: Articles 