Creating time lapse video
Previous tutorial: Blobs processing
There are cases, when it might be required to create a time lapse video - a video in which frequency of captured frames is much lower than that used to view the sequence. Although Computer Vision Sandbox is not specifically designed to create such videos, it does not mean it cannot be used to create those. As it was already mentioned before, the result which can be achieved is not so much constrained by the application itself. It is more about plug-ins, which are like LEGO pieces can be used together as building blocks to create the desired result. And so this tutorial will discuss the bits and pieces required to create time lapse video.
The first thing we'll need to find is how to save video frames at certain frequency. The problem is that this cannot be done by configuring video source to provide images at the rate we need and then save every coming image to file. Some video sources may provide the required precise frame rate control. However, most video sources provide limited control (if at all) of their frame rate - it can be set only to certain values and in most cases it is represented by a number of frames per second. Even if it is set to the smallest rate, like 1 frame a second, it may be still too fast for the time lapse video we may want to make. For example, we may want a frame in every 10 seconds to make a video of sunrise, or maybe a frame every minute to record progress of building construction.
Instead of trying to re-configure video source to provide frames at the rate we need, we'll let it play at whatever rate it prefers or was configured before. The actual control of the saving rate will be moved to a small Lua script, which saves video frames once in every X number of seconds. This approach actually has a benefit, which allows watching video source at its normal frame rate, while saving at a slower rate.
local os = require "os" -- Folder to write images to folder = 'C:\\Temp\\images\\' -- Interval between images in seconds imageInterval = 10 lastClock = -imageInterval -- Create instance of plug-in to write images imageWriter = Host.CreatePluginInstance( 'JpegExporter' ) imageWriter:SetProperty( 'quality', 100 ) ext = '.' .. imageWriter:SupportedExtensions( ) function Main( ) image = Host.GetImage( ) -- Get number of seconds of CPU time for the program now = os.clock( ) if now - lastClock >= imageInterval then lastClock = now SaveImage( image ) end end -- Save image to file with current timestamp function SaveImage( image ) dateTime = os.date( '%Y-%m-%d %H-%M-%S' ) fileName = folder .. dateTime .. ext imageWriter:ExportImage( fileName, image ) end
The above script demonstrates how to save video frames into JPEG files once in every 10 seconds (which can be easily changed). Of course using the Video File Writer plug-in mentioned before (see its usage from scripting), it can be possible to save video frames directly into a video file. However, it may not always be the desired solution for several reasons. First, if the process we want to film takes quite a lot of time, we may not want to keep video file open for that long. Second, once the required set of images is collected, those could be either pre-processed somehow before making video out of them, or simply filtered - we may want to remove some, which we did not like for whatever reason.
So we have collection of images showing the scene we wanted to film as a time lapse video. Suppose these images are pre-processed if required, all looking fine and we want to stich them together into a movie file. The plug-in to help with this is Image Folder Video Source. This plug-in represents a video source, which plays image files out of a given folder with a certain rate (frame interval). So to view the images' sequence we've got, all we need is to add a video source represented by this plug-in, specify folder name we've used before in the above script, specify desired frame interval and that is it - we can watch it playing faster than we did capturing.
Of course this is just watching the image sequence. But we are just a step away from a movie file. All we need is to create a sandbox for the video source and put Video File Writer plug-in as a video processing step for it. The video writing plug-in can be configured with the final frame rate we wish to have, which does not have to be the same as used by Image Folder Video Source plug-in. This means that if we want to process a big collection of image files as quickly as possible so we could get the final movie file faster, we just need to set frame interval to 0 for Image Folder Video Source plug-in, while keeping frame rate we need in Video File Writer.
Well, this tutorial proves it once more - the Computer Vision Sandbox application does little on its own. It is all about plug-ins and how they used together to achieve something useful. And the more plug-ins are provided, the more interesting stuff will come.
Note: as of 1.2.3 version of Computer Vision Sandbox, there is no need to use Lua scripting to create time lapsed images any more. This version provides a new Image Folder Writer plug-in, which writes images into the specified folder at the specified time intervals (supports both JPEG and PNG codecs). So, unless some custom logic is required, just use the plug-in and it will do the job.
Next tutorial: Capturing screen and windows