【问题标题】:Blob detection + foreground detection斑点检测+前景检测
【发布时间】:2019-06-13 07:36:07
【问题描述】:

我的帧差分(前景检测)工作得很好。现在,我想在其中添加一个额外的功能,即斑点检测。基本上,我的想法是在检测到的物体的运动上形成斑点圈。

这是我的代码:

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 数组,也不是标量”

【问题讨论】:

    标签: python opencv


    【解决方案1】:

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

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

    keypoints = detector.detect(current_frame)
    

    【讨论】:

    • 是的。我修好了它。但是为什么红色圆圈没有跟踪移动物体。它只是在视频的某一点保持静止。
    • @LeeAndroid 您是否尝试在循环结束时也添加keypoints = detector.detect(current_frame),以便可以在每个新视频帧处刷新斑点检测?
    • 是的,我做到了。我仍然找不到错误。即使我在循环结束时添加了代码。还是一样。
    • @LeeAndroid 对不起,我不明白为什么它不起作用。
    【解决方案2】:

    你应该导入库“numpy”

    你可以安装它,在你的 cmd(win+R , cmd , enter): pip install numpy

    然后在你的代码中写:import numpy as np

    【讨论】:

      【解决方案3】:

      这个问题是由你的阈值参数引起的,主要是params.minThreshold = 100params.maxThreshold = 255。为什么要设置这些特定值?尝试 tweeking 他们直到你得到好的结果。

      附带说明,您似乎没有收到此错误,但在我的一个 .avi 视频上运行您的代码时,我收到了 cv2.error。我通过将while(cap.isOpened()): 更改为while(ret) 来修复它。

      【讨论】:

        猜你喜欢
        • 2012-05-21
        • 2020-09-17
        • 1970-01-01
        • 1970-01-01
        • 2011-09-04
        • 2012-04-05
        • 1970-01-01
        • 2017-07-01
        • 2015-05-11
        相关资源
        最近更新 更多