如何使用OpenCV向视频添加白色背景?我使用这 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)我得到了这一结果,它非常适合我所需要的东西。
但是,当我使用视频作为源时,使用以下代码删除了背景:
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()我得到了这一结果,这是很糟糕的,但它似乎使用相同的过程,但结果非常不同。感谢所有的帮助!
发布于 2022-03-28 01:24:28
经过大量的调试,我终于发现了这个问题。它根本不在我的代码里。我的样本图像是640x480,但是当我放大到640x640时,它工作得很好,所以问题是图像的质量和大小。
https://stackoverflow.com/questions/71637794
复制相似问题