4 * Created on: Nov 23, 2010
7 * A starter sample for using opencv, get a video stream and display the images
8 * Use http://datamatrix.kaywa.com/ to generate datamatrix images using strings of length 3 or less.
11 #include "opencv2/highgui/highgui.hpp"
12 #include <opencv2/objdetect/objdetect.hpp>
13 #include <opencv2/imgproc/imgproc.hpp>
21 //hide the local functions in an anon namespace
26 cout << "\nThis program justs gets you started reading images from video\n"
27 "Usage:\n./" << av[0] << " <video device number>\n" << "q,Q,esc -- quit\n"
28 << "space -- save frame\n\n"
29 << "\tThis is a starter sample, to get you up and going in a copy pasta fashion\n"
30 << "\tThe program captures frames from a camera connected to your computer.\n"
31 << "\tTo find the video device number, try ls /dev/video* \n"
32 << "\tYou may also pass a video file, like my_vide.avi instead of a device number"
35 << "Generate a datamatrix from from http://datamatrix.kaywa.com/ \n"
36 << " NOTE: This only handles strings of len 3 or less\n"
37 << " Resize the screen to be large enough for your camera to see, and it should find an read it.\n\n"
41 int process(VideoCapture& capture)
45 string window_name = "video | q or esc to quit";
46 cout << "press space to save a picture. q or esc to quit" << endl;
47 namedWindow(window_name, CV_WINDOW_KEEPRATIO); //resizable window;
55 cv::cvtColor(frame,gray, COLOR_RGB2GRAY);
58 findDataMatrix(gray, codes, corners);
59 drawDataMatrixCodes(frame, codes, corners);
60 imshow(window_name, frame);
61 char key = (char) waitKey(5); //delay N millis, usually long enough to display and capture input
68 case ' ': //Save an image
69 sprintf(filename, "filename%.3d.jpg", n++);
70 imwrite(filename, frame);
71 cout << "Saved " << filename << endl;
82 int main(int ac, char** av)
90 std::string arg = av[1];
91 VideoCapture capture(arg); //try to open string, this will attempt to open it as a video file
92 if (!capture.isOpened()) //if this fails, try to open as a video camera, through the use of an integer param
93 capture.open(atoi(arg.c_str()));
94 if (!capture.isOpened())
96 cerr << "Failed to open a video device or video file!\n" << endl;
100 return process(capture);