-
Notifications
You must be signed in to change notification settings - Fork 1
/
auxiliary_bb - Αντιγραφή.py
147 lines (63 loc) · 3.53 KB
/
auxiliary_bb - Αντιγραφή.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
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 7 14:43:52 2020
@author: Admin
"""
import cv2
import numpy as np
from change_detection_boxes import Detects
def change_det(substruction, frame, num):
bb = []
contours_list = []
# diff = cv2.subtract(left_back, frame)
# gray_diff = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
mask = substruction.apply(frame)
blur = cv2.GaussianBlur(mask, (5, 5), 0)
_, threshold = cv2.threshold(blur, 130, 255, cv2.THRESH_BINARY)
# applying erosion - dilation to eliminate noise
kernel_dil = np.ones((5, 5), np.uint8)
dilation = cv2.dilate(threshold, kernel_dil, iterations = 1)
contours, hier = cv2.findContours(dilation, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
if not contours:
return([], [])
cntrs = sorted(contours, key = cv2.contourArea, reverse = True)[:2]
cv2.namedWindow(f'Camera{num}', cv2.WINDOW_NORMAL)
for i in cntrs:
if cv2.contourArea(i) > 5000 :
# (x,y),(MA,ma), angle = cv2.fitEllipse(i)
# print(angle)
# making contours convex
hull = cv2.convexHull(i, clockwise = True, returnPoints = True)
# contour approximation
epsilon = 0.0001 * cv2.arcLength(hull, False)
approx = cv2.approxPolyDP(hull, epsilon, True)
x, y, w, h = cv2.boundingRect(i)
bb.append([x, y, w, h])
contours_list.append(i)
# drawing and showing convexed aproximated contours
img2 = cv2.drawContours(frame , [i], -1, (255,0,0), 3)
# drawing bounding boxes through contours
cv2.rectangle(img2, (x, y), (x + w, y + h), (0,255,0),2)
cv2.imshow(f'Camera{num}', img2)
key = cv2.waitKey(20)
if key == ord('q'):
cv2.destroyAllWindows()
break
else:
continue
return(bb, contours_list)
if __name__ == '__main__':
substructionL = cv2.createBackgroundSubtractorMOG2()
substructionR = cv2.createBackgroundSubtractorMOG2()
video_right = cv2.VideoCapture('C:/Users/vero pc/Desktop/ΔΙΠΛΩΜΑΤΙΚΗ_lastTry/right_cutt_video.mp4')
video_left = cv2.VideoCapture('C:/Users/vero pc/Desktop/ΔΙΠΛΩΜΑΤΙΚΗ_lastTry/left_cutt_video.mp4')
while True:
_, frameL = video_left.read()
_, frameR = video_right.read()
bbL, contoursL = change_det(substructionL, frameL, num = 4)
bbR, contoursR = change_det(substructionR, frameR, num = 3)
if not bbL or not bbR :
continue
detectionsL = [Detects(box, contourL) for (box, contourL) in zip(bbL, contoursL)]
detectionsR = [Detects(box, contourR) for (box, contourR) in zip(bbR, contoursR)]
x=[Detects.matching(i, j, frameL, frameR, numL, numR) for numL, i in enumerate(detectionsL) for numR, j in enumerate(detectionsR)]