Image Processing using OpenCV and Numpy in Python
By: Sachin Joshi
Tags: Opencv, Jupyter Notebook, Python, Numpy, Image Processing

OpenCV provides many drawing functions to draw geometric shapes and write text on images. Let’s see some of the drawing functions and draw geometric shapes on images using OpenCV.

Images is a 3D array if it is colourful. Since computers are not intelligent they do not understand colours. They store this data in the form of data in a array format.

Images is a combination of multiple pixels in other words Pixels is the smallest unit of a Image. Values put in this array are in the form of RGB(Red,Green,Blue) ranging from 0 to 255.

OpenCV library, cv2 module stores the value in BGR format (Blue,Green,Red)

In a pixel, they store no(255,0,0) for red colour.This how image is stored internally

Same way Video is like clicking mutiple photos continously in a flash mode in a particular time frame. So basically we just have to click the photo and display it continously in infinte loop.

Some of the drawing functions are :

cv2.line() : Used to draw line on an image.

cv2.rectangle() : Used to draw rectangle on an image.

cv2.circle() : Used to draw circle on an image.

cv2.putText() : Used to write text on image.

Task 1 : Create image by yourself Using Python Code

To demonstrate uses of the above-mentioned functions we need made a image of size 400px X 400px filled with a solid colour (Black in this case). Inorder to do this, We can utilize numpy.zeroes function to create the required image. This stores the data in array form

Draw a Circle :

cv2.circle (imageObjectName, (‘center_coordinates’), (‘circle_radius’), (‘color_in_bgr’), ‘stroke_thickness’)

To save the image with the cv2 module write:

To open the photo within python code and to close it after viewing :

This will be the output:

Task 2 : Take 2 image crop some part of both image and swap it

Let us first import cv2 library.

Make sure you have 2 images you want to work on. Here I have taken two images as well named “snoopy.jpg” and “wiz.png”

Now, let us load the wiz.png image in a variable named wiz

Here let us define the co-ordinates of the face in the square/rectangle free form shape.

And store those co-ordinate in a variable named wFace, this variable will store the cropped image and use cv2.imshow to display the cropped image

Repeat the same procedure for other image name snoopy.jpg

Make sure you use/give the same co-ordinates of that of the first image.

Here store the cropped image in sFace variable

Below images shown are the cropped images

Now, next we are going to swap the cropped images

Wiz[dy1:dy2, dx1:dx2]= sFace

Means that the data storedin the variable sFace fills the data present in the co ordinates defined in the wiz image above

The same goes with snoopy image

The final output:

Task 3 : Take 2 image and combine it to form single image. For example collage

These are the two images we are taking for this task.

Lets, us start by importing cv2 module for collage

Select the co-ordinate such that from the image that it selects the part you want to collage

Crop the image “Downeyjr.jpg”

This is the required output from these lines:

Same way we will crop the image in “Ironman.jpg”

This is the required output from these lines:

Now, let us create a collage of these two images which we have crop and stored in the variable “dface” and “cface”

Import numpy library here to store the data in horizontal stack in variable collagedb

After doing all the steps we will get the final output as given below:

Thankyou for Reading and Do share !!!