python - 背景减法器python opencv(去除颗粒)
问题描述
您好,使用 MOG2 制作从基本帧到下一帧的背景子。但它向我展示了很多 ruid
id 就像是否有另一个背景减法器可以限制这个 ponts。我还有另一个问题。当汽车通过手电筒上的闪光灯时,手电筒显示为白色 im mi 图像。我需要忽略地面上肉光的反射。
有人知道这样做吗?
通过 BGS 的鳕鱼:
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()
推荐阅读
- r - 可以将 S4 对象设置为反应变量吗?
- java - Eclipse-为什么模块信息很重要?
- knockout.js - 如何使用 knockoutjs 显示结帐步骤编号
- javascript - 如何让 mapbox-gl-js-offline-example 工作?
- sql - SQL Server - 无法在过程中添加注释
- linux - 在 Linux Distro 容器中安装 Lamp 与作为单独容器安装之间的区别?
- rust - 使用 flat_map 替换字符串中的值
- amazon-web-services - 当您的 s3 存储桶达到特定帐户最大限制的 90% 时发出警报或监控
- python - 在字典列表中查找特定键的平均值
- python-3.x - 使用 tkinter.Tk() 类执行代码时遇到问题