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)
43 std::vector<DataMatrixCode> codes;
46 string window_name = "video | q or esc to quit";
47 cout << "press space to save a picture. q or esc to quit" << endl;
48 namedWindow(window_name, CV_WINDOW_KEEPRATIO); //resizable window;
56 cv::cvtColor(frame,gray,CV_RGB2GRAY);
57 findDataMatrix(gray, codes);
58 drawDataMatrixCodes(codes, frame);
59 imshow(window_name, frame);
60 char key = (char) waitKey(5); //delay N millis, usually long enough to display and capture input
67 case ' ': //Save an image
68 sprintf(filename, "filename%.3d.jpg", n++);
69 imwrite(filename, frame);
70 cout << "Saved " << filename << endl;
81 int main(int ac, char** av)
89 std::string arg = av[1];
90 VideoCapture capture(arg); //try to open string, this will attempt to open it as a video file
91 if (!capture.isOpened()) //if this fails, try to open as a video camera, through the use of an integer param
92 capture.open(atoi(arg.c_str()));
93 if (!capture.isOpened())
95 cerr << "Failed to open a video device or video file!\n" << endl;
99 return process(capture);