Smooth 3D printing timelapse by optimal frame selection

To process video files OpenCV has to be compiled with ffmpeg option. On Windows it was a bit of a fight until I found out about Anaconda:

In the Anaconda prompt you can just execute

conda install -c menpo opencv3 ffmpeg

and satisfy the other requirements normally.

#!/usr/bin/env python

import numpy as np
import cv2
from skimage.measure import compare_ssim as ssim
import time

cap = cv2.VideoCapture('video.mp4')

ret,previous =
best_frame = previous
number = 50
file = 0

	i = 0
	best_s = 0
	i += 1
	ret, frame =
	s = ssim(frame, previous, multichannel=True)
	if(s > best_s):
		best_s = s
		best_frame = frame
	cv2.imwrite('folder/%d.png' % file,best_frame)
	file += 1
	previous = best_frame


A few ideas to improve performance in the future:

  • Simplify images before comparing them – This needs a run-time analysis to make sure that the simplifications actually improve speed. The idea is that it would be sufficient to look at a single channel image with half the size instead of RGB Full HD to measure similarity.
  • Make decisions more dynamic – When similarity is larger than a certain threshold and the frame is far enough away from the last, we can use that one right away instead of going through all the remaining candidates.
  • Implement the whole thing in C++ – the actual comparison that is taking a lot of time is done in python scikit-image. Python is a relatively abstract language that’s made for people who want to solve algorithmic problems instead of worrying about syntax. That’s why there are no type declarations, no pointer arithmetic, etc. but on the other hand is a bit slower!
  • Use multithreading – You could start one thread for every comparison and distribute them to CPU cores or even GPU! I am seeing about 15% CPU utilization when the script is running.

6 thoughts on “Smooth 3D printing timelapse by optimal frame selection”

  1. I wanted to try this code (but have no idea from python) and when I try to run it I get an “SyntaxError: invalid syntax” error at the while line at the “ret”, any ideas why? Is there a typo at the code in the article?

    while(i best_s):
    SyntaxError: invalid syntax

      1. Thanks, there were some “IndentationErrors” but fixed that and then some packages dependancies with OpenCV and ffmpeg, libavcodec-dev libavformat-dev libavdevice-dev, libgtk-x11-2.0/libgtk2.0-0 and it finally started working… (it’s really very slow)
        Until from what I found it failed because it did not like the video file… Maybe I give it another try once I have a new video file to try.
        BTW, Love the videos!

  2. You know height of the model, size and time of each step on Z-axis and length of the time lapse movie you want to make. Maybe a mod 2, mod 4 or mod 8 snapshot timing would work? Maybe the Z-axis step duration is related to the more interesting parts of a model?

  3. Hi – thanks for the great videos and sharing your scripts. The script has some indentation issues – I’ve shared an adjusted version on under tolleiv/996373132e3725fe02a2137836d50723 (https:// / tolleiv/996373132e3725fe02a2137836d50723 ) (providing a link seems to trigger your spam altert).

    This also contains the installation steps needed to run this script on a RaspberryPi from the attached camera.

Leave a Reply to Tolleiv Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.