python opencv 3.3.0视频对象检测

时间:2017-09-16 07:13:40

标签: python opencv video background-subtraction

问题

现在我试图在5秒的视频中检测运动物体。我使用python 3和jupyter以及版本3.3.0的opencv

似乎我对函数cv2.imshow有一些问题,错误是:

错误:/Users/travis/build/skvark/opencv-python/opencv/modules/highgui/src/window.cpp:325:错误:(-215)size.width> 0&&函数imshow中的size.height> 0

代码

import cv2


def detect_video(video):
    camera = cv2.VideoCapture(video)
    history = 20    # 训练帧数

    bs = cv2.createBackgroundSubtractorKNN(detectShadows=True)  # 背景减除器,设置阴影检测
    bs.setHistory(history)

    frames = 0

while True:
    res, frame = camera.read()

    if not res:
        break

    fg_mask = bs.apply(frame)   # 获取 foreground mask

    if frames < history:
        frames += 1
        continue

    # 对原始帧进行膨胀去噪
    th = cv2.threshold(fg_mask.copy(), 244, 255, cv2.THRESH_BINARY)[1]
    th = cv2.erode(th, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)), iterations=2)
    dilated = cv2.dilate(th, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (8, 3)), iterations=2)
    # 获取所有检测框
    image, contours, hier = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    for c in contours:
        # 获取矩形框边界坐标
        x, y, w, h = cv2.boundingRect(c)
        # 计算矩形框的面积
        area = cv2.contourArea(c)
        if 500 < area < 3000:
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

    cv2.imshow("detection", frame)
    cv2.imshow("back", dilated)
    if cv2.waitKey(110) & 0xff == 27:
        break
camera.release()


if __name__ == '__main__':
    video = 'test.avi'
    detect_video(video)

注释

我找不到解决这个问题的好方法。有人可以帮忙吗?提前谢谢。

0 个答案:

没有答案