简体   繁体   English

Python Opencv的新功能:使用网络摄像头阈值处理/扩展的运动跟踪

[英]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. 嘿大家我非常喜欢编程和python-opencv一般,我已经找到了答案,但我找不到它。

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 (ie 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? 这意味着使用的帧不是一个numpy数组?

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 提前致谢

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 cv2.threshold返回两个值 ,第二个是图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM