Introduction to OpenCV

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, I will try to introduce the most basic and important concepts of OpenCV in an intuitive manner.
This article will cover the following topics:

  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.

Original Image



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

Reading an image

filter_none

edit
close

play_arrow

link
brightness_4
code

# Importing the OpenCV library
import cv2
# Reading the image using imread() function
image = cv2.imread('image.png')
  
# Extracting the height and width of an image
h, w = image.shape[:2]
# Displaying the height and width
print("Height = {},  Width = {}".format(h, w))

chevron_right


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

Extracting the RGB values of a pixel

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Extracting the Region of Interest (ROI)

filter_none

edit
close

play_arrow

link
brightness_4
code

# We will calculate the region of interest 
# by slicing the pixels of the image
roi = image[100 : 500, 200 : 700]

chevron_right


Region Of Interest

Resizing the Image

filter_none

edit
close

play_arrow

link
brightness_4
code

# resize() function takes 2 parameters, 
# the image and the dimensions
resize = cv2.resize(image, (800, 800))

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Resizing with proper aspect ratio


Rotating the Image

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Rotated Image

There are a lot of steps involved in rotating an image. So, let me explain each of them in detail.

The 2 main functions used here are –

  • getRotationMatrix2D()
  • warpAffine()

getRotationMatrix2D()
It takes 3 arguments –