-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcaAnalyser.py
175 lines (130 loc) · 4.64 KB
/
pcaAnalyser.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from utils import drawAxis, getOrientation
import numpy as np
import cv2 as cv
import argparse
def parser_args():
parser = argparse.ArgumentParser(
description='Process a video with applying the PCA algorithm.'
)
parser.add_argument(
'video', type=str,
help='Path to the file file to be processed.'
)
parser.add_argument(
'--color-mask', action='store_true',
help='Draw a colored mask over the detection.'
)
parser.add_argument(
'--both-axis', action='store_true',
help='Draw both PCA axis.'
)
parser.add_argument(
'--show-mask', action='store_true',
help='Displays a windows with the segmented mask.'
)
parser.add_argument(
'--save-video', action='store_true',
help='Create a video file with the analysis result.'
)
return parser.parse_args()
if __name__ == '__main__':
args = parser_args()
cap = cv.VideoCapture(args.video)
frameWidth = int(cap.get(3))
frameHeight = int(cap.get(4))
if (not cap.isOpened()):
print('Error opening video stream')
exit()
# First frame as the background image
ret, bg_img = cap.read()
if(not ret):
print('Error readning video stream')
exit()
if(args.show_mask):
mask_win = "Mask"
cv.namedWindow(mask_win, cv.WINDOW_KEEPRATIO)
cv.resizeWindow(mask_win, 450, 375)
result_win = "PCA Analyser"
cv.namedWindow(result_win, cv.WINDOW_KEEPRATIO)
cv.resizeWindow(result_win, 640, 528)
# Color range of the mice un the subtracted image
lower_white = np.array([120, 120, 120])
upper_white = np.array([160, 160, 160])
if(args.save_video):
resultFileName = args.video.split('/')[-1].split('.')[0] + '_result.avi'
outWriter = cv.VideoWriter(
resultFileName,
cv.VideoWriter_fourcc('M', 'J', 'P', 'G'),
30, (frameWidth, frameHeight)
)
while(cap.isOpened()):
ret, frame = cap.read()
if(not ret):
print('Error readning video stream')
exit()
sub_frame = cv.absdiff(frame, bg_img)
filtered_frame = cv.inRange(sub_frame, lower_white, upper_white)
# Kernel for morphological operation opening
kernel3 = cv.getStructuringElement(
cv.MORPH_ELLIPSE,
(3, 3),
(-1, -1)
)
kernel20 = cv.getStructuringElement(
cv.MORPH_ELLIPSE,
(20, 20),
(-1, -1)
)
# Morphological opening
mask = cv.dilate(cv.erode(filtered_frame, kernel3), kernel20)
mask_roi = cv.bitwise_and(sub_frame, sub_frame, mask=mask)
# Find all the contours in the mask
returns = cv.findContours(mask, cv.RETR_LIST, cv.CHAIN_APPROX_NONE)
# Check what findContours returned
contours = []
if(len(returns) == 3):
contours = returns[1]
else:
contours = returns[0]
for i, c in enumerate(contours):
# Calculate the area of each contour
area = cv.contourArea(c)
# Ignore contours that are too small or too large
if area < 1e2 or 1e5 < area:
continue
# Draw each contour only for visualisation purposes
cv.drawContours(frame, contours, i, (255, 0, 255), 2)
# Find the orientation of each shape
getOrientation(c, frame, args.both_axis)
if(args.color_mask):
# Change the color of the mask
colored_mask = cv.cvtColor(mask, cv.COLOR_GRAY2BGR)
colored_mask[np.where((colored_mask == [255, 255, 255]).all(axis = 2))] = [0, 0, 255]
# Apply the mask
frame = cv.add(frame, colored_mask)
cv.imshow(result_win, frame)
if(args.show_mask):
cv.imshow(mask_win, mask)
if(args.save_video):
outWriter.write(frame)
key = cv.waitKey(5)
if(key == 27 or key == 113):
cv.destroyAllWindows()
cap.release()
if(args.save_video):
outWriter.release()
exit()
if(key == 32):
while True:
cv.imshow(result_win, frame)
if(args.show_mask):
cv.imshow(mask_win, mask)
key2 = cv.waitKey(5)
if(key2 == 32):
break
elif(key2 == 27 or key == 113):
cv.destroyAllWindows()
cap.release()
if(args.save_video):
outWriter.release()
exit()