OpenCV- A supreme tool for
Computer Vision

Aman Rangapur
The Art of Data
Published in
5 min readAug 13, 2020

What is Computer Vision?

Computer Vision is like imparting human intelligence and instincts to a
computer. In other words it is a field of computer science that works on
enabling computers to see, identify and process images in the same way
that the human eye does.
Let us say you and your family went on a vacation and you uploaded a few
pictures on Facebook. However, it takes time to find your parents’ faces
and tag them in each picture, Facebook is intelligent enough to tag people
for you.But, how do you think this auto-tag feature works? It works through
computer vision.

What is OpenCV?

OpenCV was built to provide a common infrastructure for computer vision
applications and to accelerate the use of machine perception in commercial
products. This library has more than 2500 algorithms used to detect and
recognize human faces, identify images, track moving objects, extract 3D
models of objects.

Installation of OpenCV

To install OpenCV for python use the following code in terminal:

$ python3 -m pip install opencv-python
$ python3 -m pip install opencv-contrib-python

How does a computer read an image?

Click from New York Square

We can look at this image and figure it out that it belongs to New York
Square. But, computers cannot analyze it. It doesn’t have any intelligence.
For any color image, there are 3 primary channels — red, green and blue.
A matrix is formed for every primary color, and later, these matrices
combine to provide a pixel value for the individual R, G, and B colors. Each
element of the matrices provides data pertaining to the intensity of brightness of the pixel. It reads any image as a range of values between 0
and 255.

How to capture images and videos through a camera?

import cv2

cap = cv2.VideoCapture(0)

while True:

ret, frame = cap.read()

cv2.imshow(“Capturing”,frame)

if cv2.waitKey(1) & 0xFF == ord(‘q’):

break

cap.release()

cv2.destroyAllWindows()

As seen in the above piece of code, we need to import the OpenCV library, cv2.VideoCapture() triggers the camera, cv2.imshow shows what the camera is capturing by opening a new window. cv2.waitKey makes the window static until the user presses a key.

Basic functions of OpenCV

Loading images using OpenCV and converting it into GrayScale:

import numpy as np

import cv2

img = cv2.imread(‘Image123.png’,0) #write the name of an image

cv2.imshow(‘image’,img)

k = cv2.waitKey(0) & 0xFF

if k == 27: # wait for ESC key to exit

cv2.destroyAllWindows()

elif k == ord(‘s’): # wait for ‘s’ key to save and exit

cv2.imwrite(‘Firefox_wallpapergray.png’,img)

cv2.destroyAllWindows()

cv2.imread reads the selected image and the 0 parameter turns the image into grayscale, cv2.imshow shows the converted image.

Drawing and writing on an Image:

import numpy as np

import cv2

img = cv2.imread(‘black.jpg’,cv2.IMREAD_COLOR)

cv2.line(img,(0,0),(511,511),(255,0,0),5)

cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)

cv2.circle(img,(447,63), 63, (0,0,255), -1)

font = cv2.FONT_HERSHEY_SIMPLEX

cv2.putText(img,’OpenCV’,(10,500), font, 4,(255,255,255),2,cv2.LINE_AA)

cv2.imshow(‘image’,img)

cv2.waitKey(0)

cv2.destroyAllWindows()

cv2.line draws the line with given coordinates on the image, cv2.rectangle, cv2.circle draws a rectangle and circle respectively and cv2.putText writes the given text, here we have used Hershey Simpex font.

Output:

Output after drawing and writing on an image.

Feature and Template Matching:

Template matching is basically the portion of one image matching another image.

import cv2

import numpy as np

img_bgr=cv2.imread(‘sc1.png’)

img_gray=cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)

template=cv2.imread(‘sc2.png’,0)

w,h=template.shape[::-1]

res=cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)

threshold=0.8

loc=np.where(res>=threshold)

for pt in zip(*loc[::-1]):

cv2.rectangle(img_bgr, pt, (pt[0]+w, pt[1]+h), (0,255,255),2)

cv2.imshow(‘detected’,img_bgr)

cv2.waitKey()

cv2.imread reads the selected image, cv2.cvtColor converts the image into Grayscale, w and h variables are the positions of x and y-axis, cv2.matchTemplate helps to match the common area of two images with threshold 80%, cv2.rectangle marks the area which is matching in the image.

Output:

Output of Template matching.

Gradients and Edge Detection:

import cv2

import numpy as np

cap=cv2.VideoCapture(0)

while True:

_, frame=cap.read()

a= cv2.Laplacian(frame,cv2.CV_64F)

x= cv2.Sobel(frame,cv2.CV_64F,1 ,0, ksize=5)

y= cv2.Sobel(frame,cv2.CV_64F,0 ,1, ksize=5)

edge= cv2.Canny(frame, 100, 200)

cv2.imshow(‘original’,frame)

cv2.imshow(‘laplacian’,a)

cv2.imshow(‘sobelx’,x)

cv2.imshow(‘sobely’,y)

cv2.imshow(‘edge’,edge)

k=cv2.waitKey(5) & 0xFF

if k == 27:

break

cv2.destroyAllWindows()

cap.release()

cv2.Laplacian converts the image into the gradient, we use cv2.CV_64F as a standard label, cv2.Sobel converts into a horizontal and vertical gradient.

Output:

Laplacian
Vertical Gradient
Edge Detection
Horizontal Gradient
Fingerprint matching and Edge detection of embryo from mother’s womb.

Edge detection is pervasive in many applications such as fingerprint matching, medical diagnosis & license plate detection. These applications basically highlight the areas where image intensity changes drastically and ignore everything else.

Edge Detection is also used in Self Driving Cars for lane detection:

Land detection of roads using OpenCV.

Other features of OpenCV: Motion Detection, Intrusion Detection, Haar Cascades, MOG background reduction, Homography, Corner Detection, Color Filtering, Thresholding, Image Arithmetics etc..

Statistical machine learning libraries used by OpenCV: Naive Bayes classifier, K-nearest neighbour algorithm, Decision tree learning, Meta- Algorithm, Random forest, Support vector machine, Deep & Convolutional neural networks.

Some popular applications of OpenCV:

Driver drowsiness detection (using a camera in a car) alerts the car driver through buzz or alarm.

Vehicle counting on highways (can be segregated into buses, cars, trucks) along with their speeds.

Anomaly detection in the manufacturing process (the odd defective products).

The ANPR called automatic number plate recognition is used to control vehicle tracing and counting number of people.

OpenCV is also used in imaging the data and provides better prediction and treatment to diseases like blood flow, x-ray images that are interpreted by humans.

--

--