Search code examples
pythonopencvwebcamtrackingmotion

New to Python Opencv: Motion Tracking using webcam Thresholding/dilate


Hey everyone Im very new to programming and python-opencv in general, ive already searched for an answer for this but I couldn't find it.

Im trying to do motion tracking using my webcam by:

  • taking absolute difference of the current frame and the previous frame
  • This is converted to greyscale and run through a threshold filter so that only pixels that have changed (i.e. where there is movement) will be white. All other pixels will be black.

But I'm getting an error when I try to threshold and applying the dilate in the difference of the frames:

t_minus_dilate = cv2.dilate(t_minus_thresh, es)
TypeError: <unknown> is not a numpy array

This means that the frame used is not a numpy array?

Heres part of my code:

cv2.namedWindow("window_b", cv2.CV_WINDOW_AUTOSIZE)
# Structuring element
es = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9,4))

## Webcam Settings
capture = cv2.VideoCapture(0)

def diffImg(t0, t1, t2): #calculates the difference between frames
    d1 = cv2.absdiff(t2, t1)
    d2 = cv2.absdiff(t1, t0)
    return cv2.bitwise_and(d1, d2)

t_minus = cv2.cvtColor(capture.read()[1], cv2.COLOR_RGB2GRAY)
t_minus_thresh = cv2.threshold(t_minus, 0, 255, cv2.THRESH_OTSU)
t_minus_dilate = cv2.dilate(t_minus_thresh, es)

t = cv2.cvtColor(capture.read()[1], cv2.COLOR_RGB2GRAY)
t_thresh = cv2.threshold(t, 0, 255, cv2.THRESH_OTSU)
t_dilate = cv2.dilate(t_minus_thresh, es)

t_plus = cv2.cvtColor(capture.read()[1], cv2.COLOR_RGB2GRAY)
t_plus_thresh = cv2.threshold(t_plus, 0, 255, cv2.THRESH_OTSU)
t_plus_dilate = cv2.dilate(t_plus_thresh, es)


while True:

    diff = diffImg(t_minus_dilate, t_dilate, t_plus_dilate) #difference between the frames
    cv2.imshow('window_b',diff)

    t_minus_dilate = t_dilate
    t = diff
    t_plus_dilate = cv2.dilate(diff, es)

    key = cv2.waitKey(10) #20
    if key == 27: #exit on ESC
        cv2.destroyAllWindows()
        break

I dont know it its the best way to use this but Im gonna use this code to make a game that the objective is to pop bubbles that are falling over the screen, if theres movement where the bubble is positioned (if there are white pixels) the bubble is popped.

Thanks in advance


Solution

  • Try this:

    retvel, t_minus_thresh = cv2.threshold(t_minus, 0, 255, cv2.THRESH_OTSU)
    t_minus_dilate = cv2.dilate(t_minus_thresh, es)
    

    cv2.threshold returns two values and the second one is the image