-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
executable file
·62 lines (44 loc) · 1.48 KB
/
main.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
import cv2
import numpy as np
cap=cv2.VideoCapture("input.mp4")
cap_fundo=cv2.VideoCapture("fundo.mp4")
mean_fundo=[]
# calcula mediana de de 10 frame
#salvando fundo
for i in range(10):
fundo=cap_fundo.read()[1]
mean_fundo.append(fundo)
fundo = np.median(np.array(mean_fundo),axis=0)
fundo=fundo.astype(np.uint8)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
h, w, _ =fundo.shape
out = cv2.VideoWriter('s.mp4', fourcc, 30, (h, h))
while(cap.isOpened()):
ret,frame=cap.read()
if not ret:
break
print(frame.shape)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_red = np.array([0, 120, 70])
upper_red = np.array([30, 255, 255])
mask1 = cv2.inRange(hsv, lower_red, upper_red)
lower_red = np.array([120, 120, 70])
upper_red = np.array([180, 255, 255])
mask2 = cv2.inRange(hsv, lower_red, upper_red)
mask1 = mask1 + mask2
mask1 = cv2.morphologyEx(mask1, cv2.MORPH_OPEN, np.ones((3, 3), np.uint8))
mask1 = cv2.morphologyEx(mask1, cv2.MORPH_DILATE, np.ones((3, 3), np.uint8))
mask2 = cv2.bitwise_not(mask1)
cv2.imwrite("maks1.jpg",mask1)
cv2.imwrite("maks2.jpg", mask2)
res1 = cv2.bitwise_and(frame, frame, mask=mask2)
res2 = cv2.bitwise_and(fundo, fundo, mask=mask1)
cv2.imwrite("res1.jpg", res1)
cv2.imwrite("res2.jpg", res2)
saida = cv2.addWeighted(res1, 1, res2, 1, 0)
cv2.imshow("janela", saida)
k = cv2.waitKey(3)
#out.write(frame)
if k == ord("q"):
break
cv2.destroyAllWindows()