top of page

Creating a dream sequence video using OpenCV & Python

  • Andrew Jones
  • Oct 18, 2019
  • 1 min read

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!

1 Comment


David Walter
David Walter
Mar 19

This is a really cool and simple way to create a dream effect using OpenCV, I like how clearly the blending idea is explained. When I was learning Python and working on small video projects, I remember I had to buy your dissertation because I was struggling to manage coding and deadlines together. It gave me time to actually try things like this. It shows how small experiments can make learning more fun.

Like
bottom of page