import cv2 def back cap while frame cap read gray cv2 cvtColor frame c

 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
import cv2
def back(cap):
while (1):
_, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', gray)
k = cv2.waitKey(10)
if k >= 0:
break
cv2.destroyWindow('frame')
_, background = cap.read()
return background
def norm(cap):
while (1):
_, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', gray)
k = cv2.waitKey(10)
if k >= 0:
break
cv2.destroyWindow('frame')
_, background = cap.read()
# gray_bg = cv2.cvtColor(background, cv2.COLOR_BGR2GRAY)
# h, w = gray_bg.shape[:2]
# bottom = gray_bg[h / 2 : h, w / 3 : 2 * w / 3]
h, w = background.shape[:2]
bottom = background[0 : h, w / 3 : 2 * w / 3]
return cv2.mean(bottom)
def doIt(img, avg, th):
if img is None:
return
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = img[:,:]
cv2.subtract(gray, avg - th, gray)
_, gray = cv2.threshold(gray, th, 255, cv2.THRESH_BINARY)
return gray