Getting started with Computer Vision using OpenCV

Kunal Chhikara
4 min readJun 14, 2021

What is Computer Vision?

Computer Vision is a multi-disciplinary field that focuses on how computers can gain higher understanding from digital images / videos. This is an attempt to automate the activities performed by the human visual system. This is the process of discovering, processing, analyzing, and understanding digital images, and extracting high-quality data in the real world.

Some real world applications of computer vision include —

Self Driving Cars — Computer vision is needed to enable self-driving cars. Manufacturers such as Tesla, BMW, Volvo, and Audi use multiple cameras, lidar, radar, and ultrasonic sensors to capture images from nature so that self-driving vehicles can detect objects, line markings, signs and road signs to drive safely.

Travel and Delivery apps — In this pandemic time many companies such as Uber Eats, Door Dash, Menu log etc. have incorporated the mask detection feature in their apps. The application opens after it has detected whether the user is wearing mask or not.

Real Time sports tracking — In sports like Cricket, Baseball , Football, Basketball object detection is performed on the ball. The focus of the camera automatically shifts where the ball is going. Moreover, computer vision is also helping play and strategy analysis, player performance and ratings.

Healthcare — With 90 percent of all medical imaging information, there is a wide range of computer use in medical practice. From empowering new medical diagnostic techniques to X-ray analysis, mammography and other scans to identify problems and assist with surgery, expect our medical facilities and specialists and patients to benefit from computer vision today and beyond in the future.

and much more ….

What is OpenCV ?

OpenCV is a large library of open source computer vision, machine learning, and image processing. By using it, one can process photos and videos to identify objects, faces, or handwriting.

Installing OpenCV

We can install the OpenCV2 using the terminal as follows:

pip install opencv-python 

Importing OpenCV in IDE

import cv2
cv2.__version__
'4.5.2'

Basics of OpenCV

Reading an Image

To read an image using openCV we need to use the inbuilt function imread()

img = cv2.imread(r'C:\Users\Hp\Desktop\nature.jpg',1)

After specifying the image path we can pass the value for flag. The flag can take three values:

1— To load a color image.

0 — To load a grayscale image.

1— To load image with an alpha channel.

Displaying an Image

To display an image using openCV we need to use the inbuilt function imshow().It displays an image in a window which fits itself to the size of the image. The first argument is the window name- a string; the second is the image.

cv2.imshow('image',img)
cv2.waitKey(0)

waitKey(0) will display the window infinitely until any keypress.

Drawing a line

We can draw a line using the line() method. This takes five arguments. The first is the image on which we need to draw, followed by the starting and ending co-ordinates of the line , followed by line color and line thickness.

img_black = np.zeros((512,512,3),np.uint8)
cv2.line(img_black,(0,0),(511,511),(255,0,0),5)

Drawing a rectangle

Similar to line we can also draw a rectangle using the rectangle() method. It takes the co-ordinates of top left corner and bottom right corner.

cv2.rectangle(img_black,(100,320),(400,128),(0,255,0),3)

Adding text to images

We can use the putText() method to display text over an image. The first parameter is the image, followed by the text that needs to be displayed, co-ordinates of the bottom left corner of the text string, the font type, font scale, color, thickness, line type(optional), bottom left origin(optional).

cv2.putText(image, 'OpenCV', org, font, fontScale, color, thickness, cv2.LINE_AA)

--

--