top of page

Role of OpenCV in Image Preprocessing

Open CV is a huge open-source library for Computer Vision, Machine Learning and Image Processing. It focuses on image processing, video capture and analysis including face-detection and object detection. It can identify faces, objects or even the hand-writing of a human.

When it is integrated with NumPy (highly optimized library for numerical operations), whatever operations one can do in NumPy can be combined with OpenCV.

OpenCV supports a wide variety of programming languages such as C++, Python, Java etc. and is available on different platforms. It runs on both desktop (Windows, Linux, Android, MacOS, FreeBSD, OpenBSD) and mobile (Android, Maemo, iOS).

It is a cross-platform library.

Computer Vision

Computer Vision is the way of teaching intelligence to machines and making them see things just like humans. In its simplest form, Computer Vision is what allows computer to see and process visual data just like humans. It involves analyzing images and videos to produce useful information.


  A few of its applications are given below.

  • Face Detection

  • Cancer Detection

  • COVID-19 diagnosis

  • Movement Analysis

  • Mask detection

  • Traffic Flow Analysis

  • Driver Attentiveness Detection

  • Customer Tracking

  • Theft Detection

Image processing is the most basic and one of the important concepts in Computer Vision. Just as we do data preprocessing techniques in Machine Learning to make our data ready for easy computation, images should also go through the same preprocessing techniques.

Image Preprocessing:

In order to add a dataset of images to a convolutional network, all of the images should be of same size. Preprocessing includes image resizing, geometric and color transformations, gray-scale conversion, background removal, noise removal, edge detection and many more.

The acquired data is usually messy and come from different sources. To feed them to the ML model (or neural network), they need to be standardized and cleaned up. This can be done with the help of OpenCV. Preprocessing is used to conduct steps that reduce the complexity and increase the accuracy of the applied algorithm. We cannot write a unique algorithm for each of the condition in which an image is taken, thus, when we acquire an image, we convert it into a form that allows a general algorithm to solve it. This is the importance of image preprocessing.

Let’s take an example of an Image Classification problem.

Consider the image given below:


You can easily recognize it. It’s a car. When you were shown an image, you classified it to a class it belonged to (car, in this instance). This is what an image classification is all about. When you are given a dataset containing a massive number of images (say 10,000 or 1,00,000), manually checking and classifying is a tedious task. Here we have to build an image classification model.

Self-driving car is a great example where image classification is used in real world. To enable autonomous driving, we have to build an image classification model to recognize various objects like vehicles, people, obstacles, moving objects etc. on the road. So, the entire problem involves 4 steps;

  1. Loading and preprocessing data

  2. Defining model architecture

  3. Training the model

  4. Estimation of performance

Now, let us have a look at the role of OpenCV in Image Preprocessing techniques. This is used in cases when you want to analyze the images manually before feeding them into the training model.

1. Converting to Gray scale image

Gray-scale images reduces the complexity of computation as it removes all the unwanted information from the image. The code for converting an image to gray-scale is given below:


2. Edge Detection

Edge detection is done using the same image given above


Canny edge detection is used to detect the edges in an image. It accepts a gray scale image as input and it uses a multistage algorithm which involves noise reduction, finding the edge gradient and direction, non-maximum suppression to remove any unwanted pixels which may not constitute the edge and Hysteresis thresholding to decide which all edges are really edges and which are not based on two threshold values ‘minVal’ and ‘maxVal’.

3. Noise Removal


4. Image Contouring


5. Blurring and Smoothing

It is done to find features or to remove noise from features. There are many methods available in OpenCV for performing blurring and smoothing. It includes:

  • Gaussian Blurring

  • Median Blurring

  • Bilateral Blurring

  • Gamma Correction

  • Using built-in kernels

  • Using user-defined kernels

How it works?

Generally, there will be kernel (kernels are nothing but array of numbers) for performing different kinds of operations and we will be multiplying them with the normal image and taking the sum of that and consider that as the pixel value.

This is the kernel for blurring:


Kernel for sharpening the image:


Gaussian Blurring:

It is a type of blurring technique which helps to reduce the Gaussian noise and create blur. We are using a gaussian filter which convolves around the image and do the multiplication and summation task.


6. Line Detection

This is done to detect the lines in images. For that, we use cv2.HoughLines(P) for doing that along with creating a mask through which we create the edges using Canny edge detector and then apply that output in the Hough line prediction function.


Now, we are applying Gaussian blur function to this image.


Then, we create a mask to segment the lines in the image and create the edges using Canny edge detector. The output of this function is applied to the Hough Line prediction function to find out the lines in the image.


This is how the output looks like.

These are few of the image processing techniques which can be done using OpenCV. Other techniques include:

  • Removing background from the image

  • Color Detection

  • Extracting text from image

  • Rotate an image

  • Adjust image contrast

  • Crop an image

  • Apply mask for colored image

  • Centroid detection, etc.

Image processing allows a much wider range of algorithms to be applied to input data. The aim of image processing

is to improve the image data (features) by suppressing unwanted distortions and enhancement of some important image features so that our training models can benefit from this improved data to work on which in turn improves the performance

of the model.

BLOG mast.png
bottom of page