我目前正在尝试使用背景减法器从检测器中消除误报。 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()
为什么会这样,是由于历史原因?如何纠正?
为了确定这种情况的发生,我输出了黑色像素的数量。零表示图像是灰度的,像素值为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
如果该项目链接到历史参数,那么如何编辑减法器以设置何时可以收集图像进行捕获?