Reputation: 1
So I'm writing a program that does background subtraction and plays an audio file if it detects movement(the white pixels) and I can't find a method to get the program to detect the pixels. Here's the code
import cv2
import numpy as np
cameraRecord = cv2.VideoCapture(1)
fgbg = cv2.createBackgroundSubtractorMOG2()
while True:
ret, frame = cameraRecord.read()
fgmask = fgbg.apply(frame)
fgmask.nonzero()
cv2.imshow("Original", frame)
cv2.imshow("Fg", fgmask)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cameraRecord.release()
cv2.destroyAllWindows()
Upvotes: 0
Views: 187
Reputation: 4561
You can use numpy to sum (docs) all the values in the image, if any white pixels are present the result will be higher than 0.
result = np.sum(fgmask)
You should check the result against a threshold value to prevent false positives when 1 pixel is white.
The numbers can get very high real fast, so you might find it easier to divide the result to get a smaller number. If you divide by the number of pixels, the result will be between 0 and 255.
Upvotes: 1