python - Python Opencv 新手 : Motion Tracking using webcam Thresholding/dilate

标签 python opencv webcam tracking motion

大家好,我是编程和 python-opencv 的新手,我已经为此搜索了答案,但我找不到。

我尝试通过以下方式使用我的网络摄像头进行运动跟踪:

  • 取当前帧与上一帧的绝对差值
  • 这被转换为灰度并通过阈值过滤器运行,这样只有发生变化的像素(即有运动的像素)才会是白色。所有其他像素将为黑色。

但是当我尝试设置阈值并在帧的差异中应用膨胀时出现错误:

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

这意味着使用的框架不是 numpy 数组?

这是我的部分代码:

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

我不知道这是使用它的最佳方式,但我将使用这段代码制作一个游戏,其目标是弹出落在屏幕上的泡泡,如果泡泡所在的位置有移动(如果有白色像素)气泡被弹出。

提前致谢

最佳答案

试试这个:

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

cv2.threshold 返回 two values第二个是图片

关于python - Python Opencv 新手 : Motion Tracking using webcam Thresholding/dilate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14508190/

相关文章:

java - fillPoly 没有按预期工作

python - Opencv或Numpy-有效替换图像中的像素列表

android - 如何在 Python 中使用我的 DroidCam 视频源(通过 USB)使用 OpenCV 进行图像处理?

php - Web 应用程序上的网络摄像头捕获

javascript - 我想使用 azure 媒体服务进行视频录制和流式传输

python - pip3 停止将可执行文件安装到/usr/local/bin 中

python - 应用程序注册表未就绪 : Apps aren't loaded yet

opencv - JavaCV : Webcam capturing using javacv

python - startapp 与 manage.py 在另一个目录中创建应用程序

python - 没有这样的文件或目录 : . ../part.0.parquet