【问题标题】:How can I add a white background to a pre-recorded video using OpenCV如何使用 OpenCV 为预先录制的视频添加白色背景
【发布时间】: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


【解决方案1】:

经过大量调试,我终于找到了问题所在。它根本不在我的代码中。结果我的示例图像是 640x480,但当我放大脸部/肩膀并将其大小调整为 640x640 时,它工作得很好,所以问题在于图像的质量和大小。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-30
    • 2019-10-16
    • 2016-03-10
    • 2022-11-21
    • 2016-07-13
    • 1970-01-01
    • 2019-10-02
    • 2019-02-28
    相关资源
    最近更新 更多