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
|
|
|
|
*
|
2013-04-18 01:20:32 +08:00
|
|
|
* Modified on: April 17, 2013
|
|
|
|
* Author: Kevin Hughes
|
|
|
|
*
|
|
|
|
* A starter sample for using OpenCV VideoCapture with capture devices, video files or image sequences
|
2010-12-21 19:37:08 +08:00
|
|
|
* easy as CV_PI right?
|
|
|
|
*/
|
2013-04-18 01:20:32 +08:00
|
|
|
|
|
|
|
#include <opencv2/highgui/highgui.hpp>
|
|
|
|
|
2010-11-27 01:59:40 +08:00
|
|
|
#include <iostream>
|
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;
|
|
|
|
|
2013-04-23 10:28:35 +08:00
|
|
|
//hide the local functions in an anon namespace
|
|
|
|
namespace {
|
|
|
|
void help(char** av) {
|
|
|
|
cout << "The program captures frames from a video file, image sequence (01.jpg, 02.jpg ... 10.jpg) or camera connected to your computer." << endl
|
|
|
|
<< "Usage:\n" << av[0] << " <video file, image sequence or device number>" << endl
|
|
|
|
<< "q,Q,esc -- quit" << endl
|
|
|
|
<< "space -- save frame" << endl << endl
|
|
|
|
<< "\tTo capture from a camera pass the device number. To find the device number, try ls /dev/video*" << endl
|
|
|
|
<< "\texample: " << av[0] << " 0" << endl
|
|
|
|
<< "\tYou may also pass a video file instead of a device number" << endl
|
|
|
|
<< "\texample: " << av[0] << " video.avi" << endl
|
|
|
|
<< "\tYou can also pass the path to an image sequence and OpenCV will treat the sequence just like a video." << endl
|
|
|
|
<< "\texample: " << av[0] << " right%%02d.jpg" << endl;
|
|
|
|
}
|
2011-02-13 12:04:30 +08:00
|
|
|
|
2013-04-23 10:28:35 +08:00
|
|
|
int process(VideoCapture& capture) {
|
|
|
|
int n = 0;
|
|
|
|
char filename[200];
|
|
|
|
string window_name = "video | q or esc to quit";
|
|
|
|
cout << "press space to save a picture. q or esc to quit" << endl;
|
|
|
|
namedWindow(window_name, WINDOW_KEEPRATIO); //resizable window;
|
|
|
|
Mat frame;
|
2013-04-18 01:20:32 +08:00
|
|
|
|
2013-04-23 10:28:35 +08:00
|
|
|
for (;;) {
|
|
|
|
capture >> frame;
|
|
|
|
if (frame.empty())
|
|
|
|
break;
|
|
|
|
|
|
|
|
imshow(window_name, frame);
|
|
|
|
char key = (char)waitKey(30); //delay N millis, usually long enough to display and capture input
|
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
case 'q':
|
|
|
|
case 'Q':
|
|
|
|
case 27: //escape key
|
|
|
|
return 0;
|
|
|
|
case ' ': //Save an image
|
|
|
|
sprintf(filename,"filename%.3d.jpg",n++);
|
|
|
|
imwrite(filename,frame);
|
|
|
|
cout << "Saved " << filename << endl;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2010-12-21 19:37:08 +08:00
|
|
|
}
|
2013-04-23 10:28:35 +08:00
|
|
|
return 0;
|
2010-12-21 19:37:08 +08:00
|
|
|
}
|
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];
|
2013-04-18 01:20:32 +08:00
|
|
|
VideoCapture capture(arg); //try to open string, this will attempt to open it as a video file or image sequence
|
2010-12-21 19:37:08 +08:00
|
|
|
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()) {
|
2013-04-18 01:20:32 +08:00
|
|
|
cerr << "Failed to open the video device, video file or image sequence!\n" << endl;
|
2010-12-21 19:37:08 +08:00
|
|
|
help(av);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return process(capture);
|
2010-11-25 08:39:43 +08:00
|
|
|
}
|