import cv2 import numpy as np cap cv2 VideoCapture frame0 cap read gra

  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
148
149
150
151
152
153
154
155
156
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
#
# _, frame0 = cap.read()
# gray0 = cv2.cvtColor(frame0, cv2.COLOR_BGR2GRAY)
# while (1):
# for i in range(5):
# cap.read()
# _, frame1 = cap.read()
# gray1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
# cv2.subtract(gray1, gray0, gray0)
# can = cv2.Canny(gray0, 5, 200)
# (cnts, _) = cv2.findContours(can.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:10]
# screenCnt = None
# for c in cnts:
# # approximate the contour
# peri = cv2.arcLength(c, True)
# approx = cv2.approxPolyDP(c, 0.02 * peri, True)
#
# # if our approximated contour has four points, then
# # we can assume that we have found our screen
# if len(approx) == 4:
# screenCnt = approx
# break
# # cv2.convexHull(cnts)
# cv2.drawContours(gray0, [screenCnt], -1, 255, 3)
# cv2.imshow('frame', can)
# gray0 = gray1
# k = cv2.waitKey(10)
# if k >= 0:
# break
while (1):
_, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
h, w = gray.shape[:2]
# cv2.rectangle(gray, (w / 3, 0), (w * 2 / 3, h), 255)
cv2.imshow('frame', gray)
k = cv2.waitKey(10)
if k >= 0:
break
_, background = cap.read()
gray_bg = cv2.cvtColor(background, cv2.COLOR_BGR2GRAY)
bottom = gray_bg[h / 2 : h, w / 3 : 2 * w / 3]
avg = cv2.mean(bottom)[0]
def change(x):
global avg
avg = x
cv2.imshow('subtracted', gray_bg)
cv2.createTrackbar('avg', 'subtracted', int(avg), 255, change)
th = 50
while (1):
_, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
h, w = gray.shape[:2]
# cv2.rectangle(gray, (w / 3, 0), (w * 2 / 3, h), 255)
# cv2.imshow('frame', gray)
cv2.subtract(gray, avg - th, gray)
_, gray = cv2.threshold(gray, th, 255, cv2.THRESH_BINARY)
# for x in range(w):
# for y in range(h):
# if (avg - th) < gray[y, x] < (avg + th):
# gray[y, x] = 255
# else:
# gray[y, x] = 0
# cv2.subtract(gray, avg, gray)
#gray = cv2.Canny(gray, 0, 255)
cv2.imshow('subtracted', gray)
k = cv2.waitKey(10)
if k >= 0:
break
# while (1):
# _, frame = cap.read()
# gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# h, w = gray.shape[:2]
# cv2.subtract(gray, gray_bg, gray)
# th = 30
#
#
#
# # avg = cv2.mean(gray)[0]
# # for x in range(w):
# # for y in range(h):
# # if avg - th < gray[y, x] < avg + th:
# # gray[y, x] = 255
# # else:
# # gray[y, x] = 0
#
# cv2.imshow('frame', gray)
# k = cv2.waitKey(10)
# if k >= 0:
# break
#
#
# _, frame = cap.read()
# gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# h, w = gray.shape[:2]
# bottom = gray[0 : h, w / 3 : 2 * w / 3]
# cv2.imshow('bottom', bottom)
# avg = cv2.mean(bottom)[0]
# cv2.waitKey(0)
# cv2.destroyAllWindows()
#
# th = 30
#
# for x in range(w):
# for y in range(h):
# if avg - th < gray[y, x] < avg + th:
# gray[y, x] = 255
# else:
# gray[y, x] = 0
#
# # cv2.subtract(gray, avg, gray)
# cv2.imshow('subtracted', gray)
# cv2.waitKey(0)
# frame1 = None
# while (frame1 is None):
# ret, frame1 = cap.read()
# prvs = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY)
# hsv = np.zeros_like(frame1)
# hsv[...,1] = 255
#
# while(1):
# ret, frame2 = cap.read()
# if (frame2 is None):
# continue
# next = cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY)
#
# flow = cv2.calcOpticalFlowFarneback(prvs,next, 0.5, 3, 15, 3, 5, 1.2, 0)
#
# mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])
# hsv[...,0] = ang*180/np.pi/2
# hsv[...,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX)
# rgb = cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR)
# _,rgb = cv2.threshold(cv2.cvtColor(rgb,cv2.COLOR_BGR2GRAY), 30, 255, cv2.cv.CV_THRESH_BINARY)
# cv2.imshow('frame2',frame2)
# k = cv2.waitKey(30) & 0xff
# if k == 27:
# break
# elif k == ord('s'):
# cv2.imwrite('opticalfb.png',frame2)
# cv2.imwrite('opticalhsv.png',rgb)
# prvs = next
cap.release()
cv2.destroyAllWindows()