-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
168 lines (109 loc) · 4.56 KB
/
script.py
File metadata and controls
168 lines (109 loc) · 4.56 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
###########
# Kaan Acar
#
# 040150039
#
###########
import cv2
import numpy as np
######## Kalman Filter
class KalmanFilter:
kf = cv2.KalmanFilter(4, 2)
kf.measurementMatrix = np.array([[1, 0, 0, 0], [0, 1, 0, 0]], np.float32)
kf.transitionMatrix = np.array([[1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0], [0, 0, 0, 1]], np.float32)
def Estimate(self, coordX, coordY):
''' This function estimates the position of the object'''
measured = np.array([[np.float32(coordX)], [np.float32(coordY)]])
self.kf.correct(measured)
predicted = self.kf.predict()
return predicted
####### Name of the video !!!
video_name = "video.mp4"
# opening the video capture
cap = cv2.VideoCapture(video_name)
#first frame
_,first_frame = cap.read()
first_frame_gray = cv2.cvtColor(first_frame,cv2.COLOR_BGR2GRAY)
frameCount = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
frameWidth = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frameHeight = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
buf = np.empty((frameCount, frameHeight, frameWidth), np.dtype('uint8'))
frames = np.empty((frameCount, frameHeight, frameWidth), np.dtype('uint8'))
three_frame_dif = np.empty((frameCount, frameHeight, frameWidth), np.dtype('uint8'))
final_video = np.empty((frameCount, frameHeight, frameWidth), np.dtype('uint8'))
merged = np.empty((frameCount, frameHeight, frameWidth), np.dtype('uint8'))
########################## Background subtraction and reading the frames
fc = 0
while True:
_ , frame = cap.read()
if(_ == False):
break
frame_gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
frames[fc] = frame_gray
#Background subtraction
difference = cv2.absdiff(first_frame_gray,frame_gray)
ret , difference = cv2.threshold(difference,10,255,cv2.THRESH_BINARY)
#we add the binary image to our empty matrix
buf[fc] = difference
fc += 1
############################### Three frame difference method
fc = 0
while (fc < frameCount):
if((fc == 0) or (fc == (frameCount - 1))):
three_frame_dif[fc] = frames[fc]
else:
D_1 = cv2.absdiff(frames[fc],frames[fc - 1])
ret , D_1 = cv2.threshold(D_1,2,255,cv2.THRESH_BINARY)
D_2 = cv2.absdiff(frames[fc + 1],frames[fc])
ret2 , D_2 = cv2.threshold(D_2,2,255,cv2.THRESH_BINARY)
result = np.bitwise_and(D_1,D_2)
three_frame_dif[fc] = result
fc += 1
fc = 0
################################### kernels for morphological operations
kernel = np.ones((15,15),np.uint8)
kernel2 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
kernel3 = np.ones((25,25),np.uint8)
################################ opening video capture again !!!!
cap = cv2.VideoCapture(video_name)
##################### kalman filter object
kfObj = KalmanFilter()
predictedCoords = np.zeros((2, 1), np.float32)
#################### merging background subtraction and three frame dif method / morphology transformations
for i in range(0,frameCount):
_,color = cap.read()
### Bitwise AND operation
final_video[i] = cv2.bitwise_and(buf[i],three_frame_dif[i])
merged[i] = final_video[i]
#### Morphological transformations
final_video[i] = cv2.morphologyEx(final_video[i], cv2.MORPH_OPEN, kernel2)
final_video[i] = cv2.dilate(final_video[i],kernel,iterations = 1)
final_video[i] = cv2.morphologyEx(final_video[i], cv2.MORPH_CLOSE, kernel3)
# finding contours
contours,h = cv2.findContours(final_video[i],cv2.RETR_TREE , cv2.CHAIN_APPROX_SIMPLE)
for pic , contour in enumerate(contours):
area = cv2.contourArea(contour)
# finding blobs on the current frame
if(area > 2500):
x,y,w,h = cv2.boundingRect(contour)
cv2.rectangle(color,(x,y),(x+w , y+h),(0,0,255 ),2)
## estimating possible position
predictedCoords = kfObj.Estimate(x,y)
x0 = int(predictedCoords[0])
y0 = int(predictedCoords[1])
# Drawing a rectangle as the predicted object position
cv2.rectangle(color, (x0 , y0 ), (x0 + w, y0 + h), (255,0,0 ), 2)
cv2.putText(color,"Current Position" ,(x + w, y + h), 0, 0.5, (0, 0, 255), 2)
cv2.putText(color, "Predicted Position", (x0 + w, y0), 0, 0.5, (255, 0, 0), 2)
cv2.imshow("window",color)
key = cv2.waitKey(33)
if(key == 27):
break
##### comparison of different operations
#cv2.imwrite("background.png",buf[145])
#cv2.imwrite("three-frame.png",three_frame_dif[145])
#cv2.imwrite("merged.png",merged[145])
#cv2.imwrite("final.png",final_video[145])
#cv2.waitKey()
cap.release()
cv2.destroyAllWindows()