Image Processing using OpenCV

Mukesh RMK
3 min readJun 3, 2021
fig 1.1

Task Description:

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

To process images, we need few python libraries to install first…

pip install opencv-python

pip install numpy

I am going to use these libraries in our program, so let's get started….

Part 1: Create an image by yourself Using Python Code

Import those libraries,

import cv2
import numpy as np

Now, we use cv2 function to draw some shapes like,

circle: cv2.circle(img,(initial_coordinates), radius, color, thickness)

line: cv2.line(img,(initial_coordinates),(final_coordinates),(color),thickness)

#Create Head
cv2.circle(img,(200,60), 50, (255,255,0), -1)
#Eyes
cv2.circle(img,(180,45), 8, (0,0,0), -1)
cv2.circle(img,(215,45), 8, (0,0,0), -1)
#Mouth
cv2.line(img,(180,90),(220,90),(0,0,0),5)

#Body
cv2.line(img,(200,112),(200,250),(255,255,255),5)

#Create Hand
cv2.line(img,(200,160),(300,30),(0,0,255),5)
cv2.line(img,(200,160),(100,30),(0,0,255),5)

#Create Leg
cv2.line(img,(200,250),(300,380),(255,0,0),5)
cv2.line(img,(200,250),(100,380),(255,0,0),5)

Finally, all we need to display this image,

#Display
cv2.imshow(“My picture”,img)
cv2.waitKey(0)

imshow(): This function used to display the image having 2 parameters one is the name of the window other is the image.

waitKey(0): It holds the window until the user terminates it.

Output:

fig 1.2

Part 2 & 3: Take 2 images crop some parts of both images and swap them and combine them.

I will do both the task in the same program, so let’s begin…

import cv2
import numpy as np

Let’s import both the image so that we can do some operation over it…

#Store 1st image to variable with same dimention
img1 = cv2.imread(“img1.png”)[0:404,0:404]

#Store 2nd image to variable with same dimention
img2 = cv2.imread(“img2.png”)[0:404,0:404]

imread(): This function is used to import the image to the program and it takes one parameter as the name of the image.

You must be wondering why I have used [0:404,0:404], so this is for the reason if both images have different heights and widths then our operation will show an error so we cropped both the images in the same height and width.

#Stack the image
prev = np.hstack((img1,img2))
print(prev.shape)
cv2.imshow(“Before”,prev)
cv2.waitKey(0)

To make an image collage or to combine them we use the function of NumPy i.e.,

hstack(): To join both images horizontally.

vstack(): To join images vertically.

As we know an image is just a 2D or 3D matrix so these functions are used to merge both arrays.

fig 1.3

# Now we swap these part:

img1[0:200,150:400] = img1[0:200,150:400]+img2[0:200,150:400]
img2[0:200,150:400]= img1[0:200,150:400]-img2[0:200,150:400]
img1[0:200,150:400] = img1[0:200,150:400]-img2[0:200,150:400]

This is how I swap cropped portion of these images, you can use other methods also to swap it…

#Stack Both Image
after = np.hstack((img1,img2))

#Display the Image
cv2.imshow(“Aft”,after)
cv2.waitKey(0)

Let's see the final result…

Output:

Thank you for reading….

I hope it will be helpful for you……..

--

--