Image Manipulation and processing using OpenCV and NumPy

Anjali Singh
3 min readJun 12, 2021

Hello,

We as humans can see and interpret images using our natural vision and intelligence, but how do computers remember and interpret images?

Computers store images in the form of multi-dimensional array where each unit is called a pixel. Specifically, colored images are stored as 3D array and black and white as 2D array.

There is a special open source library in python called OpenCV which is focused on image processing, face detection, object detection, feature toolkits, facial & gesture recognition, human-computer interaction, mobile robotics, object identification, and more. Written in C++, it also comes with Python wrapper and can work in tandem with NumPy, SciPy, and Matplotlib.

NumPy is another library for image manipulation and processing.

Prerequisite Knowledge and Vocabulary

The color format in general is RGB(Red, Blue, Green) but the color code format used in OpenCV is BGR(Blue, Green, Red).

We can install opencv using the command:

pip install opencv-python

First of all, we import cv2 module of opencv

import cv2

To read an image, we use .imread() function of the cv2 module, specify the path of image in argument for the function.

img = cv2.imread(Vinu.jpeg)

The image is now treated as a numpy array and can be proved by seeing type of image:

print(type(img))
<class ‘numpy.ndarray’>

To display image, we can use .imshow() method of the cv2 module, specify the path of image as argument.

The waitkey() function take time as an argument in milliseconds as the duration for which the image will hold. Here, no argument means the window will hold infinitely until it is closed manually.

cv2.imshow(“MyImage”, img)
cv2.waitkey()

Creating An Image Using Python Code

As we know images are basically multidimensional array of pixels, therefore we can create our own image using functions provided by OpenCV and NumPy. Following is the code to the image I created using the afore mentioned libraries:

Output:

Cropping some part of two images and swapping them

We saw that when we read an image, it converts into Numpy array. Therefore, we can perform the same operations on the image what we perform on a Numpy array. For example, here we crop out some part of two images using slicing operation, resize the using resize() function and then swap them same as we do to elements in an array.

Input Images:

Output:

Creating a Collage of images

Input Images:

Output:

--

--