👨‍💻Image Processing using Python Library OpenCV

Yash Panchwatkar
4 min readJun 12, 2021

--

Summer — Task 04
⚜️ Team Task
Task Description 📄

📌 Create image by yourself Using Python Code
📌 Take 2 image crop some part of both image and swap it.
📌 Take 2 image and combine it to form single image. For example collage

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

let’s first create image by code… task 4.1

import cv2
import numpy as np
l = np.zeros((500,600,3))
l[60:120,120:180]=1
l[60:120,600-180:600-120]=1
l[220:280,270:320]=[0,255,255]
l[340:380,140:470]=1
l[10:12]=1
l[-12:-10]=1
l[:,10:12]=1
l[:,-12:-10]=1

To create an image, then we need to create an numpy array with zeros, Here in above code I had created a 3D array of 500 rows and 600 columns

def showImg(data):
cv2.imshow("new",data)
cv2.waitKey()
cv2.destroyAllWindows()

so by running above code we can create this simple image. You can customize with your requirement.

Now let’s move on towards task 4.2

Here we are taking two images and then by cropping faces of two images we will going to interchange it. For demo purpose let’s take our be loving tom and jerry pics.

Here’s the respective code.

#part 1 taking co-ordinates of faces.
j=newimg[:,:295]
jfinal=cv2.resize(j,(320,320))
i=newimg[190:,295:]
ifinal=cv2.resize(i,(320,320))
jcrop=jfinal[10:105,70:-50]
icrop=ifinal[30:160,:230]
#part 2 creating same dimensions.
width = int(jcrop.shape[1] * 230/200) # here dividing the column of jerrys face shape by the column of tom's face to make
height = int(jcrop.shape[0] * 130/95) # same shape of boy inorder to swap it
dim = (width, height)
final_crp = cv2.resize(jcrop, dim, interpolation=cv2.INTER_AREA)
#part 3 flip image horizontally
final_crp = cv2.flip(final_crp,1)

Here first part 1 will take co-ordinates of faces. part 2 will make dimensions of cropped faces same and part 3 will flip image horizontally . so now we can see image is cropped and swapped successfully.

Now let’s move towards task 4.3

here we have to take 2 images and combine it to form single image. For example collage

Now let’s take previous example we had cropped one image into 2 images tom image and jerry image.

final_image=np.concatenate((j,i),1) # final=np.hstack((img1,img2))
showImg(final_image)

Here we can see No of rows are same so we can combine it horizontally. so above code perform horizontal stack operation.

So yeah we have successfully completed our task no 4 play with images…

Thanks for reading… If any questions please feel free to leave a comment below and Do connect with me on these platforms.

  1. Mail: yashpanchwatkar@gmail.com
  2. LinkedIn: https://www.linkedin.com/in/yash-panchwatkar/

Stay tuned for such awesome stories!

--

--