• Roman Donchenko's avatar
    Merge remote-tracking branch 'origin/2.4' into merge-2.4 · aacf188e
    Roman Donchenko authored
    Conflicts:
    	modules/ocl/include/opencv2/ocl/ocl.hpp
    	modules/ocl/src/arithm.cpp
    	modules/ocl/src/build_warps.cpp
    	modules/ocl/src/color.cpp
    	modules/ocl/src/haar.cpp
    	modules/ocl/src/imgproc.cpp
    	modules/ocl/src/split_merge.cpp
    	modules/ocl/test/test_color.cpp
    	samples/cpp/3calibration.cpp
    	samples/cpp/OpenEXRimages_HDR_Retina_toneMapping.cpp
    	samples/cpp/OpenEXRimages_HDR_Retina_toneMapping_video.cpp
    	samples/cpp/Qt_sample/main.cpp
    	samples/cpp/camshiftdemo.cpp
    	samples/cpp/descriptor_extractor_matcher.cpp
    	samples/cpp/distrans.cpp
    	samples/cpp/generic_descriptor_match.cpp
    	samples/cpp/grabcut.cpp
    	samples/cpp/morphology2.cpp
    	samples/cpp/segment_objects.cpp
    	samples/cpp/stereo_calib.cpp
    	samples/cpp/tutorial_code/Histograms_Matching/compareHist_Demo.cpp
    	samples/cpp/tutorial_code/core/mat_mask_operations/mat_mask_operations.cpp
    	samples/cpp/tutorial_code/introduction/display_image/display_image.cpp
    	samples/cpp/tutorial_code/introduction/windows_visual_studio_Opencv/Test.cpp
    	samples/cpp/tutorial_code/objectDetection/objectDetection.cpp
    	samples/cpp/tutorial_code/objectDetection/objectDetection2.cpp
    	samples/cpp/video_dmtx.cpp
    aacf188e
video_dmtx.cpp 3.27 KB
/*
 * starter_video.cpp
 *
 *  Created on: Nov 23, 2010
 *      Author: Ethan Rublee
 *
 * A starter sample for using opencv, get a video stream and display the images
 * Use http://datamatrix.kaywa.com/  to generate datamatrix images using strings of length 3 or less.
 * easy as CV_PI right?
 */
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <vector>
#include <stdio.h>

using namespace cv;
using namespace std;

//hide the local functions in an anon namespace
namespace
{
    void help(char** av)
    {
        cout << "\nThis program justs gets you started reading images from video\n"
        "Usage:\n./" << av[0] << " <video device number>\n" << "q,Q,esc -- quit\n"
        << "space   -- save frame\n\n"
        << "\tThis is a starter sample, to get you up and going in a copy pasta fashion\n"
        << "\tThe program captures frames from a camera connected to your computer.\n"
        << "\tTo find the video device number, try ls /dev/video* \n"
        << "\tYou may also pass a video file, like my_vide.avi instead of a device number"
        << "\n"
        << "DATA:\n"
        << "Generate a datamatrix from  from http://datamatrix.kaywa.com/  \n"
        << "  NOTE: This only handles strings of len 3 or less\n"
        << "  Resize the screen to be large enough for your camera to see, and it should find an read it.\n\n"
        << endl;
    }

    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;
        for (;;)
        {
            capture >> frame;
            if (frame.empty())
                break;
            cv::Mat gray;
            cv::cvtColor(frame,gray,COLOR_RGB2GRAY);
            vector<String> codes;
            Mat corners;
            findDataMatrix(gray, codes, corners);
            drawDataMatrixCodes(frame, codes, corners);
            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 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;
            }
        }
        return 0;
    }

}

int main(int ac, char** av)
{

    if (ac != 2)
    {
        help(av);
        return 1;
    }
    std::string arg = av[1];
    VideoCapture capture(arg); //try to open string, this will attempt to open it as a video file
    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())
    {
        cerr << "Failed to open a video device or video file!\n" << endl;
        help(av);
        return 1;
    }
    return process(capture);
}