Skip to content

Instantly share code, notes, and snippets.

@donno2048
Created April 27, 2024 14:16
Show Gist options
  • Save donno2048/fffa89019e68c5b0990bf38b3d31482e to your computer and use it in GitHub Desktop.
Save donno2048/fffa89019e68c5b0990bf38b3d31482e to your computer and use it in GitHub Desktop.
motion enhancment
# enhance motion in video, motion is calculated based on the last 10 frames
from cv2 import VideoCapture, VideoWriter_fourcc, VideoWriter, normalize, CAP_PROP_FPS, CAP_PROP_FRAME_WIDTH, CAP_PROP_FRAME_HEIGHT, NORM_MINMAX, CV_8U
from numpy import average
vidcap = VideoCapture("in.mp4")
video = VideoWriter("out.mp4", VideoWriter_fourcc(*"mp4v"), vidcap.get(CAP_PROP_FPS), (int(vidcap.get(CAP_PROP_FRAME_WIDTH)), int(vidcap.get(CAP_PROP_FRAME_HEIGHT))))
success, image = vidcap.read()
last_images = [image] * 10
while success:
motion = (0xff - image + last_images.pop(0)) / 2 # extract motion
motion = average(motion, axis=2, keepdims=True) # greyscale
motion = (motion + 0xff) / 3 # tonedown
video.write(normalize(image + motion - 0xff / 2, None, 0, 0xff, NORM_MINMAX, dtype=CV_8U))
success, image = vidcap.read()
last_images.append(image)
video.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment