Image Processing using OpenCV

Jainsiddhant
4 min readJun 5, 2021

OpenCV is the huge open-source library for the computer vision, machine learning, and image processing and now it plays a major role in real-time operation which is very important in today’s systems. By using it, one can process images and videos to identify objects, faces, or even handwriting of a human. In this section, we are going to see how one can perform various operations while doing image processing with the help of OpenCV library.

With the help of OpenCV library different operations can be done like making an image, changing color of the image & face detection . Some of them are shown below:

Create image by yourself Using Python Code

For making an image, one should first understand that all the images are collection of 2D arrays and the smallest unit of images is pixel. Thus ,we can make any images by providing appropriate BGR( Blue Green Red) values to make different colors.

For doing this firstly we have to install and import numpy and cv2 library. While installing numpy library en error might come up “ No module named skbuild “. In this case try to upgrade your pip using pip install — upgrade pip and try installing once again. Once both the libraries are installed we can continue the process of image processing.

OpenCV comes up with different functions to draw line and different shapes to solve image processing problems. Some of them are mentioned below:

cv2.line() method is used to draw a line on any image.

cv2.ellipse() method is used to draw a ellipse on any image.

cv2.rectangle() method is used to draw a rectangle on any image.

cv2.circle() method is used to draw a circle on any image.

Using these functions we can make any type of images. For example: I have made an image of heart with the help of cv2.ellipse() and cv2.line() functions.

Code for making heart image
Heart image

To read the images cv2.imread() method is used.

cv2.imshow() method is used to display an image in a window.

cv2. waitKey(delay) waits for delay milliseconds.

cv2.destroyAllWindows() is used to close all open windows.

Cropping some part of two images and swap it.

Cropping the image is an easy task with the help of numpy . As every image is an 2D numpy array, thus cropping can be done by applying column wise operations on the image. For example if we want to crop the image from 100th row to 200th row and from 50th column to 100th column we can use numpy[100:200,50:100] and the image will be cropped. Now, to swap the cropped part of the image , just replace the cropped part with the same row and column of the other image. For better understanding, look at these images.

Here, i have taken two images mountain.jpg and nature.jpg and swap the cropped part.

Swapping the cropped part of the image

Taking 2 image and combine it to form single image.

To concatenate images vertically and horizontally with Python, cv2 library comes with two functions as:

  1. hconcat(): It is used as cv2.hconcat() to concatenate images horizontally. Here h means horizontal.
  2. vconcat(): It is used as cv2.vconcat() to concatenate images vertically. Here v means vertical.

While concatenating two images make ensure that the size of the images should be same otherwise error might come up. We can use cv2.resize() to change the size of the image according to our need.

Concatenating two images horizontally
Concatenating two images vertically

Open for any Queries and Suggestions .

Thank you

--

--