Basics of OpenCV (Resizing, Cropping, Rotation, and some other image Attributes)

Here we’ll be doing many operations on an image using OpenCV functions. To make this session interactive image of Thanos(A character from a famous movie ) is being taken for example.

Various operations of OpenCV in Python

The various operation goes as follows:

Importing the required libraries required for the program

import cv2
import numpy as np

cv2 is the OpenCV library and the numpy library is included as many times the image will be treated as an array.

Reading and displaying of an image – OpenCV Python


cv2.imread() is a function which takes image name as a parameter(provided the image is saved in the same folder where the program is or else just give the path to the image) and cv2.imshow() display the image with name and its parameters are (‘name_of_the_window’,variable_in_which_image _is_stored). The output is given as:

Reading and displaying of an image - OpenCV Python

Printing the dimensions of an image in OpenCV


Normally, the dimensions are width*height but OpenCV takes as height*width. The output will be as: (175, 289, 3) as displayed for my image. The values are (height, width, channel) where the channel is the RGB components.

Resizing of an image in Python with OpenCV

dimension = (w1, h1) 
resized_image = cv2.resize(image, dimension, interpolation = cv2.INTER_AREA)
cv2.imshow("resized", resized_image)

As seen in code the height and width are specified as 300. Both values are then inserted into the variable called dim(dimension of new image). The third line uses the function cv2.resize() which actually does the main work of changing the size. The parameters are the original image, dimension, and the algorithm to be used for this purpose. For time being we are not going to focus on the algorithm but sticking to the implementation part. The output after this is:

Resizing of an image in Python with OpenCV

 Rotation of an image in OpenCV

(h1, w1) = image.shape[:2]
center = (w1 / 2, h1 / 2)
Matrix = cv2.getRotationMatrix2D(center, 180, 1.0)
rotated_image = cv2.warpAffine(image, Matrix, (w1, h1))
cv2.imshow("rotated", rotated_image)

As we know now image.shape() returns a tuple up to indexing 2 where the first two values are height and width hence the first line extracts the values of height and width, We then discover the center coordinates by moving towards half of both height and width. The function cv2.RotationMatrix2D returns a matrix that contains the image of rotated coordinates. The parameters are center, angle_of_rotation(here we are rotating for an angle of 180 degrees) and scaling factor. Later the transformation is done by cv2.warpAffine() function whose parameters are the original_image,  obtained matrix and the dimension of the rotated image. The output is:

Rotation of an image in OpenCV


Cropping of an image in Python

cropped_image = image[50:170, 150:250]
cv2.imshow("cropped", cropped_image)

The first line returns that part of the image which starts and ends between (beginX:endX), (beginY,endY). This crop the image. Just for fun, we crop to get the Thanos’s gauntlet. The output is:

Cropping of an image in Python

The basics of OpenCV ends here.


Leave a Reply

Your email address will not be published. Required fields are marked *