1 | initial version |
global vars are evil. avoid them, whenever possible (in your case, just pass them as additional parameters):
import cv2
import numpy as np
def accu_w(frame, avg1, avg2):
if avg1 is None: avg1 = np.float32(frame)
if avg2 is None: avg2 = np.float32(frame)
cv2.accumulateWeighted(frame, avg1, 0.05)
cv2.accumulateWeighted(frame, avg2, 0.0006)
res1 = cv2.convertScaleAbs(avg1)
res2 = cv2.convertScaleAbs(avg2)
cv2.imshow('img',frame)
cv2.imshow('avg1',res1)
cv2.imshow('avg2',res2)
def main():
c = cv2.VideoCapture(0)
global avg1 = None
global avg2 = None
while(True):
got_frame,frame = c.read()
if got_frame:
accu_w(frame, avg1, avg2)
k = cv2.waitKey(20)
if k == 27:
break
cv2.destroyAllWindows()
c.release()
2 | No.2 Revision |
global vars are evil. avoid them, whenever possible (in your case, just pass them as additional parameters):
import cv2
import numpy as np
def accu_w(frame, avg1, avg2):
if avg1 is None: avg1 = np.float32(frame)
if avg2 is None: avg2 = np.float32(frame)
cv2.accumulateWeighted(frame, avg1, 0.05)
cv2.accumulateWeighted(frame, avg2, 0.0006)
res1 = cv2.convertScaleAbs(avg1)
res2 = cv2.convertScaleAbs(avg2)
cv2.imshow('img',frame)
cv2.imshow('avg1',res1)
cv2.imshow('avg2',res2)
def main():
c = cv2.VideoCapture(0)
global avg1 = None
global avg2 = None
while(True):
got_frame,frame = c.read()
if got_frame:
accu_w(frame, avg1, avg2)
k = cv2.waitKey(20)
if k == 27:
break
cv2.destroyAllWindows()
c.release()