Advertisement
Object tracking is highlighting a particular object in a live video or pre-recorded video or Image in OpenCV Python. In this blog, we are going to make an Object tracking application in OpenCV Python.
Let’s Code Object Tracking in OpenCV Python!
Steps for Object Tracking/Detection Using OpenCV
- Make a trackbar in OpenCV for controlling the color in making the mask.
- Get the trackbar values.
- Create Mask for object detection or tracking.
- Use bitwise_and for Object Tracking in OpenCV.
If you have any kind of problem in OpenCV image manipulation, click and read, as we will be using those functions in Object Detection.
Object Tracking Algorithm
Import packages
Import Computer Vision Python package for image manipulation.
NumPy for working with image matrix and matrix manipulation.
Matplotlib for data visualization and for displaying two or more images in same window.
BGR to HSV
Convert the Image or the video frame from BGR to HSV color.
hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
Callback Function
A callback function is made, which will do nothing extra but just take an argument and will print it on the terminal or just pass it.
Creating Trackbar in OpenCV
Next step is to create a Trackbar in the OpenCV Window, that will help us to change color of the window/mask using the sliders.
We will make 6 Trackbars/sliders namely HSV for Hue, Saturation, and Value color scheme.
There will be 3 sliders for lower HSV values and 3 sliders for higher HSV values.
The sliders for lower HSV will have a value ranging from 0 to 255 i.e lowest color intensity to the highest color intensity.
The High HSV value sliders will have the lowest as well as the highest value as 255.
All the three trackbars/sliders will reside on the same-named window.
createTrackbar() function takes five parameters:
- Name of the Trackbar.
- Name of the window where it will reside.
- The lowest value for the trackbar.
- The Highest Value for the trackbar.
- A Callback function.
Callback function is required in createTrackbar() function. So, we have created a callback function earlier, which does nothing but just takes an argument, either print the value on the terminal or pass.
Get Trackbar Values
Next Step is to collect all the values from the trackbars.
We will collect all the slider values seperately.
Create Lower and Upper Color range using NumPy
Store the lower and Upper HSV values of the Trackbar seperately.
Create lower color range using:
l_c = np.array([l_h, l_s, l_v])
Also, Create Upper Color Range.
u_c = np.array([u_h, u_s, u_v])
Create Mask for Object Tracking
A mask is created using cv.inRange() function.
mask = cv.inRange(hsv, l_c, u_c)
inRange() function takes three parameters:
- The HSV converted image or Video Frame.
- Lower Color Range Created using Lower HSV trackbar’s values.
- Upper Color Range Created using Upper HSV trackbar’s values.
Apply Mask on the image
res = cv.bitwise_and(img, img, mask=mask)
Mask is applied on the Image using the mask created using inRange() function.
Displaying Object Tracking Image
After using the bitwise_and() function on the image and applying mask the object is tracked.
Show the tracked image on the named window.
You can change the slider values to track the object which you want to track.
Object Tracking OpenCV Source Code
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
def change(x):
# print(x)
pass
def objectDetection():
cv.namedWindow("Tracking")
cv.createTrackbar("LH", "Tracking", 0, 255, change)
cv.createTrackbar("LS", "Tracking", 0, 255, change)
cv.createTrackbar("LV", "Tracking", 0, 255, change)
cv.createTrackbar("UH", "Tracking", 255, 255, change)
cv.createTrackbar("US", "Tracking", 255, 255, change)
cv.createTrackbar("UV", "Tracking", 255, 255, change)
img = cv.imread('./img/balls.jpg')
# for Video capture and object detection
# cap = cv.VideoCapture(0)
while True:
# _, img = cap.read()
hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
l_h = cv.getTrackbarPos("LH", "Tracking")
l_s = cv.getTrackbarPos("LS", "Tracking")
l_v = cv.getTrackbarPos("LV", "Tracking")
u_h = cv.getTrackbarPos("UH", "Tracking")
u_s = cv.getTrackbarPos("US", "Tracking")
u_v = cv.getTrackbarPos("UV", "Tracking")
l_c = np.array([l_h, l_s, l_v])
u_c = np.array([u_h, u_s, u_v])
# print(l_c)
# print(u_c)
# u_b = np.array([130, 255, 255])
# l_b = np.array([110, 50, 50])
mask = cv.inRange(hsv, l_c, u_c)
res = cv.bitwise_and(img, img, mask=mask)
cv.imshow("Object Detection", res)
cv.imshow("mask", mask)
cv.imshow("Original Image", img)
k = cv.waitKey(1) & 0xFF
if k == 27:
print(l_c)
print(u_c)
break
# cap.release()
cv.destroyAllWindows()
if __name__ == '__main__':
objectDetection()
OpenCV Documentation
Learn more about the OpenCV functions and Image manipulation techniques from the official OpenCV Documentation.