【发布时间】:2022-07-08 23:05:36
【问题描述】:
如何使用 OpenCV 为视频添加白色背景?我使用this youtube 视频使用此代码为实时视频获取白色背景:
import cv2
import cvzone
from cvzone.SelfiSegmentationModule import SelfiSegmentation
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)
segmentor = SelfiSegmentation()
success = True
while True:
success, img = cap.read()
imgOut = segmentor.removeBG(img, (255, 255, 255), threshold=0.8)
cv2.imshow("Image", imgOut)
cv2.waitKey(1)
我得到了 this result 非常适合我的需要。
但是,当我使用视频作为源时,使用以下代码严重删除了背景:
cap = cv2.VideoCapture("Vid.mp4")
segmentor = SelfiSegmentation()
new = cv2.VideoWriter("Output.mp4", -1, 30, (640, 480))
success = True
while success:
success, img = cap.read()
if success:
imgOut = segmentor.removeBG(img, (255, 255, 255), threshold=0.8)
new.write(imgOut.copy())
cap.release()
new.release()
我得到了this result,这很糟糕,但它似乎使用了相同的过程,但结果却大不相同。感谢所有帮助!
【问题讨论】:
-
可以添加原图吗?
标签: python opencv computer-vision