starter_video.cpp

Frankie Fong, 2012-01-05 03:43 pm

Download (3.1 kB)

 
1
/*
2
* starter_video.cpp
3
*
4
*  Created on: Nov 23, 2010
5
*      Author: Ethan Rublee
6
*
7
* A starter sample for using opencv, get a video stream and display the images
8
* easy as CV_PI right?
9
*/
10
#include "opencv2/highgui/highgui.hpp"
11
#include <iostream>
12
#include <vector>
13
#include <stdio.h>
14
15
using namespace cv;
16
using namespace std;
17
18
19
20
//hide the local functions in an anon namespace
21
namespace {
22
    void help(char** av) {
23
        cout << "\nThis program justs gets you started reading images from video\n"
24
            "Usage:\n./" << av[0] << " <video device number>\n"
25
            << "q,Q,esc -- quit\n"
26
            << "space   -- save frame\n\n"
27
            << "\tThis is a starter sample, to get you up and going in a copy pasta fashion\n"
28
            << "\tThe program captures frames from a camera connected to your computer.\n"
29
            << "\tTo find the video device number, try ls /dev/video* \n"
30
            << "\tYou may also pass a video file, like my_vide.avi instead of a device number"
31
            << endl;
32
    }
33
34
        VideoWriter vidOut;
35
        Mat vidOutFrame;
36
37
    int process(VideoCapture& capture) {
38
            int n = 0;
39
            char filename[200];
40
        string window_name = "video | q or esc to quit";
41
                cout << "Frame count: " << capture.get(CV_CAP_PROP_FRAME_COUNT) << endl;
42
        cout << "press space to save a picture. q or esc to quit" << endl;
43
        namedWindow(window_name, CV_WINDOW_KEEPRATIO); //resizable window;
44
        Mat frame;
45
                int64 playtimer = getTickCount();
46
        for (;;) {
47
            capture >> frame;
48
            if (frame.empty())
49
            {
50
                                cout << "last frame: " << capture.get(CV_CAP_PROP_POS_FRAMES) << endl;
51
                                cout << " last time-stamp: " << capture.get(CV_CAP_PROP_POS_MSEC) << endl;
52
                                double actualTimePlayed = (double)(getTickCount() - playtimer)/getTickFrequency();
53
                                cout << "Actual Time played: " << actualTimePlayed << " secs." << endl;
54
                                waitKey(0);
55
                                break;
56
                        }
57
            imshow(window_name, frame);
58
                        frame.copyTo(vidOutFrame);
59
                        vidOut << vidOutFrame;
60
61
            char key = (char)waitKey(5); //delay N millis, usually long enough to display and capture input
62
            switch (key) {
63
        case 'q':
64
        case 'Q':
65
        case 27: //escape key
66
            return 0;
67
        case ' ': //Save an image
68
                sprintf(filename,"filename%.3d.jpg",n++);
69
                imwrite(filename,frame);
70
                cout << "Saved " << filename << endl;
71
                break;
72
        default:
73
            break;
74
            }
75
        }
76
        return 0;
77
    }
78
79
}
80
81
int main(int ac, char** av) {
82
83
    if (ac != 2) {
84
        help(av);
85
        return 1;
86
    }
87
    std::string arg = av[1];
88
    VideoCapture capture(arg); //try to open string, this will attempt to open it as a video file
89
    if (!capture.isOpened()) //if this fails, try to open as a video camera, through the use of an integer param
90
        capture.open(atoi(arg.c_str()));
91
    if (!capture.isOpened()) {
92
        cerr << "Failed to open a video device or video file!\n" << endl;
93
        help(av);
94
        return 1;
95
    }
96
97
        
98
        vidOutFrame.create(480, 640, CV_8UC3);
99
100
        vidOut.open("starter_video_out.mp4", CV_FOURCC('x', 'v', 'i', 'd'), 30, 
101
                                vidOutFrame.size(), true);
102
103
    return process(capture);
104
}