SampleMarkerDetector.cpp
Go to the documentation of this file.
00001 #include "CvTestbed.h"
00002 #include "MarkerDetector.h"
00003 #include "GlutViewer.h"
00004 #include "Shared.h"
00005 using namespace alvar;
00006 using namespace std;
00007 
00008 bool init=true;
00009 const int marker_size=15;
00010 Camera cam;
00011 Drawable d[32];
00012 std::stringstream calibrationFilename;
00013 
00014 void videocallback(IplImage *image)
00015 {
00016     static IplImage *rgba;
00017     bool flip_image = (image->origin?true:false);
00018     if (flip_image) {
00019         cvFlip(image);
00020         image->origin = !image->origin;
00021     }
00022 
00023     if (init) {
00024         init = false;
00025         cout<<"Loading calibration: "<<calibrationFilename.str();
00026         if (cam.SetCalib(calibrationFilename.str().c_str(), image->width, image->height)) {
00027             cout<<" [Ok]"<<endl;
00028         } else {
00029             cam.SetRes(image->width, image->height);
00030             cout<<" [Fail]"<<endl;
00031         }
00032         double p[16];
00033         cam.GetOpenglProjectionMatrix(p,image->width,image->height);
00034         GlutViewer::SetGlProjectionMatrix(p);
00035         for (int i=0; i<32; i++) {
00036             d[i].SetScale(marker_size);
00037         }
00038         rgba = CvTestbed::Instance().CreateImageWithProto("RGBA", image, 0, 4);
00039     }
00040     static MarkerDetector<MarkerData> marker_detector;
00041     marker_detector.SetMarkerSize(marker_size); // for marker ids larger than 255, set the content resolution accordingly
00042     //static MarkerDetector<MarkerArtoolkit> marker_detector;
00043     //marker_detector.SetMarkerSize(2.8, 3, 1.5);
00044 
00045     // Here we try to use RGBA just to make sure that also it works...
00046     //cvCvtColor(image, rgba, CV_RGB2RGBA);
00047     marker_detector.Detect(image, &cam, true, true);
00048     GlutViewer::DrawableClear();
00049     for (size_t i=0; i<marker_detector.markers->size(); i++) {
00050         if (i >= 32) break;
00051         
00052         Pose p = (*(marker_detector.markers))[i].pose;
00053         p.GetMatrixGL(d[i].gl_mat);
00054 
00055         int id = (*(marker_detector.markers))[i].GetId();
00056         double r = 1.0 - double(id+1)/32.0;
00057         double g = 1.0 - double(id*3%32+1)/32.0;
00058         double b = 1.0 - double(id*7%32+1)/32.0;
00059         d[i].SetColor(r, g, b);
00060 
00061         GlutViewer::DrawableAdd(&(d[i]));
00062     }
00063 
00064     if (flip_image) {
00065         cvFlip(image);
00066         image->origin = !image->origin;
00067     }
00068 }
00069 
00070 int main(int argc, char *argv[])
00071 {
00072     try {
00073         // Output usage message
00074         std::string filename(argv[0]);
00075         filename = filename.substr(filename.find_last_of('\\') + 1);
00076         std::cout << "SampleMarkerDetector" << std::endl;
00077         std::cout << "====================" << std::endl;
00078         std::cout << std::endl;
00079         std::cout << "Description:" << std::endl;
00080         std::cout << "  This is an example of how to detect 'MarkerData' markers using" << std::endl;
00081         std::cout << "  'MarkerDetector' and visualize them using 'GlutViewer'. In the" << std::endl;
00082         std::cout << "  SampleMarkerDetector window, various debug information is shown" << std::endl;
00083         std::cout << "  about the detected markers. The coordinate axes and a virtual cube" << std::endl;
00084         std::cout << "  are superimposed onto the markers to visualize the detected pose." << std::endl;
00085         std::cout << "  For each marker, a small image of the marker content is displayed" << std::endl;
00086         std::cout << "  at the origin and the marker number is displayed at one of the" << std::endl;
00087         std::cout << "  corners. At the opposing corner, the error estimation percentages" << std::endl;
00088         std::cout << "  'MARGIN_ERROR' and 'DECODE_ERROR' (red) or 'TRACK_ERROR' (dark red)" << std::endl;
00089         std::cout << "  are displayed." << std::endl;
00090         std::cout << std::endl;
00091         std::cout << "  In the AR window, squares are drawn over the marker positions using" << std::endl;
00092         std::cout << "  OpenGL. In the VR window, the squares are drawn with respect to the" << std::endl;
00093         std::cout << "  camera coordinate frame. The viewpoint can be modified by dragging" << std::endl;
00094         std::cout << "  with the left and right mouse buttons." << std::endl;
00095         std::cout << std::endl;
00096         std::cout << "Usage:" << std::endl;
00097         std::cout << "  " << filename << " [device|filename]" << std::endl;
00098         std::cout << std::endl;
00099         std::cout << "    device    integer selecting device from enumeration list (default 0)" << std::endl;
00100         std::cout << "              highgui capture devices are prefered" << std::endl;
00101         std::cout << "    filename  string specifying a media file as input" << std::endl;
00102         std::cout << std::endl;
00103         std::cout << "Keyboard Shortcuts:" << std::endl;
00104         std::cout << "  q: quit" << std::endl;
00105         std::cout << std::endl;
00106 
00107         // Initialise GlutViewer and CvTestbed
00108         GlutViewer::Start(argc, argv, 640, 480);
00109         CvTestbed::Instance().SetVideoCallback(videocallback);
00110 
00111         // Create capture object from camera (argv[1] is a number) or from file (argv[1] is a string)
00112         Capture *cap;
00113         std::string uniqueName;
00114         if ((argc > 1) && (!isdigit(argv[1][0]))) {
00115             // Manually create capture device and initialize capture object
00116             CaptureDevice device("file", argv[1]);
00117             cap = CaptureFactory::instance()->createCapture(device);
00118             uniqueName = "file";
00119         }
00120         else {    
00121             // Enumerate possible capture plugins
00122             CaptureFactory::CapturePluginVector plugins = CaptureFactory::instance()->enumeratePlugins();
00123             if (plugins.size() < 1) {
00124                 std::cout << "Could not find any capture plugins." << std::endl;
00125                 return 0;
00126             }
00127 
00128             // Display capture plugins
00129             std::cout << "Available Plugins: ";
00130             outputEnumeratedPlugins(plugins);
00131             std::cout << std::endl;
00132 
00133             // Enumerate possible capture devices
00134             CaptureFactory::CaptureDeviceVector devices = CaptureFactory::instance()->enumerateDevices();
00135             if (devices.size() < 1) {
00136                 std::cout << "Could not find any capture devices." << std::endl;
00137                 return 0;
00138             }
00139 
00140             // Check command line argument for which device to use
00141             int selectedDevice = defaultDevice(devices);
00142             if (argc > 1) {
00143                 selectedDevice = atoi(argv[1]);
00144             }
00145             if (selectedDevice >= (int)devices.size()) {
00146                 selectedDevice = defaultDevice(devices);
00147             }
00148             
00149             // Display capture devices
00150             std::cout << "Enumerated Capture Devices:" << std::endl;
00151             outputEnumeratedDevices(devices, selectedDevice);
00152             std::cout << std::endl;
00153             
00154             // Create capture object from camera
00155             cap = CaptureFactory::instance()->createCapture(devices[selectedDevice]);
00156             uniqueName = devices[selectedDevice].uniqueName();
00157         }
00158 
00159         // Handle capture lifecycle and start video capture
00160         // Note that loadSettings/saveSettings are not supported by all plugins
00161         if (cap) {
00162             std::stringstream settingsFilename;
00163             settingsFilename << "camera_settings_" << uniqueName << ".xml";
00164             calibrationFilename << "camera_calibration_" << uniqueName << ".xml";
00165             
00166             cap->start();
00167             cap->setResolution(640, 480);
00168             
00169             if (cap->loadSettings(settingsFilename.str())) {
00170                 std::cout << "Loading settings: " << settingsFilename.str() << std::endl;
00171             }
00172 
00173             std::stringstream title;
00174             title << "SampleMarkerDetector (" << cap->captureDevice().captureType() << ")";
00175 
00176             CvTestbed::Instance().StartVideo(cap, title.str().c_str());
00177 
00178             if (cap->saveSettings(settingsFilename.str())) {
00179                 std::cout << "Saving settings: " << settingsFilename.str() << std::endl;
00180             }
00181 
00182             cap->stop();
00183             delete cap;
00184         }
00185         else if (CvTestbed::Instance().StartVideo(0, argv[0])) {
00186         }
00187         else {
00188             std::cout << "Could not initialize the selected capture backend." << std::endl;
00189         }
00190 
00191         return 0;
00192     }
00193     catch (const std::exception &e) {
00194         std::cout << "Exception: " << e.what() << endl;
00195     }
00196     catch (...) {
00197         std::cout << "Exception: unknown" << std::endl;
00198     }
00199 }


ar_track_alvar
Author(s): Scott Niekum
autogenerated on Sun Oct 5 2014 22:16:26