Basic OpenCV Operations Python

Opencv basic operations python

OpenCV is one of the most common computer vision libraries written in C / C++. Whether you want to continue your career in the area of computer vision, Machine Learning or Artificial Intelligence, then knowing the basic concepts of OpenCV is really necessary.

In this blog, I will try to introduce the most basic and important concepts to learn OpenCV.

Basic OpenCV Operations

  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. Displaying text
  7. Drawing Geometric Shapes

Why you should learn OpenCV in Python?

GitHub:- Divyanshu Shekhar GitHub

OpenCV Image

cv2.imread() function takes the image which you want the machine to read.

cv2.imshow() takes two parameters-

  1. Name of the window and
  2. The image that will be shown in that particular window.

cv2.waitKey() takes time in seconds as an argument, when not specified anything it takes 0 as the default value and waits for the user to end the cv2.imshow() task.

cv2.destroyAllWindow() – destroys all window.

# Importing the OpenCV library 
import cv2 
import numpy as np

# Reading the image using imread() function 
image = cv2.imread('image.png') 

print(image)

cv2.imshow("Original Image",image)
cv2.waitKey()
cv2.destroyAllWindow()

OpenCV Image Shape

shape() returns 3 values- height, width and Dimension of the image.

h, w = image.shape[:2]

OpenCV Python RGB Color

RGB stands for Red, Green, and Blue.

# Extracting RGB values.  
# Computer stores the Color value in BGR Format
(B, G, R) = image[50, 100] 
  
# Displaying the pixel values 
print(f"R = {R}, G = {G}, B = {B}") 

You can access a pixel value by its row and column coordinates. For the BGR image, it returns an array of Blue, Green, Red values.

For a grayscale image, just corresponding intensity is returned.

>>> px = img[100,100]
>>> print(px)
[157 166 200]

# accessing only blue pixel
>>> blue = img[100,100,0]
>>> print(blue)
157

Region of Interest (ROI)

Region of Interest is the particular area in an image where you want to certain manipulations and don’t want to interrupt the rest of the image area.

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

OpenCV Resize

OpenCV resize() function takes 2 parameters-

  1. The image on which resize function will work.
  2. Size in form of tuple i.e width and height Ex- (512,512)
# resize() function takes 2 parameters,  
# the image and the dimensions 
resized_image = cv2.resize(image, (512, 512))
# 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) 

OpenCV Rotate

You must have done this. You post pictures on social media and sometimes rotate it till you get the perfect angle. This is how it’s done.

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

Geometric Shapes

Drawing shapes on images is one of the important features of OpenCV. You can draw Rectang, Square, Circle, Triangle or any Polygon, Arrows, and lines.

Drawing shapes on the image are widely used in OpenCV. In Face Recognition, the rectangle is drawn on the face of the person to show that the face is detected/recognized.

In Object Detection, a rectangle area is marked and the objects are detected when they are placed in that rectangle area.

# Copy of original Image
output = image.copy() 
  
# rectangle() function to create a rectangle. 
rectangle = cv2.rectangle(output, (1500, 900),  
                          (600, 400), (255, 0, 0), 2)
# Draw Circle
circle = cv2.circle(output, (100,100), 5, (0,255,0) 2)

parameters for Rectangle

  • Image
  • Top-left corner co-ordinates
  • Bottom-right corner co-ordinates
  • Color (in BGR format)
  • Thickness (-1 to fill the rectangle with the selected color)

Parameters for Circle

  • Image
  • Co-ordinate of center
  • Radius
  • Color (in BGR format)
  • Thickness (-1 to fill the Circle with the selected color )

Display Text

Displaying text on the image is again an important feature of the OpenCV.

This feature is used in Face Recognition and Object Detection to label the Face with the person’s name and to label the detected object.

This feature is widely used in CCTV cameras to display timestamp and video recording status on the videos.

It takes in 7 parameters –

  1. Image
  2. Text to be displayed
  3. Bottom-left corner co-ordinates
  4. Font
  5. Font size
  6. Color (BGR format)
  7. Thickness (-1 to fill with the selected color )
# Copying the original image 
output = image.copy() 
  
# Adding the text using putText() function 
text = cv2.putText(output, 'OpenCV DS', (500, 550),  
                   cv2.FONT_HERSHEY_SIMPLEX, 4, (0, 255, 0), 2) 

What cv2.waitKey() in OpenCV Python does?

cv2.waitKey() takes time in seconds as an argument, when not specified anything it takes 0 as the default value and waits for the user to end the cv2.imshow() task.

What is Region Of Interest (ROI)?

Region of Interest is the particular area in an image where you want to certain manipulations and don’t want to interrupt the rest of the image area.

Displaying text in OpenCV Python.

cv2.putText() takes in 7 parameters –
1. Image
2. Text to be displayed
3. Bottom-left corner co-ordinates
4. Font
5. Font size
6. Color (BGR format)
7. Thickness (-1 to fill with the selected color )

text = cv2.putText(output, ‘OpenCV DS’, (500, 550),
cv2.FONT_HERSHEY_SIMPLEX, 4, (0, 255, 0), 2)

Image Resizing in OpenCV Python

resize() function takes 2 parameters,
1. The image and
2. The dimensions
resized_image = cv2.resize(image, (512, 512))

Tags: , ,

Leave a Reply

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