Object Tracking
-Akshat
Object tracking is the process of locating a moving object (or multiple objects) over time using a camera. It has a variety of uses, some of which are: human-computer interaction, security and surveillance, augmented reality, traffic control, medical imaging and video editing.
Akshat
![](http://stech3.firstpost.com/tech2images/640x359/proportional/jpeg/gallery/2011/mar/img_306052_hawkeye_main1_102005242001.jpg)
![](http://cmp.felk.cvut.cz/data/motorway/images/4lanesOneTargetHiddenLS.jpg)
Some Day2Day Examples
Akshat
It seems interesting
?
Akshat
so for next 80 mins..
a short introduction on video capture
describe the object to be tracked
contour extraction
sketch the frame
Akshat
![](https://s3.amazonaws.com/media-p.slid.es/uploads/749239/images/4266888/Screen_Shot_2017-10-16_at_10.48.24_AM.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/749239/images/4266892/Screen_Shot_2017-10-26_at_5.47.48_PM.png)
Some
Results
Akshat
before we do anything
import cv2
import numpy as np
Akshat
let' s capture a video
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while True:
# Let's capture/read the frames for the video
_, frame = cap.read()
# This frame captured is flipped, Lets's get the mirror effect
frame = cv2.flip(frame, 1)
# Any operations on the frame captured will be processed in the operate(frame) method
final_frame = operate(frame)
# Showing the captured frame
cv2.imshow('garrix', frame)
cv2.imshow('martin', final_frame)
# Continuous, Large amount of frames produce a video
# waitkey(value), value is the ammount of millisecs a frame must be displayed
# 0xff represents the ASCII value for the key, 27 is for ESC
# waitKey is necessary to show a frame
if cv2.waitKey(1) & 0xff == 27:
break
cap.release()
cv2.destroyAllWindows()
Akshat
let' s describe the object
# Info : ZOOM in the frame to get the RGB value of the corresponding pixel
# The range of color for the object to be detected in HSV
hsv_supremum = np.array([172, 221, 255])
hsv_infinum = np.array([150, 40, 130])
def operate(frame):
frame_copy = frame.copy()
frame_ret = frame.copy()
# Converting the frame from BGR format to HSV format
frame_copy = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Thresholding the frame to get our object(color) tracked
mask = cv2.inRange(frame_copy, hsv_infinum, hsv_supremum)
mask = cv2.medianBlur(mask, 5)
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
#cv2.imshow('mask', mask)
# Obtaining the binary Image for the corresponding mask
res = cv2.bitwise_and(frame_ret,frame_ret, mask= mask)
#cv2.imshow('res', res)
# The result can be improved by smoothening the frame
# To be contd...
Akshat
my object
Akshat
![](https://s3.amazonaws.com/media-p.slid.es/uploads/749239/images/4266744/hsv1.jpeg)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/749239/images/4266745/hsv2.jpeg)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/749239/images/4266743/WhatsApp_Image_2017-10-26_at_13.20.44.jpeg)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/749239/images/4266740/WhatsApp_Image_2017-10-26_at_13.20.44-2.jpeg)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/749239/images/4266739/WhatsApp_Image_2017-10-26_at_13.20.44-3.jpeg)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/749239/images/4266738/WhatsApp_Image_2017-10-26_at_13.20.44-4.jpeg)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/749239/images/4266742/WhatsApp_Image_2017-10-26_at_13.20.43.jpeg)
object configuration
![](https://s3.amazonaws.com/media-p.slid.es/uploads/749239/images/4265878/final_data.png)
Akshat
object configuration
hsv_supremum = np.array([172, 221, 255])
hsv_infinum = np.array([150, 40, 130])
def operate(frame):
# Some code above
# Converting the frame from BGR format to HSV format, Hue Saturation Value GOOGle for more
frame_copy = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Thresholding the frame to get our object(color) tracked
mask = cv2.inRange(frame_copy, hsv_infinum, hsv_supremum)
# Some code below
Akshat
object configuration
# In the operate function
mask = cv2.medianBlur(mask, 5)
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
# The result can be further improved by smoothening the frame
# Issues:
# Lightning
# Variable Contour
operations to enhance the object
Akshat
dilation
Akshat
![](https://s3.amazonaws.com/media-p.slid.es/uploads/749239/images/4270337/Screen_Shot_2017-10-27_at_3.55.46_PM.png)
source UDACITY
erosion
Akshat
![](https://s3.amazonaws.com/media-p.slid.es/uploads/749239/images/4270338/Screen_Shot_2017-10-27_at_3.55.30_PM.png)
source UDACITY
![](https://s3.amazonaws.com/media-p.slid.es/uploads/749239/images/4266889/Screen_Shot_2017-10-16_at_10.49.36_AM.png)
Results After Dilation & Erosion in the binary Image
Akshat
contour extraction
# In the same operate function
mask_copy = mask.copy()
cnts = cv2.findContours(mask.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(frame_ret, (int(x), int(y)), int(radius), (0, 150, 255), 2)
cv2.circle(frame_ret, center, 5, (0, 0, 255), -1)
operations to extract the contour
Akshat
![](https://s3.amazonaws.com/media-p.slid.es/uploads/749239/images/4266891/Screen_Shot_2017-10-16_at_10.51.43_AM.png)
Object Contour Extracted
Object Mask Extracted
Video Capture
Akshat
sketch the frame
import cv2
import numpy as np
from collections import deque # <--
pts = deque() # <--
cap = cv2.VideoCapture(0)
# In the operate function
pts.appendleft(center)
#print(pts[0])
for i in range(1 , len(pts)):
if pts[i-1] is None or pts[i] is None:
continue
thickness = 6
cv2.line(frame_ret, pts[i-1], pts[i], (0, 0, 255), thickness)
return frame_ret
operations to sketch on the frame
Akshat
![](https://s3.amazonaws.com/media-p.slid.es/uploads/749239/images/4266893/Screen_Shot_2017-10-26_at_5.48.44_PM.png)
Sample Sketch
Akshat
![](https://s3.amazonaws.com/media-p.slid.es/uploads/749239/images/4266895/Screen_Shot_2017-10-26_at_5.55.02_PM.png)
Sample Sketch
Akshat
Code
Implementation
Lets Begin
[Quest]ions
?
Thank you
Akshat Sharma
Sophomore
Phoenix | CV Last Lecture
By Akshat Sharma
Phoenix | CV Last Lecture
Project Based session on Computer Vision, covering Object Tracking & Webcam Drawing.
- 403