removed separate example for reading image sequences and modified starter_video.cpp to better explain the functionality of VideoCapture. I also added a bit more explanation in the documentation of the VideoCapture interface

This commit is contained in:
Kevin Hughes 2013-04-17 13:20:32 -04:00
parent b141762070
commit eedb6fa3c4
2 changed files with 43 additions and 41 deletions

View File

@ -197,8 +197,8 @@ VideoCapture
------------ ------------
.. ocv:class:: VideoCapture .. ocv:class:: VideoCapture
Class for video capturing from video files or cameras. Class for video capturing from video files, image sequences or cameras.
The class provides C++ API for capturing video from cameras or for reading video files. Here is how the class can be used: :: The class provides C++ API for capturing video from cameras or for reading video files and image sequences. Here is how the class can be used: ::
#include "opencv2/opencv.hpp" #include "opencv2/opencv.hpp"
@ -249,7 +249,7 @@ VideoCapture constructors.
.. ocv:cfunction:: CvCapture* cvCaptureFromFile( const char* filename ) .. ocv:cfunction:: CvCapture* cvCaptureFromFile( const char* filename )
.. ocv:pyoldfunction:: cv.CaptureFromFile(filename) -> CvCapture .. ocv:pyoldfunction:: cv.CaptureFromFile(filename) -> CvCapture
:param filename: name of the opened video file :param filename: name of the opened video file (eg. video.avi) or image sequence (eg. img%02d.jpg)
:param device: id of the opened video capturing device (i.e. a camera index). If there is a single camera connected, just pass 0. :param device: id of the opened video capturing device (i.e. a camera index). If there is a single camera connected, just pass 0.
@ -266,7 +266,7 @@ Open video file or a capturing device for video capturing
.. ocv:pyfunction:: cv2.VideoCapture.open(filename) -> retval .. ocv:pyfunction:: cv2.VideoCapture.open(filename) -> retval
.. ocv:pyfunction:: cv2.VideoCapture.open(device) -> retval .. ocv:pyfunction:: cv2.VideoCapture.open(device) -> retval
:param filename: name of the opened video file :param filename: name of the opened video file (eg. video.avi) or image sequence (eg. img%02d.jpg)
:param device: id of the opened video capturing device (i.e. a camera index). :param device: id of the opened video capturing device (i.e. a camera index).

View File

@ -4,47 +4,51 @@
* Created on: Nov 23, 2010 * Created on: Nov 23, 2010
* Author: Ethan Rublee * Author: Ethan Rublee
* *
* A starter sample for using opencv, get a video stream and display the images * Modified on: April 17, 2013
* Author: Kevin Hughes
*
* A starter sample for using OpenCV VideoCapture with capture devices, video files or image sequences
* easy as CV_PI right? * easy as CV_PI right?
*/ */
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <iostream> #include <iostream>
#include <vector>
#include <stdio.h> #include <stdio.h>
using namespace cv; using namespace cv;
using namespace std; using namespace std;
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;
}
int process(VideoCapture& capture) {
//hide the local functions in an anon namespace int n = 0;
namespace { char filename[200];
void help(char** av) { string window_name = "video | q or esc to quit";
cout << "\nThis program justs gets you started reading images from video\n" cout << "press space to save a picture. q or esc to quit" << endl;
"Usage:\n./" << av[0] << " <video device number>\n" namedWindow(window_name, CV_WINDOW_KEEPRATIO); //resizable window;
<< "q,Q,esc -- quit\n" Mat frame;
<< "space -- save frame\n\n"
<< "\tThis is a starter sample, to get you up and going in a copy pasta fashion\n" for (;;) {
<< "\tThe program captures frames from a camera connected to your computer.\n" capture >> frame;
<< "\tTo find the video device number, try ls /dev/video* \n" if (frame.empty())
<< "\tYou may also pass a video file, like my_vide.avi instead of a device number" break;
<< endl;
} imshow(window_name, frame);
char key = (char)waitKey(30); //delay N millis, usually long enough to display and capture input
int process(VideoCapture& capture) {
int n = 0; switch (key) {
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;
for (;;) {
capture >> frame;
if (frame.empty())
break;
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 'Q': case 'Q':
case 27: //escape key case 27: //escape key
@ -56,11 +60,9 @@ namespace {
break; break;
default: default:
break; break;
}
} }
return 0;
} }
return 0;
} }
int main(int ac, char** av) { int main(int ac, char** av) {
@ -70,11 +72,11 @@ int main(int ac, char** av) {
return 1; return 1;
} }
std::string arg = av[1]; std::string arg = av[1];
VideoCapture capture(arg); //try to open string, this will attempt to open it as a video file VideoCapture capture(arg); //try to open string, this will attempt to open it as a video file or image sequence
if (!capture.isOpened()) //if this fails, try to open as a video camera, through the use of an integer param 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())); capture.open(atoi(arg.c_str()));
if (!capture.isOpened()) { if (!capture.isOpened()) {
cerr << "Failed to open a video device or video file!\n" << endl; cerr << "Failed to open the video device, video file or image sequence!\n" << endl;
help(av); help(av);
return 1; return 1;
} }