I'm using background subtraction and I'm using python to do this but when I use the code it just seams to give me a black and white feed of what the camera is seeing. To my knowledge if nothing in front of the camera moves everything should go black, however this is a image of what I am getting.
Screenshot of problem:

This is the code that I am using.
import numpy as np
import cv2
import time
cap = cv2.VideoCapture(0)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
fgbg = cv2.BackgroundSubtractorMOG()
while(1):
    ret, frame = cap.read()
    fgmask = fgbg.apply(frame)
    fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel)
    cv2.imshow('frame',fgmask)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break
cap.release()
cv2.destroyAllWindows()
What am i doing wrong?
First of all, you should perform background subtraction to grayscale images. What you, basically, should do is saving a frame as reference in the first place, then subtract it from following frames.
You would like to apply some kind of blurring beforehand, and a dilation operation afterward to reduce noise.
This is the most basic background subtraction operation you can do with basic algebra. What you need is a subtraction and a couple of morphological operation.
import numpy as np
import cv2
import time
cap = cv2.VideoCapture(0)
ret, first = cap.read()
# Save the first image as reference
first_gray = cv2.cvtColor(first, cv2.COLOR_BGR2GRAY)
first_gray = cv2.GaussianBlur(first_gray, (21, 21), 0)
while True:
    ret, frame = cap.read()
    if not ret:
        break
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (21, 21), 0)
    # In each iteration, calculate absolute difference between current frame and reference frame
    difference = cv2.absdiff(gray, first_gray)
    # Apply thresholding to eliminate noise
    thresh = cv2.threshold(difference, 25, 255, cv2.THRESH_BINARY)[1]
    thresh = cv2.dilate(thresh, None, iterations=2)
    cv2.imshow("thresh", thresh)
    key = cv2.waitKey(1) & 0xFF
    # if the `q` key is pressed, break from the lop
    if key == ord("q"):
        break
cap.release()
cv2.destroyAllWindows()
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With