Pravat kumar Nath sharma
10 min readSep 28, 2021

--

Image Processing using python and opencv

In this article, you will learn how to perform basic Image Processing Operations in Python using OpenCV.

Table of Content:

· Introduction to Technologies Used in this Tutorial
·
Installation of OpenCV
Windows
macOS
Linux
·
3 basic Digital Image Processing Using Python and OpenCV With Examples and Code included
· Creating a Custom Image by Using Python Code OpenCV
·
Take 2 images, crop some part of both the images and swap them
·
Take 2 images and combine them to form a single image.
·
Conclusion
·
Github Project Link

Introduction to Image Processing:

In this tutorial, we are going to learn how we can perform image processing using the Python language. We are not going to restrict ourselves to a single library or framework; however, there is one that we will be using the most frequently, the Open CV library.

Many applications use images, and with this, there is usually a need to process the images used. If you are building your application with Python and need to add image processing features to it, there are various libraries you could use. Some popular ones are OpenCV.

We will start by talking a little about image processing and then we will move on to see different applications/scenarios where image processing can come in handy. So, let’s begin!

What is An Image?

An image is defined as a two-dimensional function, F(x,y), where x and y are spatial coordinates, and the amplitude of F at any pair of coordinates (x,y) is called the intensity of that image at that point. When x,y, and amplitude values of F are finite, we call it an image.

In other words, an image can be defined by a two-dimensional array specifically arranged in rows and columns.

Image is composed of a finite number of elements, each of which elements have a particular value at a particular location. These elements are referred to as picture elements, image elements, and pixels. A Pixel is most widely used to denote the elements of an Image.

Types of an images

  1. BINARY IMAGE– The binary image as its name suggests, contains only two-pixel elements i.e 0 & 1, where 0 refers to black and 1 refers to white. This image is also known as Monochrome.
  2. BLACK AND WHITE IMAGE– The image which consists of the only black and white color is called BLACK AND WHITE IMAGE.
  3. 8-bit COLOR FORMAT– It is the most famous image format. It has 256 different shades of colors in it and is commonly known as Grayscale Image. In this format, 0 stands for Black, and 255 stands for white, and 127 stands for gray.
  4. 16-bit COLOR FORMAT– It is a color image format. It has 65,536 different colors in it. It is also known as High Color Format. In this format, the distribution of color is not as same as the Grayscale image.

A 16-bit format is divided into three further formats which are Red, Green, and Blue. That famous RGB format.

Image as a Matrix

As we know, images are represented in rows and columns we have the following syntax in which images are represented:

matrix in images

The right side of this equation is the image by definition. Every element of this matrix is called an image element, picture element, or pixel.

What is Image Processing?

It is important to know what exactly image processing is and what is its role in the bigger picture before diving into its hows. Image Processing is most commonly termed as ‘Digital Image Processing’ and the domain in which it is frequently used is ‘Computer Vision.

Don’t be confused — we are going to talk about both of these terms and how they connect. Both Image Processing algorithms and Computer Vision (CV) algorithms take an image as input; however, in image processing, the output is also an image, whereas in computer vision the output can be some features/information about the image.

Why do we need Image Processing?

The data that we collect or generate is mostly raw, i.e. it is not fit to be used in applications directly due to several possible reasons. Therefore, we need to analyze it first, perform the necessary pre-processing, and then use it.

For instance, let’s assume that we were trying to build a cat classifier. Our program would take an image as input and then tell us whether the image contains a cat or not. The first step for building this classifier would be to collect hundreds of cat pictures.

One common issue is that all the pictures we have scraped would not be of the same size/dimensions, so before feeding them to the model for training, we would need to resize/pre-process them all to a standard size.

This is just one of many reasons why image processing is essential to any computer vision application.

What is OpenCV in Python?

OpenCV library in python

OpenCV-Python is a library of Python bindings designed to solve computer vision problems.

OpenCV (Open Source Computer Vision Library) is a library of programming functions mainly aimed at real-time computer vision. Originally developed by Intel. The library is cross-platform and free for use. OpenCV features GPU acceleration for real-time operations.

OpenCV is written in C++ and its primary interface is in C++, but it still retains a less comprehensive though extensive older C interface. All of the new developments and algorithms appear in the C++ interface. There are bindings in Python, Java, and MATLAB/OCTAVE.

OpenCV-Python makes use of Numpy, which is a highly optimized library for numerical operations with a MATLAB-style syntax. All the OpenCV array structures are converted to and from Numpy arrays. This also makes it easier to integrate with other libraries that use Numpy such as SciPy and Matplot

Prerequisites

