How about โ€œ๐Ÿ“Œ Image Manipulation with OpenCV: Combining and Swapping

Abhinav Shreyash
4 min readAug 3, 2023

Objective :

To perform the following tasks -

๐Ÿ“Œ Create image by yourself Using Python Code

๐Ÿ“Œ Take 2 images, crop some part of the image and swap it.

๐Ÿ“Œ Take 2 images and combine it to form single image.

Approach :

To perform the following tasks we will use OpenCV with Python and the ide is Jupyter notebook.

Letโ€™s see what is OpenCV in Python?

So, OpenCV-Python is a library of Python bindings designed to solve computer vision problems. It makes use of Numpy, which is a highly optimized library for numerical operations with a MATLAB-style syntax. All the OpenCV array structures are converted to and from Numpy arrays. This also makes it easier to integrate with other libraries that use Numpy such as SciPy and Matplotlib.

๐Ÿ“Œ Creating an image by yourself Using Python Code :

For this we can refer to the shapes drawing functions available in OpenCV.

  • Drawing Line : To draw a line, you need to pass starting and ending coordinates of line.
  • Drawing Rectangle : To draw a rectangle, you need top-left corner and bottom-right corner of rectangle.

The function that draws a rectangle is cv2.rectangle(). The rectangle is defined with two points in two opposite corners. The first one is at the top left corner and the second one is at the bottom right corner. It is good to remember that negative values for the thickness parameter, will give us a filled shape as our output.

  • Drawing Circle : To draw a circle, you need its center coordinates and radius.

So, here is a snippet of how using the above functions I was able to create a custom image:

Code snippet
The output

๐Ÿ“ŒTake 2 images, crop some part of the image and swap it.

The important thing to note here is the slicing function since images are nothing but 2D/3D arrays we can perform slicing function on them to extract specific sections of the image.

<cropped_image_variable>=image[starty:endy, startx:endx]

The starty:endy slice provides the rows (since the y-axis is thenumber of rows) while startx:endx provides the columns (since the x-axis is the number of columns) in the image.

Below are the images used to perform the task.

Balls
A person
Code Snippet
Output

๐Ÿ“Œ Take 2 images and combine it to form a single image

There are different ways to merge to images in OpenCV using Numpy arrays so one the methods is:

hstack() :

numpy.hstack(tup)

  • Stack arrays in sequence horizontally (column wise).
  • This is equivalent to concatenation along the second axis, except for 1-D arrays where it concatenates along the first axis. Rebuilds arrays divided by hsplit.
  • This function makes most sense for arrays with up to 3 dimensions. For instance, for pixel-data with a height (first axis), width (second axis), and r/g/b channels (third axis). The functions concatenate, stack and block provide more general stacking and concatenation operations.

Parameters: tup : sequence of ndarrays

The arrays must have the same shape along all but the second axis, except 1-D arrays which can be any length.

Returns : stacked: ndarray

The array formed by stacking the given arrays.

Below are the images used to perform the task:

img1
img2
Code Snippet
Output

--

--