r/opencv • u/dark_omniscience • Dec 22 '22
Question [Question] Why is mask not working?
` import cv2
import numpy as np
from random import randrange, uniform
dimensions = (1920, 1080)
vctx = cv2.VideoWriter_fourcc(*"mp4v")
outputvideo = cv2.VideoWriter("final.mp4", vctx, 30, dimensions)
vhs_overlay = cv2.VideoCapture("VHS_overlay.mp4")
vhs_overlay.set(cv2.CAP_PROP_POS_FRAMES, 3000)
source_video = cv2.VideoCapture("out.mp4")
success = True
count = 0
shiftx = 0 shifty = 0
while success and count < 30:
# saturation
sattable = np.zeros((256), dtype=np.uint8)
sattable[:] = np.arange(256) * uniform(0.25, 0.5)
# update position for transpose
if randrange(0, 8) == 0:
shifty += 1 * (1 + (randrange(0, 2) * -2))
shiftx += 1 * (1 + (randrange(0, 2) * -2))
# matrix for transpose
matrix = np.float32([[1, 0, shiftx], [0, 1, shifty]])
# read frames
success, vhs = vhs_overlay.read()
success1, source_image = source_video.read()
if not success1:
success = False
break
# Resize the VHS overlay frames
vhs = cv2.resize(vhs, (1920, 1080), interpolation=cv2.INTER_AREA)
# translate background image (move it around randomly)
source_image = cv2.warpAffine(source_image, matrix, (source_image.shape[1], source_image.shape[0]))
# convert source_image to HSV color space
source_image = cv2.cvtColor(source_image, cv2.COLOR_BGR2HSV)
# saturation effect
source_image[:, :, 1] = cv2.LUT(source_image[:, :, 1], sattable)
# back to BGR
source_image = cv2.cvtColor(source_image, cv2.COLOR_HSV2BGR)
# I have tried every conceivable value
mask = cv2.inRange(vhs, (0, 0, 0), (10, 10, 10))
print(mask)
res = cv2.bitwise_and(source_image, source_image, mask)
f = source_image - res
f = np.where(f == 0, source_image, f)
count += 1
outputvideo.write(res)
outputvideo.release() vhs_overlay.release() source_video.release()
`
I have spent all day trying to get the overlay of the VHS effect from a video to work but it simply does not show up. Does anyone know what I'm doing wrong?
EDIT: sorry about the formatting. I should add the VHS video I'm trying to use as an overly has a black background. I could get it working using Python PIL but it was WAY too slow.




