top of page
Andrew Jones

Creating a dream sequence video using OpenCV & Python


Another very short post, this time showing how to take a video and create a 'dream sequence' version of it using OpenCV.

We do this by continuously blending frames together using the cv2.addWeighted functionality.

Code below:

##############################################################################

# import packages

##############################################################################

import numpy as np

import cv2

##############################################################################

# bring in video file and get stats

##############################################################################

cap = cv2.VideoCapture("driving_dubai_clipped.mp4")

width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))

height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

weighted_image = np.zeros([height, width, 3]).astype('uint8')

##############################################################################

# loop through frames, blending with the weighted image

##############################################################################

while True:

# read in frame

ret, frame = cap.read()

# weighted image

weighted_image = cv2.addWeighted(weighted_image,0.98,frame,0.02,0)

# stack original and dream frames

dual_image = np.vstack((frame,weighted_image))

# display the resulting frame

cv2.imshow('frame',dual_image)

if cv2.waitKey(1) & 0xFF == ord('q'):

break

# release capture

cap.release()

cv2.destroyAllWindows()

Here is the result!

802 views0 comments
bottom of page