SampleLabeling.cpp
Go to the documentation of this file.
00001 #include "CvTestbed.h"
00002 #include "ConnectedComponents.h"
00003 #include "Shared.h"
00004 using namespace alvar;
00005 using namespace std;
00006 
00007 int thresh_param1 = 31;
00008 
00009 void videocallback(IplImage *image)
00010 {
00011     bool flip_image = (image->origin?true:false);
00012     if (flip_image) {
00013         cvFlip(image);
00014         image->origin = !image->origin;
00015     }
00016 
00017     static LabelingCvSeq* labeling = 0;
00018     if(!labeling) {
00019         labeling = new LabelingCvSeq();
00020     }
00021 
00022     labeling->SetThreshParams(thresh_param1, 5);
00023 
00024     const int min_edge_size = 10;
00025     CvSeq* edges = labeling->LabelImage(image, min_edge_size);
00026 
00027     int n_edges = edges->total;
00028     for(int i = 0; i < n_edges; ++i)
00029     {
00030         CvSeq* pixels = (CvSeq*)cvGetSeqElem(edges, i);
00031         int n_pixels = pixels->total;
00032         for(int j = 0; j < n_pixels; ++j)
00033         {
00034             CvPoint* pt = (CvPoint*)cvGetSeqElem(pixels, j);
00035             cvLine(image, *pt, *pt, CV_RGB(255,0,0));
00036         }
00037     }
00038 
00039     // Visualize now also the square corners.
00040     labeling->LabelSquares(image, true);
00041 
00042     if (flip_image) {
00043         cvFlip(image);
00044         image->origin = !image->origin;
00045     }
00046 }
00047 
00048 int keycallback(int key)
00049 {
00050     if(key == '+')
00051     {
00052         thresh_param1+=2;
00053         std::cout<<"Adaptive threshold block size: "<<thresh_param1<<std::endl;
00054     }
00055     else if(key == '-')
00056     {
00057         thresh_param1-=2;
00058         if(thresh_param1<3) thresh_param1 = 3;
00059         std::cout<<"Adaptive threshold block size: "<<thresh_param1<<std::endl;
00060     }
00061 
00062     else return key;
00063 
00064     return 0;
00065 }
00066 
00067 int main(int argc, char *argv[])
00068 {
00069     try {
00070         // Output usage message
00071         std::string filename(argv[0]);
00072         filename = filename.substr(filename.find_last_of('\\') + 1);
00073         std::cout << "SampleLabeling" << std::endl;
00074         std::cout << "==============" << std::endl;
00075         std::cout << std::endl;
00076         std::cout << "Description:" << std::endl;
00077         std::cout << "  This is an example of how to use the 'LabelingCvSeq' class to perform" << std::endl;
00078         std::cout << "  labeling. Blobs are detected in the image and if the blobs have four" << std::endl;
00079         std::cout << "  corners, the edges between the corners are visualized." << std::endl;
00080         std::cout << std::endl;
00081         std::cout << "Usage:" << std::endl;
00082         std::cout << "  " << filename << " [device]" << std::endl;
00083         std::cout << std::endl;
00084         std::cout << "    device    integer selecting device from enumeration list (default 0)" << std::endl;
00085         std::cout << "              highgui capture devices are prefered" << std::endl;
00086         std::cout << std::endl;
00087         std::cout << "Keyboard Shortcuts:" << std::endl;
00088         std::cout << "  +: Increase adaptive threshold block size." << std::endl;
00089         std::cout << "  -: Decrease adaptive threshold block size." << std::endl;
00090         std::cout << "  q: quit" << std::endl;
00091         std::cout << std::endl;
00092 
00093         // Initialise CvTestbed
00094         CvTestbed::Instance().SetVideoCallback(videocallback);
00095         CvTestbed::Instance().SetKeyCallback(keycallback);
00096 
00097         // Enumerate possible capture plugins
00098         CaptureFactory::CapturePluginVector plugins = CaptureFactory::instance()->enumeratePlugins();
00099         if (plugins.size() < 1) {
00100             std::cout << "Could not find any capture plugins." << std::endl;
00101             return 0;
00102         }
00103 
00104         // Display capture plugins
00105         std::cout << "Available Plugins: ";
00106         outputEnumeratedPlugins(plugins);
00107         std::cout << std::endl;
00108 
00109         // Enumerate possible capture devices
00110         CaptureFactory::CaptureDeviceVector devices = CaptureFactory::instance()->enumerateDevices();
00111         if (devices.size() < 1) {
00112             std::cout << "Could not find any capture devices." << std::endl;
00113             return 0;
00114         }
00115 
00116         // Check command line argument for which device to use
00117         int selectedDevice = defaultDevice(devices);
00118         if (argc > 1) {
00119             selectedDevice = atoi(argv[1]);
00120         }
00121         if (selectedDevice >= (int)devices.size()) {
00122             selectedDevice = defaultDevice(devices);
00123         }
00124         
00125         // Display capture devices
00126         std::cout << "Enumerated Capture Devices:" << std::endl;
00127         outputEnumeratedDevices(devices, selectedDevice);
00128         std::cout << std::endl;
00129         
00130         // Create capture object from camera
00131         Capture *cap = CaptureFactory::instance()->createCapture(devices[selectedDevice]);
00132         std::string uniqueName = devices[selectedDevice].uniqueName();
00133 
00134         // Handle capture lifecycle and start video capture
00135         // Note that loadSettings/saveSettings are not supported by all plugins
00136         if (cap) {
00137             std::stringstream settingsFilename;
00138             settingsFilename << "camera_settings_" << uniqueName << ".xml";
00139             
00140             cap->start();
00141             cap->setResolution(640, 480);
00142             
00143             if (cap->loadSettings(settingsFilename.str())) {
00144                 std::cout << "Loading settings: " << settingsFilename.str() << std::endl;
00145             }
00146 
00147             std::stringstream title;
00148             title << "SampleLabeling (" << cap->captureDevice().captureType() << ")";
00149 
00150             CvTestbed::Instance().StartVideo(cap, title.str().c_str());
00151 
00152             if (cap->saveSettings(settingsFilename.str())) {
00153                 std::cout << "Saving settings: " << settingsFilename.str() << std::endl;
00154             }
00155 
00156             cap->stop();
00157             delete cap;
00158         }
00159         else if (CvTestbed::Instance().StartVideo(0, argv[0])) {
00160         }
00161         else {
00162             std::cout << "Could not initialize the selected capture backend." << std::endl;
00163         }
00164 
00165         return 0;
00166     }
00167     catch (const std::exception &e) {
00168         std::cout << "Exception: " << e.what() << endl;
00169     }
00170     catch (...) {
00171         std::cout << "Exception: unknown" << std::endl;
00172     }
00173 }


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