Object Tracking Using OpenCV
We will learn Color based Object Tracking System
Introduction
Basic idea behind tracking object is find contours for object. Based on HSV color values.
Contours : Segment of Image which highlights object. For example if you apply binary threshold on image with (180,255) then pixels greater than 180 will be shown white highlighted and other is black. This white part is called contour.
Before proceeding install OpenCV in your system. Open Command Prompt and type
pip install opencv-python
Steps:
step1 : Read Frame from Camera
import cv2
cam = cv2.VideoCapture(0)
img = cam.read()[1] #_, img = cam.read()
Argument 0- for primary camera, example in laptop webcam is considered as primary camera. 1- for secondary and so on.
Step2: Preprocessing Frame
- Use Gaussian Filter to normalize image. Normalizing an image may cause lose of many small information but we need to Normalize/Blur our image so that our object get equally distribution of color.
#cv2.gaussianBlur(source_image, Kernal_size, Border_width)
Blur_img = cv2.GaussianBlur(img,(11,11),0)
2. Convert image into HSV color model.
HSV = cv2.cvtColor(Blur_img, cv2.COLOR_BGR2HSV)
Step3: Find HSV Color for corresponding Object.
I suggest you to go through following link CLICK HERE. From this link you will come to know about Low HSV and High HSV values for your object.
Obj_low = np.array([0,0,0]) # In my case (H,S,V)
Obj_high = np.array([179,157,79])
Step4: Thresholding
Apply Binary Thresholding in given range of HSV value. B/W Obj_low & Obj_high.
MASK = cv2.inRange(HSV, Obj_low, Obj_high)
#MASK2 = cv2.inRange(HSV, Obj2_low, Obj2_high)
if you try to track two different objects you need to create 2 different masks and at final use ‘bitwise And’ operator on both masks.
#mask = cv2.bitwise_and(mask1, mask2)
Erosion & Dilation:
Erosion and dilation fills black and white spots in threshold image. This make image more clear, smooth and highlights main objects.
MASK = cv2.erode(MASK1, None, iterations=2)
MASK = cv2.dilate(MASK1, None, iterations =2)
Step5 : Find Contours in image
Contours : Segment of Image which highlights object. For example if you apply binary threshold on image with (180,255) then pixels greater than 180 will be shown white highlighted and other is black. This white part is called contour.
cnts = cv2.findContours(MASK1.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]if len(cnts)>0 :
c = max(cnts, key = cv2.contourArea)
In above given image whole white bounded area is contours. There may be more than one contours but area of our main object would be maximum. so choose contour with maximum. and Then..
Step6: Draw Circle on object
After getting contour of main object draw a circle on contour .
((x,y), radius) = cv2.minEnclosingCircle(c)
M = cv2.moments(c)
center = (int(M[‘m10’]/ M[‘m00’]), int(M[‘m01’]/ M[‘m00’]) )
cv2.circle(img, center, 5, (0,0,255), -1)
cv2.circle(img, center, int(radius), (0,0,255), 2)
In last put a Loop on camera…
Find following Code for Object tracking program
import cv2
import numpy as npcam = cv2.VideoCapture(0)
Obj_low = np.array([0,0,0])
Obj_high = np.array([179,157,79])while True:
img = cam.read()[1]
img = cv2.resize(img, (800,600) )
blur_img = cv2.GaussianBlur(img,(21,21),0)
HSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)MASK1 = cv2.inRange(HSV, Obj_low, Obj_high)
MASK1 = cv2.erode(MASK1, None, iterations=2)
MASK1 = cv2.dilate(MASK1, None, iterations =2)
cnts = cv2.findContours(MASK1.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
center = None
if len(cnts)>0 :
c = max(cnts, key = cv2.contourArea)
((x,y), radius) = cv2.minEnclosingCircle(c)
M = cv2.moments(c)
center = (int(M['m10']/ M['m00']), int(M['m01']/ M['m00']) )
if radius>10:
cv2.circle(img, center, 5, (0,0,255), -1)
cv2.circle(img, center, int(radius), (0,0,255), 2)
cv2.imshow("my window",img)
k = cv2.waitKey(1)
if k==27:
break
cam.release()
cv2.destroyAllWindows()
Thanks for reading:)
see me on LinkedIn https://www.linkedin.com/in/naurat-singh-rawat-072888148/