1

I'm trying to get basic motion tracking working to be later used in an raspberrypi/arduino project. I don't know very much python yet but I can wrap my head around the logic of whats going on pretty well. I've been using some examples to try and get it working with my laptops built-in camera but it seems to be tracking the entirety of the image even when I'm outside the first frame. My guess is that the low-resolution (640x480) and frame rate (6 fps) is causing jitter, and the differences these frames from the jitter is what it's attempting to track. From what I've read the gaussianblur is supposed to take care of this- but it's not. The code seems to compile, I can see the multiple types of processing taking place in multiple windows and there is some motion-detection going on but its very inconsistent and I can't troubleshoot whats going wrong.

import cv2,time

first_frame = None

video = cv2.VideoCapture(0)

a = 1

while True:
    a = a + 1

    check, frame = video.read()
    print (frame)

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (21, 21), 0)

    if first_frame is None:
        first_frame = gray
        continue

    delta_frame = cv2.absdiff(first_frame, gray)

    thresh_delta = cv2.threshold(delta_frame, 25, 255, cv2.THRESH_BINARY)[1]

    thresh_delta = cv2.dilate(thresh_delta, None, iterations=2)

    (_, cnts, _) = cv2.findContours(thresh_delta.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    for contour in cnts:
        if cv2.contourArea(contour) < 1000:
            continue

        (x, y, w, h) = cv2.boundingRect(contour)
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

    cv2.imshow('captureFrame', frame)
    cv2.imshow('captureGrey', gray)
    cv2.imshow('delta', delta_frame)
    cv2.imshow('thresh', thresh_delta)

    key = cv2.waitKey(1)

    if key == ord('q'):
        break

print(a)
video.release()
cv2.destroyAllWindows()

enter image description here

EDIT: it seems to be a hardware problem regarding auto-lighting? Cannot confirm. But buying a cheap Microsoft lifecam VX 2000 seemed to resolved the issue.

Aspen
  • 143
  • 10
  • If your camera performs some kind of automatic brightness correction you will always get the whole frame as motion area – VideoProcessingResearcher Oct 29 '18 at 07:31
  • since I am using python to control the hardware at a very basic level, is there code I can use to check for this? or something within the windows shell? I do not have any other camera programs installed other than the basic driver. Would auto-brightness be controlled at a software or hardware level? – Aspen Oct 29 '18 at 08:04
  • @VideoProcessingResearcher UPDATE: It's night and theres not much light to work with. I discovered that if I turn off all nearby ambient light and then put my hand right up the camera, it then works. It seems it's actually locking onto all white light sources regardless of movement. Is this an indication of integrated auto-brightness? or is there something wrong in the code causing this? It seems like it's not even referencing the "first frame" for it's locking point. I'm thinking the first frame is capturing pure black and it just thinks all white around it is a change – Aspen Oct 29 '18 at 08:21
  • If you think that first frame is captured absolutely black, it's very easy to check if will save it to a JPG file via imwrite function. – VideoProcessingResearcher Oct 29 '18 at 08:55
  • You know, I think it is a problem with the built-in hardware cam. I bought a cheap microsoft lifecam vx2000 and it fixed the problem. First frame was not all black btw. – Aspen Nov 11 '18 at 17:03

0 Answers0