"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

February 14, 2019

Day #212 - OpenCV based Object Tracking Learning's


#https://docs.opencv.org/3.1.0/db/df8/tutorial_py_meanshift.html
#Yolo + Meanshift - OpenCV
#Write comment for each line that you don't understand :)
import numpy as np
import cv2
cap = cv2.VideoCapture(r'E:\Optical_Flow\slow.flv')
ret, frame = cap.read()
#frame = cv2.resize(old_frame, (500, 400))
#set up initial location window
r,h,c,w = 250,90,400,125 #assign it based on Yolo
track_window = (c,r,w,h)
#set up roi for tracking
roi = frame[r:r+h,c:c+w]
#Converts an image from one color space to another.
hsv_roi = cv2.cvtColor(roi,cv2.COLOR_BGR2HSV)
#The cv2.inRange - three arguments
#first is the image to perform color detection
#second - lower limit of the color you want to detect
#third argument - upper limit of the color you want to detect.
mask = cv2.inRange(hsv_roi,np.array((0.,60.,32.)),np.array((180.,255.,255.)))
#cv2.calcHist to calculate the histogram of an image
#cv2.calcHist(images, channels, mask, bins, ranges)
#For grayscale images as there's only one channel and [0], [1] or [2]
#bins - is a list containing the number of bins to use for each channel
#ranges - is the range of the possibile pixel values which is [0, 256] in case of RGB color space (where 256 is not inclusive).
roi_hist = cv2.calcHist([hsv_roi],[0],mask,[180],[0,180])
#its normalized values will be R/S, G/S and B/S (where, S=R+G+B).
#when normType=NORM_MINMAX (for dense arrays only).
#The optional mask specifies a sub-array to be normalized.
#This means that the norm or min-n-max are calculated over the sub-array, and then this sub-array is modified to be normalized
cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)
#Set up Termination Criteria either 10 iteration or move by atlease 1 pt
term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT,10,1)
while(1):
ret, frame = cap.read()
if ret == True:
hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
#Calculates the back projection of a histogram.
#images, channels, hist, ranges, scale[, dst]
dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1)
#apply mean shift to get the new location
#move that window to the area of maximum pixel density (or maximum number of points)
#the movement is reflected in histogram backprojected image.
#As a result, meanshift algorithm moves our window to the new location with maximum density
ret, track_window = cv2.meanShift(dst,track_window,term_crit)
#draw it on image00000000
x,y,w,h = track_window
img2 = cv2.rectangle(frame,(x,y),(x+w,y+h),255,2)
cv2.imshow('img2',img2)
k = cv2.waitKey(0) & 0xff
if k==27:
break
else:
break
cv2.destroyAllWindows()
cap.release()
view raw py_meanshift.py hosted with ❤ by GitHub
Happy Mastering DL!!!

No comments: