-
Notifications
You must be signed in to change notification settings - Fork 29
/
high.py
83 lines (63 loc) · 1.94 KB
/
high.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
import sys
import cv2 as cv
import os
def check_dir(path):
if path is None:
return
if not os.path.isdir(path):
os.makedirs(path)
def get_high(mask):
cnts, _ = cv.findContours(mask, cv.RETR_CCOMP, cv.CHAIN_APPROX_SIMPLE)
chigh = None
cy = 10000
for c in cnts:
x,y,w,h = cv.boundingRect(c)
s = min(w, h)
if s < 15:
continue
if y < cy:
r = max(w, h) / s
if r > 1.5:
continue
cy = y
chigh = c
return chigh
def draw_high_cont(path):
mask = cv.imread(path, cv.IMREAD_GRAYSCALE)
chigh = get_high(mask)
if chigh is not None:
cmask = cv.cvtColor(mask, cv.COLOR_GRAY2BGR)
cv.drawContours(cmask ,[chigh], 0,(255,0,0), 2)
cv.imwrite("out.jpg", cmask)
cv.imshow('frame', cmask)
cv.waitKey()
def get_high_blobs(clip_path, out_path = None, clr_out_path = None):
check_dir(out_path)
check_dir(clr_out_path)
vs = cv.VideoCapture(clip_path)
backSub = cv.createBackgroundSubtractorMOG2()
n = 0
while(True):
ret, frame = vs.read()
if not ret or frame is None:
break
mask = backSub.apply(frame)
mask = cv.GaussianBlur(mask, (7, 7),0)
ret,mask = cv.threshold(mask,0,255,cv.THRESH_BINARY | cv.THRESH_OTSU)
cmask = cv.cvtColor(mask, cv.COLOR_GRAY2BGR)
chigh = get_high(mask)
if chigh is not None:
rx,ry,rw,rh = cv.boundingRect(chigh)
cut = mask[ry : ry + rh, rx : rx + rw]
if not out_path is None:
cv.imwrite("{0}/b-{1:03d}.jpg".format(out_path, n), cut)
if not clr_out_path is None:
cut_f = frame[ry : ry + rh, rx : rx + rw]
cut_c = cv.bitwise_and(cut_f,cut_f,mask = cut)
cv.imwrite("{0}/c-{1:03d}.jpg".format(clr_out_path, n), cut_c)
print(n)
n += 1
if __name__ == "__main__":
#draw_high_cont(sys.argv[1])
get_high_blobs(sys.argv[1], sys.argv[2], sys.argv[2])
#get_high_blobs("D:/Videos/aus4.avi", "out", "clr")