2010-11-25 08:39:43 +08:00
|
|
|
/*
|
2010-12-21 19:37:08 +08:00
|
|
|
* starter_video.cpp
|
|
|
|
*
|
|
|
|
* Created on: Nov 23, 2010
|
|
|
|
* Author: Ethan Rublee
|
|
|
|
*
|
|
|
|
* A starter sample for using opencv, get a video stream and display the images
|
|
|
|
* easy as CV_PI right?
|
|
|
|
*/
|
2010-11-27 01:59:40 +08:00
|
|
|
#include <opencv2/highgui/highgui.hpp>
|
|
|
|
#include <iostream>
|
2010-11-25 08:39:43 +08:00
|
|
|
#include <vector>
|
2011-02-13 12:04:30 +08:00
|
|
|
#include <stdio.h>
|
2010-11-25 08:39:43 +08:00
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
using namespace std;
|
|
|
|
|
2011-02-13 12:04:30 +08:00
|
|
|
|
|
|
|
|
2010-11-25 08:39:43 +08:00
|
|
|
//hide the local functions in an anon namespace
|
|
|
|
namespace {
|
2010-12-21 19:37:08 +08:00
|
|
|
void help(char** av) {
|
|
|
|
cout << "\nThis program justs gets you started reading images from video\n"
|
|
|
|
"Usage:\n./" << av[0] << " <video device number>\n"
|
2011-02-13 12:04:30 +08:00
|
|
|
<< "q,Q,esc -- quit\n"
|
|
|
|
<< "space -- save frame\n\n"
|
2010-12-21 19:37:08 +08:00
|
|
|
<< "\tThis is a starter sample, to get you up and going in a copy pasta fashion\n"
|
|
|
|
<< "\tThe program captures frames from a camera connected to your computer.\n"
|
|
|
|
<< "\tTo find the video device number, try ls /dev/video* \n"
|
|
|
|
<< "\tYou may also pass a video file, like my_vide.avi instead of a device number"
|
|
|
|
<< endl;
|
|
|
|
}
|
2010-11-25 08:39:43 +08:00
|
|
|
|
2010-12-21 19:37:08 +08:00
|
|
|
int process(VideoCapture& capture) {
|
2011-02-13 12:04:30 +08:00
|
|
|
int n = 0;
|
|
|
|
char filename[200];
|
2010-12-21 19:37:08 +08:00
|
|
|
string window_name = "video | q or esc to quit";
|
2011-02-13 12:04:30 +08:00
|
|
|
cout << "press space to save a picture. q or esc to quit" << endl;
|
2010-12-21 19:37:08 +08:00
|
|
|
namedWindow(window_name, CV_WINDOW_KEEPRATIO); //resizable window;
|
|
|
|
Mat frame;
|
|
|
|
for (;;) {
|
|
|
|
capture >> frame;
|
|
|
|
if (frame.empty())
|
|
|
|
continue;
|
|
|
|
imshow(window_name, frame);
|
|
|
|
char key = (char)waitKey(5); //delay N millis, usually long enough to display and capture input
|
|
|
|
switch (key) {
|
|
|
|
case 'q':
|
|
|
|
case 'Q':
|
|
|
|
case 27: //escape key
|
|
|
|
return 0;
|
2011-02-13 12:04:30 +08:00
|
|
|
case ' ': //Save an image
|
|
|
|
sprintf(filename,"filename%.3d.jpg",n++);
|
|
|
|
imwrite(filename,frame);
|
|
|
|
cout << "Saved " << filename << endl;
|
|
|
|
break;
|
2010-12-21 19:37:08 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2010-11-25 08:39:43 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int ac, char** av) {
|
|
|
|
|
2010-12-21 19:37:08 +08:00
|
|
|
if (ac != 2) {
|
|
|
|
help(av);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
std::string arg = av[1];
|
|
|
|
VideoCapture capture(arg); //try to open string, this will attempt to open it as a video file
|
|
|
|
if (!capture.isOpened()) //if this fails, try to open as a video camera, through the use of an integer param
|
|
|
|
capture.open(atoi(arg.c_str()));
|
|
|
|
if (!capture.isOpened()) {
|
|
|
|
cerr << "Failed to open a video device or video file!\n" << endl;
|
|
|
|
help(av);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return process(capture);
|
2010-11-25 08:39:43 +08:00
|
|
|
}
|