-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjectTrackingViaHomogrphy.py
148 lines (109 loc) · 3.68 KB
/
objectTrackingViaHomogrphy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import numpy as np
import cv2
'''VARIABLES TO SELECT OBJECT <RETANGULAR REGION>'''
x1 = -1
x2 = -1
y1 = -1
y2 = -1
drawing = False
'''MOUSE CALLBACK TO SELECT FOUR CORNER OF THE OBJECT'''
def mouseCallBack(action,x,y,flags,userData):
global x1,x2,y1,y2,drawing,firstFrame,frameCopy
if action == cv2.EVENT_LBUTTONDOWN:
drawing = True
x1 = x
y1 = y
elif action == cv2.EVENT_MOUSEMOVE:
if drawing:
frameCopy = firstFrame.copy()
cv2.rectangle(frameCopy,(x1,y1),(x,y),(255,0,0),1)
elif action == cv2.EVENT_LBUTTONUP:
drawing = False
frameCopy = firstFrame.copy()
x2= x
y2= y
cv2.rectangle(frameCopy,(x1,y1),(x2,y2),(0,255,0),1)
'''
FUNCTION TO ALLOW USER TO SELECT OBJECT FROM THE FRAME
'''
def selectObject():
global x1,x2,y1,y2,frameCopy,firstFrame
frameCopy = firstFrame.copy()
cv2.namedWindow('Select Object by drag mouse. Press c to clear,Enter to Continue')
cv2.setMouseCallback('Select Object by drag mouse. Press c to clear,Enter to Continue',mouseCallBack)
while True:
cv2.imshow('Select Object by drag mouse. Press c to clear,Enter to Continue',frameCopy)
k = cv2.waitKey(1) & 0xFF
if k == 13 :
break
elif k == ord('c'):
x1 = -1
x2 = -1
y1 = -1
y2 = -1
frameCopy = firstFrame.copy()
obj = firstFrame[y1:y2,x1:x2]
gObj = cv2.cvtColor(obj,cv2.COLOR_BGR2GRAY)
return obj,gObj
#capture frames from webcamera
cap = cv2.VideoCapture(0)
ret, firstFrame = cap.read()
frameCopy = firstFrame.copy()
#Selecting Object to be tracked
object,gObject = selectObject()
sift = cv2.xfeatures2d.SIFT_create()
objectKeypts, objectDesc = sift.detectAndCompute(gObject,None)#None=Mask
indexParams = dict(algorithm=0,trees=5)
searchParams = dict()
flann = cv2.FlannBasedMatcher(
indexParams,
searchParams
)
while True:
ret, frame = cap.read()
gFrame = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
gFrameKeypts,gFrameDesc = sift.detectAndCompute(gFrame,None)
###########################
### Visualize KeyPoints ###
###########################
"""
viewFrameKeypts = cv2.drawKeypoints(frame,gFrameKeypts,frame)
viewobjectKeypts = cv2.drawKeypoints(object,objectKeypts,object)
cv2.imshow('View Frame Key Points',viewFrameKeypts)
cv2.imshow('View Object Key Points',viewobjectKeypts)
"""
''' FEATURE MATCHING '''
matches = flann.knnMatch(objectDesc,gFrameDesc,k=2)
goodMatches = []
for a,b in matches:
if a.distance < b.distance * .8:
goodMatches.append(a)
matchImg = cv2.drawMatches(
gObject,
objectKeypts,
gFrame,
gFrameKeypts,
goodMatches,
gFrame
)
#Uncomment to see the matched features
#cv2.imshow('Matches',matchImg)
''' HOMOGRAPHY '''
if len(goodMatches) > 10 :
object_pts = np.float32([objectKeypts[p.queryIdx].pt for p in goodMatches]).reshape(-1,1,2)
frame_pts = np.float32([gFrameKeypts[p.trainIdx].pt for p in goodMatches]).reshape(-1,1,2)
matrix, mask = cv2.findHomography(object_pts,frame_pts,cv2.RANSAC,5.0)
matchesMask = mask.ravel().tolist()
h,w = gObject.shape
pts = np.float32([[0,0],[0,h],[w,h],[w,0]]).reshape(-1,1,2)
prep = cv2.perspectiveTransform(pts,matrix)
tracking = cv2.polylines(frame,[np.int32(prep)],True,(123,123,123),3)
cv2.imshow('Tracking',tracking)
else:
cv2.imshow('Tracking',frame)
k = cv2.waitKey(1)
#Break when Esc is pressed
if k==27:
break
cv2.destroyAllWindows()
cap.release()