gpt4 book ai didi

python - 无法对背景减去的视频opencv python执行 Blob 检测

转载 作者:行者123 更新时间:2023-12-02 17:41:33 25 4
gpt4 key购买 nike

我有一个视频是通过对另一个视频执行背景减法获得的。现在我需要对这个视频执行 blob 检测并用红色边框标记 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 检测发现黑色/灰色 Blob 时,将减去背景的视频反转以使 Blob 变黑,使背景变白。我能够在单个帧中检测到 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

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

最佳答案

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

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

# blob detection code

关于python - 无法对背景减去的视频opencv python执行 Blob 检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44275155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com