我目前正在尝试使用背景减除器从检测器中去除假阳性。Eack链接读取mjpeg视频,并将减法器应用于每个视频。代码运行正常,结果如下所示:

MoG2背景分隔符的代码是:
for index, link in enumerate(onlyfiles):
print(link)
subtractor = cv2.createBackgroundSubtractorMOG2(history=20, varThreshold=100, detectShadows=True)
count=0
count2=0
# Create a VideoCapture object and read from input file
# If the input is the camera, pass 0 instead of the video file name
cap = cv2.VideoCapture(link)
# Check if camera opened successfully
if (cap.isOpened() == False):
print("Error opening video stream or file")
# Read until video is completed
while (cap.isOpened()):
# Capture frame-by-frame
ret, frame = cap.read()
if ret == True:
print("Frame detected")
frame1 = frame.copy()
gray = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
blurred = cv2.bilateralFilter(gray, 9, 9, 9)
mask = subtractor.apply(blurred)
cv2.imshow("mask1", mask)
# Copy the thresholded image.
im_floodfill = mask.copy()
# Mask used to flood filling.
# Notice the size needs to be 2 pixels than the image.
h, w = mask.shape[:2]
mask1 = np.zeros((h+2, w+2), np.uint8)
# Floodfill from point (0, 0)
cv2.floodFill(im_floodfill, mask1, (0,0), 255);
# Invert floodfilled image
im_floodfill_inv = cv2.bitwise_not(im_floodfill)
# Combine the two images to get the foreground.
im_out = mask | im_floodfill_inv
cv2.imshow("Foreground", im_out)
cv2.imshow('Video', frame1)
cv2.waitKey(25)
# Press Q on keyboard to exit
if cv2.waitKey(25) & 0xFF == ord('q'):
break
# Break the loop
cv2.imshow('Video', frame)
cv2.waitKey(25)
else:
break
# When everything done, release the video capture object
cap.release()
# Closes all the frames
cv2.destroyAllWindows()在某些情况下,会输出一批灰度图像,如下所示:

为什么会发生这种情况,是因为历史原因吗?该如何纠正呢?
为了确定这种情况的发生,我输出了黑色pixels.zero的数量,这意味着图像是灰色的,像素值为127。
Frame detected
zero pixels: 0
Frame detected
zero pixels: 414628
Frame detected
zero pixels: 414615
Frame detected
zero pixels: 41465
Frame detected
zero pixels: 0
Frame detected
zero pixels: 413462
Frame detected
zero pixels: 414719
Frame detected
zero pixels: 414720
Frame detected
zero pixels: 414720
Frame detected
zero pixels: 414592
Frame detected
zero pixels: 413932
Frame detected
zero pixels: 412518
Frame detected
zero pixels: 412495
Frame detected
zero pixels: 414221
Frame detected
zero pixels: 0
Frame detected
zero pixels: 412651
Frame detected
zero pixels: 414290
Frame detected
zero pixels: 414490
Frame detected
zero pixels: 414707
Frame detected
zero pixels: 414687
Frame detected
zero pixels: 414689
Frame detected
zero pixels: 414665
Frame detected
zero pixels: 414704
Frame detected
zero pixels: 414583
Frame detected
zero pixels: 0如果此项目链接到历史参数,那么如何编辑减法器来设置何时可以收集图像以进行捕获?
发布于 2021-08-22 10:46:20
我也在试验这个背景减除器的东西。我也有同样的问题,并以某种方式解决了它。我认为你不能应用某种类型的循环,所以你可能想要提前调用它。就像在你的例子中,在for循环之前。也许这会有所帮助。
https://stackoverflow.com/questions/52803526
复制相似问题