Image Manipulations using OpenCV, Numpy and Python

Venkatesh
Analytics Vidhya
Published in
5 min readJun 27, 2020

--

OpenCV was started at Intel in 1999 by Gary Bradsky, and the first release came out in 2000. Vadim Pisarevsky joined Gary Bradsky to manage Intel’s Russian software OpenCV team. In 2005, OpenCV was used on Stanley, the vehicle that won the 2005 DARPA Grand Challenge. Later, its active development continued under the support of Willow Garage with Gary Bradsky and Vadim Pisarevsky leading the project. OpenCV now supports a multitude of algorithms related to Computer Vision and Machine Learning and is expanding day by day

Python is a general-purpose programming language started by Guido van Rossum that became very popular very quickly, mainly because of its simplicity and code readability. It enables the programmer to express ideas in fewer lines of code without reducing readability

Image manipulations are the very basic concepts of understanding OpenCV very well. Here, I am trying to introduce some of the concepts of OpenCV in a easy understandable way.

To work on OpenCV. First of all, check whether OpenCV is installed or not. Open new Jupiter notebook and type following and run.

import cv2
print(cv2.__version__)

If the output is a version of OpenCV then OpenCV is installed. If not execute following commands.

pip install opencv-python
pip install opencv-contrib-python

Now again run above commands to check the OpenCV is successfully installed or not

Now in the next cell import required libraries

import cv2
import numpy as np

Image Translations

Translations are very simple. It is basically moving an image in one direction(Left, Right, Up, Down and Diagonally). To make translations we need translation matrix.

T = [[1 0 x],
[0 1 y]] #Translation Matrix

X represents shift along the x-axis and Y represents shift along the y-axis

Firstly, Read an image

image = cv2.imread('image.jpg')
cv2.imshow('image',image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Actual image

Now apply a translation to the image

image = cv2.imread('image.jpg')
height, width = image.shape[:2]
wrap_x, wrap_y = 100,100
T= np.float32([[1, 0, wrap_x],[0, 1, wrap_y]])
img_trans = cv2.warpAffine(image,T,(width,height))
cv2.imshow('image',img_trans)
cv2.waitKey(0)
cv2.destroyAllWindows()
Diagonally Translated

We can obtain different translations by changing the values in the Translation Matrix.

Rotations

OpenCV allows us rotation and scale at a time. we use Rotation matrix for image rotations.

Syntax: cv2.getRotationMatrix2D(center, angle, scale)
Syntax: cv2.warpAffine(src, Matrix, dsize, dst, flags, borderMode, borderValue)

image = cv2.imread('image.jpg')
height, width = image.shape[:2]
rotation_matrix = cv2.getRotationMatrix2D((width/2,height/2),90,1)
img_rotated = cv2.warpAffine(image,rotation_matrix,(width,height))
cv2.imshow('image',img_rotated)
cv2.waitKey(0)
cv2.destroyAllWindows()

90 indicates how much image is to rotate in a counter-clockwise direction and 1 is scale.

Rotated image

Resizing images

Resizing images is one of the technics in OpenCV. This makes the image occupy less space in the disk. Interpolation is used for a better way of resizing images.

Interpolation: It is a method of constructing new data points within the range of a discrete set known data points.

Interpolation is optional. Interpolations are different types. It takes one of the following methods. INTER_NEAREST — a nearest-neighbour interpolation INTER_LINEAR — a bilinear interpolation (used by default) INTER_AREA — resampling using pixel area relation. It may be a preferred method for image decimation, as it gives moire’-free results. But when the image is zoomed, it is similar to the INTER_NEAREST method. INTER_CUBIC — a bicubic interpolation over 4×4 pixel neighbourhood INTER_LANCZOS4 — a Lanczos interpolation over 8×8 pixel neighbourhood.

Syntax: cv2.resize(src, dsize, fx, fy, interpolation)

image = cv2.imread('image.jpg')
image_scaled = cv2.resize(image,None,fx=0.75,fy=0.75)
cv2.imshow('image Scalled',image_scaled)
cv2.waitKey(0)
cv2.destroyAllWindows()
#interpolation
#increasing size of image
img_scl=cv2.resize(image,None,fx=2,fy=2,interpolation=cv2.INTER_CUBIC)
cv2.imshow('image Scalled',img_scl)
cv2.waitKey(0)
cv2.destroyAllWindows()
#Resizing the image to specific size
image_scaled = cv2.resize(image,(700,540),interpolation=cv2.INTER_AREA)
cv2.imshow('image Scalled',image_scaled)
cv2.waitKey(0)
cv2.destroyAllWindows()

Cropping

It is so simple to crop a particular segment from an image. We can give directly Starting and ending points in an image to crop a certain segment.

image = cv2.imread('image.jpg')
height, width = image.shape[:2]
start_row,start_col =int(height * 0.25), int(width * 0.25)
end_row,end_col = int(height * 0.75),int( width * 0.75)
crop = image[start_row:end_row,start_col:end_col]
cv2.imshow('actual image', image)
cv2.waitKey(0)
cv2.imshow('cropped image',crop)
cv2.waitKey(0)
cv2.destroyAllWindows()
Cropped Image

Brightening and Darkening

We can make an image brighter or darker simply, by adding/subtracting a value from each element of the image matrix. The value must lie between 0 and 255.

image = cv2.imread('image.jpg')value =100
mat = np.ones(image.shape,dtype = 'uint8')*value
brighter = cv2.add(image,mat)
cv2.imshow('brighter',brighter)
cv2.waitKey(0)
cv2.destroyAllWindows()
Brighten image
image = cv2.imread('image.jpg')value =100
mat = np.ones(image.shape,dtype = 'uint8')*value
subtract = cv2.subtract(image,mat)
cv2.imshow('subtract',subtract)
cv2.waitKey(0)
cv2.destroyAllWindows()
Darken Image

Bitwise Operations

Bitwise operations help you in image masking. To understand the bitwise operation. Let us build some shapes.

Square

Syntax: cv2.rectangle(image, start_point, end_point, color, thickness)

image=np.zeros((300,300),np.uint8)
cv2.rectangle(image,(50,50),(250,250),255,-2)
cv2.imshow('square',image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Ellipse

Syntax: cv2.ellipse(image, centerCoordinates, axesLength, angle, startAngle, endAngle, color , thickness)

image=np.zeros((300,300),np.uint8)
cv2.ellipse(image,(150,150),(150,150),30,0,180,255,-1)
cv2.imshow('Eclipse',image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Now apply bitwise operations(AND, OR, XOR, NOT) to above shapes to understand more clearly

Bitwise AND

And = cv2.bitwise_and(square,image)
cv2.imshow('And',And)
cv2.waitKey(0)
cv2.destroyAllWindows()
AND

Bitwise OR

Or = cv2.bitwise_or(square,image)
cv2.imshow('OR',Or)
cv2.waitKey(0)
cv2.destroyAllWindows()
OR

Bitwise XOR

xor = cv2.bitwise_xor(square,image)
cv2.imshow('XOR',xor)
cv2.waitKey(0)
cv2.destroyAllWindows()
XOR

Bitwise Not

Not = cv2.bitwise_not(square)
cv2.imshow('Not',Not)
cv2.waitKey(0)
cv2.destroyAllWindows()
NOT

These are some concepts of OpenCV for the image manipulations. you can find entire code in my GitHub repo here. If you have any doubts, Please free to leave a comment.

--

--

Venkatesh
Analytics Vidhya

Beginner to Machine learning and computer vision