Image processing using openCV python library

Sonipuru
3 min readJun 3, 2021

Here in this article we will do some image processing operations like creating our own image, swapping two images and concatenating two images to form a collage by using opencv python library.

Introduction to Digital image processing

Digital image processing consists of the manipulation of images using digital computers. Its use has been increasing exponentially in the last decades.The processing of digital images can be divided into several classes: image enhancement, image restoration, image analysis, and image compression.

OpenCV python library

OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrated with various libraries, such as Numpy which is a highly optimized library for numerical operations, then the number of weapons increases in your Arsenal i.e whatever operations one can do in Numpy can be combined with OpenCV.

Now we will do some image processing operations:-

Firstly We will create our own image

For this first we have to import two modules cv2 and numpy in our jupyter notebook.

Import cv2

Import bumpy

Now we will write our code as:-

In this we had first created an image by using zeros function of cv2 module.

Then drawn some shapes according to our image Using line,circle, rectangle functions.

Then we displayed the image using
Imshow() ,waitkey() and destroyAllWindows() function.

So here is our own created image

Now we will swap two images

For this we will first download two images. In our case we downloaded images of msdhoni and suresh raina .

Now we will first crop the faces of both images just like slicing of arrays as array_name[y1:y2,x1:x2]

Now in place of Dhoni’s face we put Raina’s face by just swapping the face variable of both images.

Here imread() function will read the images and put it in dhoni and raina variables respectively

And by

Dhoniface = dhoni[20:240,290:470]

We crop dhoni’s face.similarly,we do for raina’s face as

Rainaface = raina[30:250,160:340]

Now we will swap both the faces and our image will look like

Now we will concatenate two images

For concatenation we will used the previous images of dhoni and Raina download earlier now we will store them in new variable image1 and image2 respectively.

Now we will resize them to make the two images equal in size because can’t concatenate two different sized arrays by using slicing operator.

After that we will import numpy module because for concatenating two images we will use hstack() function of numpy module

Our desired results would be--

Now our task is done .

Thankyou for reading

--

--