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:
- Reading an image
- Extracting the RGB values of a pixel
- Extracting the Region of Interest (ROI)
- Resizing the Image
- Rotating the Image
- Drawing a Rectangle
- Displaying text
This is the original image that we will manipulate throughout the course of this article.

Let’s start with the simple task of reading an image using OpenCV.
Reading an image
import cv2
image = cv2.imread( 'image.png' )
h, w = image.shape[: 2 ]
print ( "Height = {}, Width = {}" . format (h, w))
|
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
(B, G, R) = image[ 100 , 100 ]
print ( "R = {}, G = {}, B = {}" . format (R, G, B))
B = image[ 100 , 100 , 0 ]
print ( "B = {}" . format (B))
|
Extracting the Region of Interest (ROI)
roi = image[ 100 : 500 , 200 : 700 ]
|

Resizing the Image
resize = cv2.resize(image, ( 800 , 800 ))
|

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.
ratio = 800 / w
dim = ( 800 , int (h * ratio))
resize_aspect = cv2.resize(image, dim)
|

Rotating the Image
center = (w / / 2 , h / / 2 )
matrix = cv2.getRotationMatrix2D(center, - 45 , 1.0 )
rotated = cv2.warpAffine(image, matrix, (w, h))
|

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 –
- 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 co-ordinates of the image and transforms it.
Drawing a Rectangle
It is an in-place operation.
output = image.copy()
rectangle = cv2.rectangle(output, ( 1500 , 900 ),
( 600 , 400 ), ( 255 , 0 , 0 ), 2 )
|

It takes in 5 arguments –
- Image
- Top-left corner co-ordinates
- Bottom-right corner co-ordinates
- Color (in BGR format)
- Line width
Displaying text
It is also an in-place operation
output = image.copy()
text = cv2.putText(output, 'OpenCV Demo' , ( 500 , 550 ),
cv2.FONT_HERSHEY_SIMPLEX, 4 , ( 255 , 0 , 0 ), 2 )
|

It takes in 7 arguments –
- Image
- Text to be displayed
- Bottom-left corner co-ordinates, from where the text should start
- Font
- Font size
- Color (BGR format)
- Line width
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
11 Jan, 2023
Like Article
Save Article