斑点检测+前景检测

2024-04-23 23:57:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我的帧差分(前景检测)工作得很好。现在,我想在其中添加一个额外的功能,即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数组,也不是标量”


Tags: bywithdiffparamscurrentfiltercv2detector
3条回答

你应该导入库“numpy”

你可以在你的命令(win+R,cmd,enter)上调用它:pip install numpy

然后写下你的代码:导入numpy作为np

您正在将您的cap变量传递给函数^{},但是cap来自返回CvCapture对象而不是Numpy数组的{a2}。在

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

keypoints = detector.detect(current_frame)

这个问题是由阈值参数引起的,主要是params.minThreshold = 100和{}。你为什么要设定这些具体的价值观?试着把它们夹起来直到你得到好的结果。在

顺便说一句,你似乎没有发现这个错误,但是我在我的一个.avi视频上运行你的代码时遇到了一个cv2.error。我通过将while(cap.isOpened()):更改为while(ret)来修复它。在

相关问题 更多 >