Image Processing using Python Library OpenCV

Ajmal Muhammed
3 min readJun 12, 2021

Objectives :

To perform the following tasks -

📌 Create image by yourself Using Python Code

📌 Take 2 images, crop some part of the image and swap it.

📌 Take 2 images and combine it to form single image.

Let’s start

What is OpenCV in Python?

OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library. OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in the commercial products. Being a BSD-licensed product, OpenCV makes it easy for businesses to utilize and modify the code.

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

Color code format used in OpenCV is BGR(Blue Green Red).

Let’s begin with first task!

Install OpenCV

pip install opencv-python

Note: Make sure you are using python3 as the default version

Task 1: 📌 Creating an image Using OpenCV:

First of all, to use OpenCV import cv2 module

Also, need to import numpy module to create an 3D array.

For a computer, an image is a two-dimensional signal, made up of rows and columns of pixels. An input of one form can sometimes be transformed into another. For instance, Magnetic Resonance Imaging (MRI), records the excitation of ions and transforms it into a visual image.import cv2

import cv2

To create an image, then we need to create an numpy array with zeros

cn = numpy.zeros((600, 600, 3))

This will create an image of 600 rows and 600 columns with value as zeros. So, the resulting output would be a dark image because of zero.

To view the output

cv2.imshow("Anyname", cn)
cv2.WaitKey()
cv2.destroyAllWindows()

Now, we can perform any operations on this array. On changing the values of each row or column with BGR values we will able to draw pictures or create pictures of different shapes.

For more details visit this Githublink:

https://github.com/ajmalmohammedn/Computer-Vision/blob/9455fd8e418a5ca0b05d51d3d5c6f79d7920d991/Creating%20image%20using%20opencv.ipynb

Task 2: 📌 Crop some part of the image and swap it.

Since images are nothing but 2D/3D arrays we can perform slicing function on them to extract specific sections of the image. For that we can perform the slicing operations on the array.

First of all select two images on which we need to perform swapping.

import cv2

Then we need to read images

image1 = cv2.imread("path_of_the_image")
image2 = cv2.imread("path_of_the_image")

After reading the image and store it a varibale,then we need to slice/crop the part which we need to swap and store in another variable.

After this exchanging the variables with values will swap the two images.

But here we need to make sure that both the crop parts having the same shape. If it’s not then we would not be able to perform the swapping operation.

To make it as the same shape we need to perform some math operation on one of the image.

Sample code:

girl_face = girl[1:145, 40:180]
width = int(girl_face.shape[1] * 215/140) # here dividing the column of boy's face shape by the column of girl's face to make
height = int(girl_face.shape[0] * 310/144) # same shape of boy inorder to swap it
dim = (width, height)
girl_resized = cv2.resize(girl_face, dim, interpolation=cv2.INTER_AREA)

For more details visit this Githublink:

Task 3: 📌 Combine two images to form single image / collage.

There are different ways to merge to images in OpenCV using Numpy arrays.

One of the method is using hstack() :

To use the hstack() first we need to import numpy .

import numpy as np
import cv2

For mergin two images, first read the two images and store it in a variable. Then make sure both images shape is same as explained earlier in swapping.

If it’s not then perform the below operation on one of the image.

It’s a sample code:

girl_face = girl[1:145, 40:180]
width = int(girl_face.shape[1] * 215/140) # here dividing the column of boy's face shape by the column of girl's face to make
height = int(girl_face.shape[0] * 310/144) # same shape of boy inorder to swap it
dim = (width, height)
girl_resized = cv2.resize(girl_face, dim, interpolation=cv2.INTER_AREA)

For more details visit this link:

https://github.com/ajmalmohammedn/Computer-Vision/blob/9455fd8e418a5ca0b05d51d3d5c6f79d7920d991/Two%20images%20combine%20together%20using%20OpenCV%20and%20hstack%20to%20make%20collage.ipynb

--

--