斑点检测+前景检测

时间:2016-07-09 08:05:00

标签: python opencv

我的帧差分(前景检测)完美运行。现在,我想在其中添加一个额外的功能,即blob检测。基本上,我的想法是在检测到的物体的运动上形成斑点圆。

这是我的代码:

import cv2

cap = cv2.VideoCapture('14.mp4')
ret, current_frame = cap.read()
previous_frame = current_frame

# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()

# Change blob detection thresholds
params.minThreshold = 200
params.maxThreshold = 255

params.minDistBetweenBlobs = 100

# Filter by Area.
params.filterByArea = True
params.minArea = 1200
params.maxArea = 40000

# Filter by Circularity
params.filterByCircularity = False
params.minCircularity = 0.1

# Filter by Convexity
params.filterByConvexity = False
params.minConvexity = 0.87

# Filter by Inertia
params.filterByInertia = True
params.minInertiaRatio = 0.02

# Create a detector with the parameters
detector = cv2.SimpleBlobDetector_create(params)

#Detect blobs
keypoints = detector.detect(current_frame)

while(cap.isOpened()):
    current_frame_gray = cv2.cvtColor(current_frame, cv2.COLOR_BGR2GRAY)
    previous_frame_gray = cv2.cvtColor(previous_frame, cv2.COLOR_BGR2GRAY)    

    frame_diff = cv2.absdiff(current_frame_gray,previous_frame_gray)

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

    cv2.imshow('frame diff ',im_with_keypoints )         
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

    previous_frame = current_frame.copy()
    ret, current_frame = cap.read()
    keypoints = detector.detect(current_frame)

cap.release()
cv2.destroyAllWindows() 

我的错误是"图片不是一个numpy arrary,也不是标量"

3 个答案:

答案 0 :(得分:0)

您正在将cap变量传递给函数detector.detect,但cap来自cv2.VideoCapture,它返回CvCapture对象,而不是Numpy数组。

相反,您应该使用current_frame返回的.read()

keypoints = detector.detect(current_frame)

答案 1 :(得分:0)

您应该导入库" numpy"

你可以在你的cmd上输入它(win + R,cmd,输入):pip install numpy

并在您的代码中输入:import numpy as np

答案 2 :(得分:0)

此问题是由您的阈值参数(主要是params.minThreshold = 100params.maxThreshold = 255)引起的。为什么要设置这些特定值?尝试对它们进行调整,直到获得良好的结果。

在旁注中,您似乎没有收到此错误,但是在我的一个.avi视频中运行您的代码时出现cv2.error。我将while(cap.isOpened()):更改为while(ret)

相关问题