4 * Created on: Nov 23, 2010
7 * A starter sample for using opencv, get a video stream and display the images
10 #include "opencv2/highgui/highgui.hpp"
11 #include <opencv2/objdetect/objdetect.hpp>
12 #include <opencv2/imgproc/imgproc.hpp>
20 //hide the local functions in an anon namespace
25 cout << "\nThis program justs gets you started reading images from video\n"
26 "Usage:\n./" << av[0] << " <video device number>\n" << "q,Q,esc -- quit\n"
27 << "space -- save frame\n\n"
28 << "\tThis is a starter sample, to get you up and going in a copy pasta fashion\n"
29 << "\tThe program captures frames from a camera connected to your computer.\n"
30 << "\tTo find the video device number, try ls /dev/video* \n"
31 << "\tYou may also pass a video file, like my_vide.avi instead of a device number"
35 int process(VideoCapture& capture)
37 std::vector<DataMatrixCode> codes;
40 string window_name = "video | q or esc to quit";
41 cout << "press space to save a picture. q or esc to quit" << endl;
42 namedWindow(window_name, CV_WINDOW_KEEPRATIO); //resizable window;
50 cv::cvtColor(frame,gray,CV_RGB2GRAY);
51 findDataMatrix(gray, codes);
52 drawDataMatrixCodes(codes, frame);
53 imshow(window_name, frame);
54 char key = (char) waitKey(5); //delay N millis, usually long enough to display and capture input
61 case ' ': //Save an image
62 sprintf(filename, "filename%.3d.jpg", n++);
63 imwrite(filename, frame);
64 cout << "Saved " << filename << endl;
75 int main(int ac, char** av)
83 std::string arg = av[1];
84 VideoCapture capture(arg); //try to open string, this will attempt to open it as a video file
85 if (!capture.isOpened()) //if this fails, try to open as a video camera, through the use of an integer param
86 capture.open(atoi(arg.c_str()));
87 if (!capture.isOpened())
89 cerr << "Failed to open a video device or video file!\n" << endl;
93 return process(capture);