masterhead masterhead  masterhead

 Example program

When conducting research on high-speed image processing, the most important part is the development of algorithms.
This page shows some sample programs in order to...

  • provide sample programs for general processing (e.g. file input/output) to help your development
  • allow you to understand core approaches to deal with high-speed image processing

We hope you find this page informative. The program uses the “OpenCV” image processing library. The download page for OpenCV is here.

Table of Contents

Example Programs

Code for reading a movie file

This program loads sample.wmv and displays it.

cpp python2.7
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
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

int main(int argc, char *argv[])
{
    // opening a movie file
    cv::VideoCapture capture("sample.wmv");
    // Checking if the file is opened
    if(!capture.isOpened())
    {
        // exiting if opening file failed
        return 1;
    }
    
    cv::namedWindow("Sample", CV_WINDOW_AUTOSIZE|CV_WINDOW_FREERATIO);
    
    while(1)
    {
        cv::Mat frame;
        // Capturing
        capture >> frame;
        if(frame.empty())
        {
            // Exit if capture failed
            break;
        }
        
        /*
         *  ...
         *  Various Processing
         *  ...
         */
        
        // displaying the image
        cv::imshow("Sample", frame);
        // exiting when 'q' key is pressed
        int key = cv::waitKey(30);
        if(key == 'q')
        {
            break;
        }
    }
    return 0;
}

Code for saving a movie file

This program loads sample.wmv, converts it to gray scale videos, and then saves to a movie file.

cpp python2.7
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
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

int main(int argc, char *argv[])
{
    // opening a movie file
    cv::VideoCapture capture("sample.wmv");
    // Checking if the file is opened
    if(!capture.isOpened())
    {
        // exiting if opening file failed
        return -1;
    }
    
    /*  
     *  preparing writing video
     *  Saving movies at 30 frames per second without compression.
     *  Resolution of output movie is the same as that of input file.
     *  (Here, we assume that you use movies of SHIP-v, whose sizes are all 1024x1024.)
     */
    cv::VideoWriter writer("out.avi", CV_FOURCC('D', 'I', 'B', ' '), 30, cv::Size(1024, 1024));
    if(!writer.isOpened())
    {
        // exiting if preparing writing video failed
        return -1;
    }
    
    cv::namedWindow("Input");
    cv::namedWindow("Output");
    
    while(1)
    {
        cv::Mat frame, res;
        // Capturing
        capture >> frame;
        if(frame.empty())
        {
            // Exit if capture failed
            break;
        }
        cv::imshow("Input", frame);
        
        // The movie is converted to gray scale one, then saved.
        cv::cvtColor(frame, res, CV_BGR2GRAY);
        cv::imshow("Output", res);
        
        // Saving Video
        writer << res;
        
        // exiting when 'q' key is pressed
        int key = cv::waitKey(30);
        if(key == 'q')
        {
            break;
        }
    }
    return 0;
}
Ishikawa Senoo Laboratory, Department of Information Physics and Computing, Department of Creative Informatics,
Graduate School of Information Science and Technology, University of Tokyo
Ishikawa Senoo Laboratory WWW admin: www-admin@k2.t.u-tokyo.ac.jp
Copyright © 2008 Ishikawa Senoo Laboratory. All rights reserved.