How can I add a white background to a pre-recorded video using OpenCV

530 Views Asked by At

How can I add a white background to a video using OpenCV? I used this youtube video to get a white background for a real-time video using this code:

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)
    

and I got this result which is perfect for what I need.

When I used a video as the source, however, the background was removed terribly using the following code:

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()

And I got this result which is terrible, but it seems to be using the same process but very different results. All help is appreciated!

1

There are 1 best solutions below

0
On

After lots of debugging, I finally found the problem. It wasn't in my code at all. My sample image that turned out bad was 640x480, but when I zoomed in on the face/shoulders and resized to 640x640, it worked perfectly, so the problem was the quality and size of the image.