Operations on Image in OpenCV

Shubham Kumar
4 min readJun 8, 2021

Creating an Image:

Here is the code for creating an image using numpy array, as first I have created a 3D numpy array of size (600*600*3) in which I filled orange color from rows (0–200) and then accordingly I filled white and green colors in the rows (200–400) & (400–600) respectively. Then with the cv2.circle function I created an image with centre coordinate as (300,300) and radius as 100, with filling blue color in it

This is the view of the image created

Cropping and Swapping the cropped part:

rob.jpg
hal.jpg

These are the two images which I am going to use for cropping the faces and swapping with one another

First we will load two different image in two different variables

Then we will crop a part of first image by slicing through rows and columns, i.e. for rows(start_index=7: end_index=82) and for columns(start_index=90: end_index=160)

Displaying the first cropped part

Then we will crop a part of second image by slicing through rows and columns, i.e. for rows(start_index=15: end_index=92) and for columns(start_index=90: end_index=175)

Displaying the cropped part

Allocating the cropped image in the particular selected rows and columns of the first image for swapping

Code for setting the cropped part of first image in the second image

Face Swapped (Cropped part swapped)

Making a collage:

Here, first we have checked for the dimensions of each image and then we have set the dimensions of both the image in such a way so that both image should have same dimension for making a collage

Code in which I have added two images in numpy.hstack which horizontally stacks the sequences of input array to convert it into a single array, so I have used this method for making a collage of two images

In this I have used numpy.vstack function which stacks the sequences in a vertical order and I have implemented this for creating a collage in which images are stored in a vertical order

So these were my 3 operations which I performed using OpenCV and numpy

ThankYou !

For reading my blog, I hope you understood the operations which I have performed, if not then you can reach out to me through LinkedIn

--

--