Basics of OpenCV, Part 1

Prathyash Joy Binu
3 min readAug 9, 2023

--

In this article, we will be familiarizing the basic functions of OpenCV while handling images and video.

OpenCV is the huge open-source library for computer vision, machine learning, and image processing and now it plays a major role in real-time operation which is very important in today’s systems. By using it, one can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrated with various libraries, such as NumPy, python is capable of processing the OpenCV array structure for analysis. To Identify an image pattern and its various features we use vector space and perform mathematical operations on these features.

Application

These are some common applications of OpenCV currently used in various sectors

  1. Object detection & Counting
  2. Edge Detection
  3. Number plate recognition
  4. Optical character recognition
  5. Inspection and surveillances
  6. Pattern matching

Basics

Let's work on the basic function like visualization, saving, rescaling,buring resizing, etc. while dealing with images and videos.

1. Image Visualization and Saving

For visualizing the image use the below code to display. Change the file path according to your need.

import cv2 as cv

img = cv.imread("cat.png")
cv.imshow('Cat', img)

#save image as a file

cv.imwrite('image.jpg',img)

The above code will open a window showing the image you want to display and also save. In the Save function, we need to specify two arguments

  1. Name of the file you need to save
  2. Source of the image

2. Video Visualization and Saving

For reading the video, we need to use the code below to read the frames one by one.

import cv2 

# video writer configuration
fourcc = cv2.VideoWriter_fourcc(*"XVID")
out = cv2.VideoWriter('output.avi', fourcc,20.0,(600,600))


cap =cv2.VideoCapture(0)#can also add the video files by specifying the path

while (True):

ret, frame = cap.read()
if ret == True:
cv2.imshow("video capture", frame)

#save video capture
out.write(frame)

if cv2.waitKey(1) & 0xFF == ord('q'):
break

else:
break


cap.release()
cv2.destroyAllWindows()

The above code will read the visuals from the camera and save them to the specified location.

Till now we got the idea of visualizing and saving images and videos. Before moving to further steps. Now let us create a reusable function for visualizing the various action like blurring, resizing, drawing, etc. Here the image is displayed using matplotlib.

# Creating a Reusable function

With the help of the below function, we can visualize the output of the open cv function that here we use.

import cv2
import matplotlib.pyplot as plt

def imshow(title = "Image", image = None ,size =10):
w, h = image.shape[0], image.shape[1]
asp_rations =w/h

plt.figure(figsize=(size*asp_rations,size))
plt.imshow(image)
plt.title(title)
plt.show()

3. Gray Scaling an RGB Image

Gray scaling is a method that is used to convert an RGB image to a Black and White image with channel 1. Before gray scaling the image, we need to open it using cv2.imread. After that follow the code below mentioned

image = cv2.imread("Image.jpg") 
gray_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
imshow("Test Image",gray_image)

4. Draw Over Images



#Draw a Rectangle
image = cv2.imread("Image.jpg")
cv2.rectangle(image,(0,0),(200,200),(100,254,255),4)
imshow("Color Test",image)

# 3. Draw A Line
image = cv2.imread("Image.jpg")
cv2.line(image,(0,0),(200,200),(100,254,255),4)
imshow("Color Test",image)

# 4. Draw a Polyline
image = cv2.imread("Image.jpg")
pts = np.array([[100,100],[300,300],[570,370]],np.int32)
cv2.polylines(image,[pts],True,(255,4,234),4)
imshow("Polygon Test",image)

# 5. Write text
image = cv2.imread("Image.jpg")
cv.putText(image, 'Hello', (0,225), cv.FONT_HERSHEY_TRIPLEX, 1.0, (0,255,0), 2)
cv.imshow('Text', image)

5. Image Rotation

In this code the image can be rotated in any direction.

image = cv2.imread("Image.jpg")
w, h =image.shape[:2]
rotate = cv2.getRotationMatrix2D((w/2,h/2),90,1)
rotated_image = cv2.warpAffine(image,rotate,(w,h))
imshow("Rotate Test",rotated_image)

Another method of rotating the image is using transpose function.

image = cv2.imread("Image.jpg")
cv2.transpose(image)
imshow("Rotate Test",image)

6. Image Resizing

Using image resizing function we can change the dimension of the input image to desired dimension.

image = cv2.imread("Image.jpg")
# double the size of the image
cv2.resize(image, None, fx=2,fy=2, interpolation= cv2.INTER_CUBIC)

# exact dimension
cv2.resize(image,(400,400),interpolation= cv2.INTER_CUBIC)

7. Cropping Image

In some situation, we need to crop the image, using the snippet below we can crop the image.

image = cv2.imread("Image.jpg")

crop = image[50:180, 100:300]
imshow("Crop",crop)

Above snippets are the basic function that we use to handle with images and videos. In the coming article, we will be having detailed explanation of other advanced function.

--

--

Prathyash Joy Binu

AI/ ML Engineer @ Reflections Infos Systems , R&D Enthusiast, Product Development,