mirror of https://github.com/opencv/opencv.git
Open Source Computer Vision Library
https://opencv.org/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
3.1 KiB
94 lines
3.1 KiB
8 years ago
|
/**
|
||
|
* @file videocapture_starter.cpp
|
||
|
* @brief A starter sample for using OpenCV VideoCapture with capture devices, video files or image sequences
|
||
|
* easy as CV_PI right?
|
||
14 years ago
|
*
|
||
|
* Created on: Nov 23, 2010
|
||
|
* Author: Ethan Rublee
|
||
|
*
|
||
12 years ago
|
* Modified on: April 17, 2013
|
||
|
* Author: Kevin Hughes
|
||
14 years ago
|
*/
|
||
12 years ago
|
|
||
11 years ago
|
#include <opencv2/imgcodecs.hpp>
|
||
9 years ago
|
#include <opencv2/videoio.hpp>
|
||
|
#include <opencv2/highgui.hpp>
|
||
12 years ago
|
|
||
14 years ago
|
#include <iostream>
|
||
14 years ago
|
#include <stdio.h>
|
||
14 years ago
|
|
||
|
using namespace cv;
|
||
|
using namespace std;
|
||
|
|
||
12 years ago
|
//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;
|
||
|
}
|
||
14 years ago
|
|
||
12 years ago
|
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;
|
||
11 years ago
|
|
||
12 years ago
|
for (;;) {
|
||
|
capture >> frame;
|
||
|
if (frame.empty())
|
||
|
break;
|
||
11 years ago
|
|
||
12 years ago
|
imshow(window_name, frame);
|
||
|
char key = (char)waitKey(30); //delay N millis, usually long enough to display and capture input
|
||
11 years ago
|
|
||
12 years ago
|
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;
|
||
|
}
|
||
14 years ago
|
}
|
||
12 years ago
|
return 0;
|
||
14 years ago
|
}
|
||
14 years ago
|
}
|
||
|
|
||
|
int main(int ac, char** av) {
|
||
9 years ago
|
cv::CommandLineParser parser(ac, av, "{help h||}{@input||}");
|
||
|
if (parser.has("help"))
|
||
|
{
|
||
|
help(av);
|
||
|
return 0;
|
||
|
}
|
||
|
std::string arg = parser.get<std::string>("@input");
|
||
|
if (arg.empty()) {
|
||
14 years ago
|
help(av);
|
||
|
return 1;
|
||
|
}
|
||
12 years ago
|
VideoCapture capture(arg); //try to open string, this will attempt to open it as a video file or image sequence
|
||
14 years ago
|
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()) {
|
||
12 years ago
|
cerr << "Failed to open the video device, video file or image sequence!\n" << endl;
|
||
14 years ago
|
help(av);
|
||
|
return 1;
|
||
|
}
|
||
|
return process(capture);
|
||
14 years ago
|
}
|