Before going any further, let’s discuss what you need to know to follow this tutorial with ease. Firstly, you should have some basic programming knowledge in Python.

Secondly, you should know what machine learning is and the basics of how it works. As a bonus, it would help if you have had any exposure to, or basic knowledge of, Open CV before going on with this tutorial. But this is not required.

One thing you should know to follow this tutorial is how exactly an image is represented in memory. Each image is represented by a set of pixels i.e. a matrix of pixel values.

For a grayscale image, the pixel values range from 0 to 255 and they represent the intensity of that pixel. For instance, if you have an image of 20 x 20 dimensions, it would be represented by a matrix of 20x20 (a total of 400-pixel values).

If you are dealing with a colored image, you should know that it would have three channels — Red, Green, and Blue (RGB). Therefore, there would be three such matrices for a single image.

Installation of Python OpenCV

Note: Since we are going to use OpenCV via Python, it is an implicit requirement that you already have Python (version 3) already installed on your workstation.

Windows

$ pip install opencv-python

macOS

$ brew install opencv3 --with-contrib --with-python3

Linux

$ sudo apt-get install libopencv-dev python-opencv

To check if your installation was successful or not, run the following command in either a Python shell or your command prompt:

import cv2

3 basic Image Processing Techniques With Examples and Code

Now we are going to perform 3 basic Image Processing Techniques Using Python which will help us in understanding the working of OpenCV and Image Processing

  1. 📌 Create an image by yourself Using Python Code and OpenCV
  2. 📌 Take 2 images, crop some parts of both the images and swap them.
  3. 📌 Take 2 images and combine them to form a single image. For example, collage

Creating a Custom Image by Using Python Code OpenCV

Our 1st Objective to create an Image Custom Image using OpenCV on a Blank Canvas.

Step1:

Importing Important Python Libraries like Numpy and OpenCV

importing python libraries OpenCV and NumPy for image processing

Step 2:

Creating a Numpy Array with zeros.

The zeros() function is used to get a new array of given shape and type, filled with zeros.

This will give us a black background for the white background you can use ones() function.

creating NumPy array with zeros for image processing

Step 3:

Now, this is where our actual custom image is created using geometric functions in OpenCV like line and rectangle with color, coordinates, and thickness specified.

using geometric shapes provided in OpenCV for image processing

Step 4:

Now final step is to show the image using imshow() function in OpenCV with a putText() function using cv2.FONT_HERSHEY_SIMPLEX

putting text in the image with puttext()

Now you can see our image is shown here below.

the final output of custom-generated image in OpenCV and python

Take 2 images, crop some part of both the images and swap them

For this tutorial, I have taken these two pictures of Pokemon: Charmendar and Cuban

Step 1:

Importing Python Libraries cv2 and NumPy

import libraries as previously did

Step 2:

Resizing the photo to get the same resolution for both images. It is important to have the same image resolution. So that we can perform operations on images easily.

resizing images

Step 3:

Using the hpstack() function to create a horizontal stack of images and checking the resolution of images using the shape function in OpenCV.

using hstack to stack images horizontally

checking shapes of images

Step 4:

Now comes the main part where you need to crop the faces of two pokemon characters and swap them with each other.

After the cropping and swapping process. Proceed to the next step

cropping and swapping images in image processing

cropping and swapping image

Step 5:

Now here the final step is to stack the images horizontally using the hstack() function and Display the image using imshow().

Below the Picture is the final output after performing the cropping and swapping process.

final output after cropping and swapping

Take 2 images and combine them to form a single image. For example, collage

Step 1:

In this Tutorial, First, you have to import required libraries like NumPy and cv2

importing libraries

Step 2:

Now You need to read the images using the imread() function that is in PNG format. The same two images that we used in the previous tutorial.

Then you will display the images using the imshow() function

If any key is pressed then the display window is terminated.

reading images

Step 3:

Now you need to check the resolutions or shape of the images using the shape function in OpenCV.

As you can see the shape of both the images is different. You are required to change the shape of images and make them similar. Only then we can apply our different image operations successfully.

checking shapes

Step 4:

In this step, you can change the shape of the image which is image number 2 from 643x625 to 240x260 using resize function in OpenCV

resizing images with image processing in python

Step 5:

This is the final step in which you will combine both the images horizontally like a stack using the hstack() function in OpenCV.

finally stacking images horizontally

That’s it you can see the below result. Our Tutorial is completed successfully

Final output images after making collage image processing python

Conclusion:

You have successfully performed all the Image Processing in Python and OpenCV.

Image Processing is fascinating! I started my journey with Python Image Processing in not more than 5 days. So, please ignore any mistake

That’s it for now. I hope you find this article helpful.

--

--