gpt4 book ai didi

python - 背景减法python opencv(去除颗粒)

转载 作者:行者123 更新时间:2023-12-02 15:54:01 25 4
gpt4 key购买 nike

您好,使用 MOG2 制作从基础帧到下一帧的背景子。但它向我展示了很多 ruid

enter image description here

id 就像如果有另一个背景减法器可以消除这个桥。我还有另一个问题。当一辆带闪光灯的汽车经过时,手电筒上显示为白色 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() 的参数:如果您需要更好地解释他们的工作,请尝试 this page .

这是使用 this video 进行 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()

关于python - 背景减法python opencv(去除颗粒),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62775713/

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