无法在背景扣除视频opencv python上执行blob检测

时间:2017-05-31 03:32:06

标签: python opencv video

我有一个视频是通过在另一个视频上执行背景减法获得的。现在我需要对此视频执行blob检测并使用红色边框标记斑点。我的代码如下:

capture = cv2.VideoCapture('bw.avi')
size = (int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
video = cv2.VideoWriter('harsha_blob.avi', fourcc, 10.0,size)

while (1):
    ret, im = capture.read()
    im = cv2.convertScaleAbs(im)

    params = cv2.SimpleBlobDetector_Params()
    params.blobColor = 0
    params.filterByColor = True
    params.minArea = 0
    params.filterByArea = False
    params.minThreshold = 120;
    params.maxThreshold = 255;
    detector = cv2.SimpleBlobDetector_create(params)

    keypoints = detector.detect(im)

    # Draw detected blobs as red circles.
    # cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle corresponds to the size of blob
    im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)


    if ret==True:

        video.write(im_with_keypoints)


    else:
        capture.release()
        video.release()
        break

    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

cv2.destroyAllWindows()

背景扣除视频被反转以使斑点变黑并且背景为白色,因为斑点检测发现黑色/灰色斑点。我能够在一个帧中检测到blob,但是当我尝试在视频上运行它时,我收到以下错误。

    im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

error: C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\features2d\src\draw.cpp:115: error: (-215) !outImage.empty() in function cv::drawKeypoints

为什么我收到错误?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

!outImage.empty()发生在视频的末尾,当没有更多帧且ret, im = capture.read()返回ret==False时。在找到blob的关键点之前,应检查该条件。

while (1):
    ret, im = capture.read()
    if not ret: 
        break

    # blob detection code