2017-05-31 71 views
0

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

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() 

背景减去视频翻转,使斑点的黑色和白色的背景作为斑点检测发现黑/灰斑点。我能够在单个框架中检测到斑点,但是当我试图在视频上运行时出现以下错误。

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

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

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

    # blob detection code