Open In App

Introduction to OpenCV

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

OpenCV is one of the most popular computer vision libraries. If you want to start your journey in the field of computer vision, then a thorough understanding of the concepts of OpenCV is of paramount importance. 

In this article, to understand the basic functionalities of Python OpenCV module, we will cover the most basic and important concepts of OpenCV in an intuitive manner:

  1. Reading an image
  2. Extracting the RGB values of a pixel
  3. Extracting the Region of Interest (ROI)
  4. Resizing the Image
  5. Rotating the Image
  6. Drawing a Rectangle
  7. Displaying text

This is the original image that we will manipulate throughout the course of this article. 

Input Image

Input Image

Let’s start with the simple task of reading an image using OpenCV. 

For the implementation, we need to install the OpenCV library using the following command:

pip install opencv-python

Reading an Image 

First of all, we will import cv2 module and then read the input image using cv2’s imread() method. Then extract the height and width of the image.

Python3
# Importing the OpenCV library
import cv2
# Reading the image using imread() function
image = cv2.imread('image.jpg')

# Extracting the height and width of an image
h, w = image.shape[:2]
# Displaying the height and width
print("Height = {}, Width = {}".format(h, w))

Output:

Height = 1603, Width = 2400

Extracting the RGB Values of a Pixel 

Now we will focus on extracting the RGB values of an individual pixel. OpenCV arranges the channels in BGR order. So the 0th value will correspond to the Blue pixel and not the Red. 

Python3
# Extracting RGB values.
# Here we have randomly chosen a pixel
# by passing in 100, 100 for height and width.
(B, G, R) = image[100, 100]

# Displaying the pixel values
print("R = {}, G = {}, B = {}".format(R, G, B))

# We can also pass the channel to extract
# the value for a specific channel
B = image[100, 100, 0]
print("B = {}".format(B))

Output:

R = 211, G = 172, B = 165
B = 165

Extracting the Region of Interest (ROI) 

Sometimes we want to extract a particular part or region of an image. This can be done by slicing the pixels of the image.

Python3
# We will calculate the region of interest
# by slicing the pixels of the image
roi = image[100 : 500, 200 : 700]
cv2.imshow("ROI", roi)
cv2.waitKey(0)

Output:

Output Image of Region of Interest

Resizing the Image 

We can also resize an image in Python using resize() function of the cv2 module and pass the input image and resize pixel value.

Python3
# resize() function takes 2 parameters,
# the image and the dimensions
resize = cv2.resize(image, (500, 500))
cv2.imshow("Resized Image", resize)
cv2.waitKey(0)

Output:

Resizing Image

Output of Resized Image

The problem with this approach is that the aspect ratio of the image is not maintained. So we need to do some extra work in order to maintain a proper aspect ratio. 

Python3
# Calculating the ratio
ratio = 800 / w

# Creating a tuple containing width and height
dim = (800, int(h * ratio))

# Resizing the image
resize_aspect = cv2.resize(image, dim)
cv2.imshow("Resized Image", resize_aspect)
cv2.waitKey(0)

Output:

Resizing Image with maintained Aspect Ratio

Output of Resized Image with maintained Aspect Ratio

Rotating the Image 

There are a lot of steps involved in rotating an image. So, let us see each of them in detail. The 2 main functions used here are:

  • getRotationMatrix2D() It takes 3 arguments:
    • center – The center coordinates of the image
    • Angle – The angle (in degrees) by which the image should be rotated
    • Scale – The scaling factor

It returns a 2*3 matrix consisting of values derived from alpha and beta
alpha = scale * cos(angle)
beta = scale * sine(angle)

  • warpAffine(): The function warpAffine transforms the source image using the rotation matrix:
dst(x, y) = src(M11X + M12Y + M13, M21X + M22Y + M23)

Here M is the rotation matrix, described above.

It calculates new x, y coordinates of the image and transforms it.

Python3
# Calculating the center of the image
center = (w // 2, h // 2)

# Generating a rotation matrix
matrix = cv2.getRotationMatrix2D(center, -45, 1.0) 

# Performing the affine transformation
rotated = cv2.warpAffine(image, matrix, (w, h))

Output:

Rotating image

Output Image after Rotation

Drawing a Rectangle

We can draw a rectangle on the image using rectangle() method. It takes in 5 arguments: 

Python3
# We are copying the original image,
# as it is an in-place operation.
output = image.copy()

# Using the rectangle() function to create a rectangle.
rectangle = cv2.rectangle(output, (1500, 900),
                        (600, 400), (255, 0, 0), 2)

Output:

Drawing Rectangle on image

Output Image with rectange drawn on it

Displaying text

It is also an in-place operation that can be done using the putText() method of OpenCV module. It takes in 7 arguments:

Python3
# Copying the original image
output = image.copy()

# Adding the text using putText() function
text = cv2.putText(output, 'OpenCV Demo', (500, 550),
                cv2.FONT_HERSHEY_SIMPLEX, 4, (255, 0, 0), 2)

Output:

Displaying text on image

Output image with text on it



Last Updated : 14 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads