|
|
@@ -0,0 +1,126 @@ |
|
|
|
import cv2 |
|
|
|
import numpy as np |
|
|
|
import sys |
|
|
|
|
|
|
|
winname = 'Diff' |
|
|
|
|
|
|
|
def tovalues(values, histo): |
|
|
|
return [ 0 if x < histo[1][1] else 2 if x > histo[1][2] else 1 for x in values ] |
|
|
|
|
|
|
|
def frameiter(fname): |
|
|
|
#vc = cv2.VideoCapture('output.mp4') |
|
|
|
#vc = cv2.VideoCapture('PXL_20220806_231420638.mp4') |
|
|
|
#vc = cv2.VideoCapture('PXL_20220806_231644224.mp4') |
|
|
|
#vc = cv2.VideoCapture('PXL_20220808_233318199.mp4') |
|
|
|
vc = cv2.VideoCapture('PXL_20220809_162724414.mp4') |
|
|
|
|
|
|
|
while True: |
|
|
|
r, frame = vc.read() |
|
|
|
if not r: |
|
|
|
break |
|
|
|
|
|
|
|
# base image processing |
|
|
|
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY, 1) |
|
|
|
|
|
|
|
yield frame |
|
|
|
|
|
|
|
def pixtot(frame): |
|
|
|
colsum = np.sum(frame, 0, np.float64) |
|
|
|
colsum = np.sum(colsum, 0, np.float64) |
|
|
|
|
|
|
|
return colsum |
|
|
|
|
|
|
|
def numpixels(frame): |
|
|
|
return pixtot(frame) / 255 |
|
|
|
|
|
|
|
sumhist = [] |
|
|
|
cont = False |
|
|
|
#cont = True |
|
|
|
key = 0 |
|
|
|
|
|
|
|
valuehist = [] |
|
|
|
|
|
|
|
basefr = None |
|
|
|
for frame in frameiter('output.mp4'): |
|
|
|
if basefr is None: |
|
|
|
basefr = frame |
|
|
|
continue |
|
|
|
|
|
|
|
frdiff = cv2.absdiff(basefr, frame) |
|
|
|
|
|
|
|
#hist = np.histogram(frdiff, bins=5) |
|
|
|
#print(repr(hist)) |
|
|
|
#frdiff = frame - basefr |
|
|
|
|
|
|
|
#frdiffbin = cv2.threshold(frdiff, 50, 100, cv2.THRESH_BINARY) |
|
|
|
frdiffbin = cv2.compare(frdiff, 50, cv2.CMP_GE) |
|
|
|
kernel = np.ones((4,4), np.uint8) |
|
|
|
frdiffbin = cv2.erode(frdiffbin, kernel, iterations = 2) |
|
|
|
|
|
|
|
numpix = numpixels(frdiffbin) |
|
|
|
print('np:', repr(numpix)) |
|
|
|
|
|
|
|
if numpix: |
|
|
|
# detected change |
|
|
|
# get average pixel value |
|
|
|
selected = cv2.bitwise_and(frame, frdiffbin) |
|
|
|
|
|
|
|
avg = pixtot(selected) / numpix |
|
|
|
print('avg:', avg) |
|
|
|
|
|
|
|
valuehist.append(avg) |
|
|
|
|
|
|
|
cv2.imshow('selected', selected) |
|
|
|
cv2.moveWindow('Base Frame', 1200, 300) |
|
|
|
|
|
|
|
if len(valuehist) > 6: |
|
|
|
print(repr(sorted(valuehist))) |
|
|
|
hist = np.histogram(valuehist, bins=3) |
|
|
|
print('hist:', repr(hist)) |
|
|
|
|
|
|
|
#print(repr(frdiffbin)) |
|
|
|
#moments = cv2.moments(frdiffbin) |
|
|
|
#((x, y), radius) = cv2.minEnclosingCircle(conts[0][0]) |
|
|
|
#x = int(x) |
|
|
|
#y = int(y) |
|
|
|
#radius = int(radius) |
|
|
|
#circ = ((x, y), radius) |
|
|
|
|
|
|
|
#white = (255,) |
|
|
|
|
|
|
|
#print(repr(moments)) |
|
|
|
#frdiff = cv2.circle(frdiff, circ[0], circ[1], white) |
|
|
|
cv2.imshow('Base Frame', basefr) |
|
|
|
cv2.moveWindow('Base Frame', 300, 300) |
|
|
|
cv2.imshow('Diff', frdiffbin) |
|
|
|
cv2.moveWindow('Diff', 600, 300) |
|
|
|
cv2.imshow('Next Frame', frame) |
|
|
|
cv2.moveWindow('Next Frame', 900, 300) |
|
|
|
|
|
|
|
# sum up the diffs |
|
|
|
colsum = np.sum(frdiff, 0, np.float64) |
|
|
|
colsum = np.sum(colsum, 0, np.float64) |
|
|
|
colsum = np.sum(colsum, 0, np.float64) |
|
|
|
sumhist.append(colsum) |
|
|
|
print(repr(colsum)) |
|
|
|
|
|
|
|
if not cont: |
|
|
|
key = cv2.waitKey(0) |
|
|
|
|
|
|
|
if key == ord('q'): |
|
|
|
break |
|
|
|
elif key == ord('c'): |
|
|
|
cont = True |
|
|
|
|
|
|
|
basefr = frame |
|
|
|
|
|
|
|
print(len(sumhist)) |
|
|
|
hist = np.histogram(sumhist, bins=5) |
|
|
|
print(repr(hist)) |
|
|
|
print(len(hist)) |
|
|
|
print(len(hist[1])) |
|
|
|
|
|
|
|
print(repr(valuehist)) |
|
|
|
hist = np.histogram(valuehist, bins=3) |
|
|
|
print(repr(hist)) |
|
|
|
print(repr(tovalues(valuehist, hist))) |