背景减法器python opencv(去除颗粒)
您好,在使用MOG2制作从基础帧到下一帧的背景子生成器时。 但它让我明白了很多 我想如果有另一个背景减法,可以省略这个桥。 我还有另一个问题。 当汽车经过时,手电筒上的闪光灯显示为白色im mi图像。我需要忽略地上肉体之光的反射 有人知道道指会这么做吗 根据BGS的cod:背景减法器python opencv(去除颗粒),python,opencv,background,Python,Opencv,Background,您好,在使用MOG2制作从基础帧到下一帧的背景子生成器时。 但它让我明白了很多 我想如果有另一个背景减法,可以省略这个桥。 我还有另一个问题。 当汽车经过时,手电筒上的闪光灯显示为白色im mi图像。我需要忽略地上肉体之光的反射 有人知道道指会这么做吗 根据BGS的cod: backSub = cv2.createBackgroundSubtractorMOG2(history=1, varThreshold=150, detectShadows=True) fgMask = backSub.
backSub = cv2.createBackgroundSubtractorMOG2(history=1, varThreshold=150, detectShadows=True)
fgMask = backSub.apply(frame1)
fgMask2 = backSub.apply(actualframe)
maskedFrame = fgMask2 - fgMask
cv2.imshow("maskedFrame1 "+str(id), maskedFrame)
在将帧发送到
backSub.apply()
之前,您可以尝试执行高斯模糊,或者尝试使用cv2.createBackgroundSubtractorMOG2()的参数:如果您需要更好的解释,请尝试
这是使用7x7高斯模糊的结果
代码:
import cv2
import numpy as np
import sys
# read input video
cap = cv2.VideoCapture('traffic.mp4')
if (cap.isOpened()== False):
print("!!! Failed to open video")
sys.exit(-1)
# retrieve input video frame size
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
print('* Input Video settings:', frame_width, 'x', frame_height, '@', fps)
# adjust output video size
frame_height = int(frame_height / 2)
print('* Output Video settings:', frame_width, 'x', frame_height, '@', fps)
# create output video
video_out = cv2.VideoWriter('traffic_out.mp4', cv2.VideoWriter_fourcc(*'MP4V'), fps, (frame_width, frame_height))
#video_out = cv2.VideoWriter('traffic_out.avi', cv2.VideoWriter_fourcc('M','J','P','G'), fps, (frame_width, frame_height), True)
# create MOG
backSub = cv2.createBackgroundSubtractorMOG2(history=5, varThreshold=60, detectShadows=True)
while (True):
# retrieve frame from the video
ret, frame = cap.read() # 3-channels
if (frame is None):
break
# resize to 50% of its original size
frame = cv2.resize(frame, None, fx=0.5, fy=0.5)
# gaussian blur helps to remove noise
blur = cv2.GaussianBlur(frame, (7,7), 0)
#cv2.imshow('frame_blur', blur)
# subtract background
fgmask = backSub.apply(blur) # single channel
#cv2.imshow('fgmask', fgmask)
# concatenate both frames horizontally and write it as output
fgmask_bgr = cv2.cvtColor(fgmask, cv2.COLOR_GRAY2BGR) # convert single channel image to 3-channels
out_frame = cv2.hconcat([blur, fgmask_bgr]) #
#print('output=', out_frame.shape) # shape=(360, 1280, 3)
cv2.imshow('output', out_frame)
video_out.write(out_frame)
# quick pause to display the windows
if (cv2.waitKey(1) == 27):
break
# release resources
cap.release()
video_out.release()
cv2.destroyAllWindows()
您可以使用SuBSENSE:一种具有局部自适应灵敏度的通用变化检测方法
您可以在以下位置找到完整的实现:
另外,我不知道你的工作规模和要求。但Murari Mandal在GitHub上构建了一个信息丰富的存储库,其中包含了与背景减法相关的资源列表,可以解决上述问题
请提供您的输入图像和输出图像。它来自网络摄像头,没有图像。我有很多车从车库经过,但我听到了来自MOG2的噪音。当从网络摄像头读取图像时,使用imshow和waitKey在窗口显示它们(1)。如果cv2.waitKey(1)==32:cv2.imwrite(str(counter)+“.jpg”,image),也可以使用此语句代替waitKey。。。在按下空格键时保存网络摄像头的图像。每当存储图像时,计数器也会增加值。这样,您的网络摄像头图像将被存储。我特别要求您提供图像,以便帮助其他人找到解决此问题的方法。好的,我将提供这些方法/片段,作为答案的一部分,可能会有所帮助。那就更有用了:)@devforfu谢谢你,下次我一定会记住的
BackgroundSubtractionSuBSENSE bgs(/*...*/);
bgs.initialize(/*...*/);
for(/*all frames in the video*/) {
//...
bgs(input,output);
//...
}