-
Notifications
You must be signed in to change notification settings - Fork 6
/
camshift.py
117 lines (103 loc) · 2.88 KB
/
camshift.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
import numpy as np
import cv2
from matplotlib import pyplot as plt
import matplotlib
cap = cv2.VideoCapture('trial2.mp4')
ret,frame = cap.read()
print(ret)
refPt = []
cropping = False
image = frame
def click_and_crop(event, x, y, flags, param):
global refPt, cropping
if event == cv2.EVENT_LBUTTONDOWN:
refPt = [(x, y)]
cropping = True
elif event == cv2.EVENT_LBUTTONUP:
refPt.append((x, y))
cropping = False
cv2.rectangle(image, refPt[0], refPt[1], (0, 255, 0), 2)
cv2.imshow("image", image)
clone = image.copy()
cv2.namedWindow("image")
cv2.setMouseCallback("image", click_and_crop)
while True:
# display the image and wait for a keypress
cv2.imshow("image", image)
key = cv2.waitKey(1) & 0xFF
# if the 'c' key is pressed, break from the loop
if key == ord("c"):
break
if len(refPt) == 2:
roi = clone[refPt[0][1]:refPt[1][1], refPt[0][0]:refPt[1][0]]
cv2.imshow("ROI", roi)
cv2.waitKey(0)
x1,y1=refPt[0]
x2,y2=refPt[1]
c=x1
r=y1
w=x2-x1
h=y2-y1
roi1=cv2.cvtColor(roi,cv2.COLOR_BGR2HSV)
kernel = np.ones((5,5),np.uint8)
#roi1 = cv2.erode(roi1,kernel,iterations = 1)
#cv2.imshow("eroded",roi1)
blue = cv2.calcHist([roi1],[0],None,[180],[0,180])
green = cv2.calcHist([roi1],[1],None,[256],[0,256])
red = cv2.calcHist([roi1],[2],None,[256],[0,256])
plt.plot(blue)
plt.plot(green)
plt.plot(red)
plt.show()
bluey, bluex, _ = plt.hist(blue)
max_blue=bluey.max()
min_blue=bluey.min()
mean_blue=bluey.mean()
std_blue=bluey.std()
greeny, greenx, _ = plt.hist(green)
max_green=greeny.max()
min_green=greeny.min()
mean_green=greeny.mean()
std_green=greeny.std()
redy, redx, _ = plt.hist(red)
max_red=redy.max()
min_red=redy.min()
mean_red=redy.mean()
std_red=redy.std()
a=1
higher=np.array((mean_blue+a*std_blue,mean_green+a*std_green,mean_red+a*std_red))
lower=np.array((mean_blue-a*std_blue,mean_green-a*std_green, mean_red-a*std_red))
print(lower)
print(higher)
# setup initial location of window
# simply hardcoded the values
track_window = (c,r,w,h)
# set up the ROI for tracking
roi = frame[r:r+h, c:c+w]
hsv_roi = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_roi,lower,higher)
roi_hist = cv2.calcHist([hsv_roi],[0],mask,[180],[0,180])
cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)
# Setup the termination criteria, either 10 iteration or move by atleast 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)
dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1)
# apply camshift to get the new location
ret, track_window = cv2.CamShift(dst, track_window, term_crit)
# Draw it on image
pts = cv2.boxPoints(ret)
pts = np.int0(pts)
img2 = cv2.polylines(frame,[pts],True, 255,2)
cv2.imshow('img2',img2)
k = cv2.waitKey(60) & 0xff
if k == 27:
break
else:
cv2.imwrite(chr(k)+".jpg",img2)
else:
break
cv2.destroyAllWindows()
cap.release()