Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /share/CACHEDEV1_DATA/Web/www/libraries/UBBcode/text_parser.class.php on line 228
Writing a time-lapse application with OpenCV

Comments Blog About Development Research Sites

Writing a time-lapse application with OpenCV

Dec 13, 2011
For an upcoming research project, we will be working on an algorithm to track the sun. From ordinary recordings (made with a webcam or mobile phone camera for example) we wish to determine the trajectory and location of the sun at a specific time. With that information, we can then hopefully make an accurate location estimate - much like sailors used to do in ages past.

To this end, we need data to test with. Lots of data, preferably. And, what is perhaps more important, data with accurate time information from a known location, with seperable frames. While there are many time-lapse recordings of the sun and sky on youtube (for example: Ocean Sky, or Sunset in Athens), they generally lack either accurate frame-by-frame timestamps, localization information, or both. Not to mention, downloading a movie from youtube and parsing it into individual frames is a tricky business. Since I have a high-resolution (1280x1024) webcam available, I wrote my own application to create time-lapse recordings.

Initially my platform of choice was JAVA. However, all libraries I could find eventually relied on the Java Media Framework, or JMF, which is so antique the manual still mentions Windows 95/98 as supported platforms. Needless to say, it completely failed to recognize my webcam.

Next, I looked at OpenCV in JAVA. I have worked with OpenCV before (when using computer vision to guide Pioneer robots) and there is a good reason why so many computer-vision users like it. Sadly, that too, did not work. With a passionate "bugger that" attitude, I looked at the Visual C++ implementation of OpenCV, and that at last worked (after I switched the build to x64 instead of Win32, that is).

It has been a while since I last wrote any C, so the result is not exactly optimal, but it works just fine for what I need it to do:
Code (java) (nieuw venster):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <highgui.h>
#include <vidcap.h>

using namespace std;

int main(int argc, char ** argv) {

    // Initialize parameters.
    char *          image_extension         = new char[5];
    char            filename[FILENAME_MAX];
    int             camera_index            = 1;
    const int       width                   = 1280;
    const int       height                  = 1024;
    bool            store_video             = true;
    bool            store_images            = true;
    CvSize          size                    = cvSize(width, height);
    IplImage *      frame;
    CvVideoWriter * video;

    // Check command-line arguments.
    if (argc > 1)
        strcpy(image_extension, argv[1]);
    else
        strcpy(image_extension, "bmp");
    if (argc > 2)
        camera_index  = atoi(argv[2]);
    if (argc > 3)
        store_video   = atoi(argv[3]) == 1;
    if (argc > 4)
        store_images  = atoi(argv[4]) == 1;
    cout << "Useage:   \t timelapse [image extension] [camera index] [store video] [store images]" << endl;
    cout << "\nExample:\t timelapse jpg 1 1 0"      << endl;
    cout << "\n\n\t[Selected Options]"              << endl;
    cout << "Extension:     \t" << image_extension  << endl;
    cout << "Camera:        \t" << camera_index     << endl;
    cout << "Store video:   \t" << store_video      << endl;
    cout << "Store images:  \t" << store_images     << endl << endl;

    cvNamedWindow("Image:",1);
    CvCapture* capture = cvCaptureFromCAM(camera_index);

    if (store_video)
        video = cvCreateVideoWriter("video.avi", CV_FOURCC('D', 'I', 'V', 'X'), 25, size, 1);

    // Initialize the webcam.
    cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH,  width);
    cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, height);

    // Loop for time-lapse capturing.
    for (int index = 1; true; index++) {
        frame = cvQueryFrame(capture);
        cvShowImage("Image:", frame);
        sprintf_s(filename, "images/image_%.5d.%s", index, image_extension);

        if (store_images)
            cvSaveImage(filename, frame);
        if (store_video)
            cvWriteFrame(video, frame);
        if (cvWaitKey(30) >= 0)
            break;

        Sleep(2 * 1000);
    }


    // Cleanup
    cout << "Cleaning up and closing down." << endl;
    cvReleaseCapture(& capture);
    cvReleaseVideoWriter(& video);
    cvDestroyAllWindows();
    return 0;
}

Or you can skip all that, and just download the application as an MSI installer: http://download.fragfrog.nl/TimeLapseSetup.msi. Should work on Windows 7, no idea about older windows versions.

Run it on a laptop overnight with a webcam facing the window, and you get something like this:



FragFrog out!

New comment

Your name:
Comment: