-
Notifications
You must be signed in to change notification settings - Fork 1
/
imaging.py
50 lines (37 loc) · 1.65 KB
/
imaging.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
import cv2
import imutils
def draw_color_mask(img, borders, color=(0, 0, 0)):
h = img.shape[0]
w = img.shape[1]
x_min = int(borders[0] * w / 100)
x_max = w - int(borders[2] * w / 100)
y_min = int(borders[1] * h / 100)
y_max = h - int(borders[3] * h / 100)
img = cv2.rectangle(img, (0, 0), (x_min, h), color, -1) # left border
img = cv2.rectangle(img, (0, 0), (w, y_min), color, -1) # top border
img = cv2.rectangle(img, (x_max, 0), (w, h), color, -1) # right border
img = cv2.rectangle(img, (0, y_max), (w, h), color, -1) # bottom border
return img
def preprocess_image_change_detection(img, gaussian_blur_radius_list=None, black_mask=(5, 10, 5, 0)):
gray = img.copy()
gray = cv2.cvtColor(gray, cv2.COLOR_BGR2GRAY)
if gaussian_blur_radius_list is not None:
for radius in gaussian_blur_radius_list:
gray = cv2.GaussianBlur(gray, (radius, radius), 0)
gray = draw_color_mask(gray, black_mask)
return gray
def compare_frames_change_detection(prev_frame, next_frame, min_contour_area, frame_change_thresh=45):
frame_delta = cv2.absdiff(prev_frame, next_frame)
thresh = cv2.threshold(frame_delta, frame_change_thresh, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.dilate(thresh, None, iterations=2)
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
score = 0
res_cnts = []
for c in cnts:
if cv2.contourArea(c) < min_contour_area:
continue
res_cnts.append(c)
score += cv2.contourArea(c)
return score, res_cnts, thresh