SampleCvTestbed.cpp
Go to the documentation of this file.
00001 #include "CvTestbed.h"
00002 #include "Shared.h"
00003 using namespace alvar;
00004 using namespace std;
00005 
00006 void videocallback(IplImage *image)
00007 {
00008     static IplImage *img_gray=NULL;
00009 
00010     assert(image);
00011     if (img_gray == NULL) {
00012         // Following image is toggled visible using key '0'
00013         img_gray = CvTestbed::Instance().CreateImageWithProto("Grayscale", image, 0, 1);
00014     }
00015     if (image->nChannels > 1) { 
00016         cvCvtColor(image, img_gray, CV_RGB2GRAY);
00017     } else {
00018         cvCopy(image, img_gray);
00019     }
00020     // TODO: Do your image operations
00021 }
00022 
00023 int main(int argc, char *argv[])
00024 {
00025     try {
00026         // Output usage message
00027         std::string filename(argv[0]);
00028         filename = filename.substr(filename.find_last_of('\\') + 1);
00029         std::cout << "SampleCvTestbed" << std::endl;
00030         std::cout << "===============" << std::endl;
00031         std::cout << std::endl;
00032         std::cout << "Description:" << std::endl;
00033         std::cout << "  This is an example of how to use the 'CvTestbed', 'CaptureFactory' and" << std::endl;
00034         std::cout << "  'Capture' classes. The 'CaptureFactory' can create 'Capture' objects" << std::endl;
00035         std::cout << "  from many different backends (see SampleCvTestbed.cpp). You can also" << std::endl;
00036         std::cout << "  show/hide the 1st ten images created using 'CvTestbed' using the number" << std::endl;
00037         std::cout << "  keys. In this example you can use key '0' to show/hide a grayscale" << std::endl;
00038         std::cout << "  version of the captured image." << std::endl;
00039         std::cout << std::endl;
00040         std::cout << "Usage:" << std::endl;
00041         std::cout << "  " << filename << " [device]" << std::endl;
00042         std::cout << std::endl;
00043         std::cout << "    device    integer selecting device from enumeration list (default 0)" << std::endl;
00044         std::cout << "              highgui capture devices are prefered" << std::endl;
00045         std::cout << std::endl;
00046         std::cout << "Keyboard Shortcuts:" << std::endl;
00047         std::cout << "  0: show/hide grayscale image" << std::endl;
00048         std::cout << "  q: quit" << std::endl;
00049         std::cout << std::endl;
00050 
00051         // Initialise CvTestbed
00052         CvTestbed::Instance().SetVideoCallback(videocallback);
00053 
00054         // Enumerate possible capture plugins
00055         CaptureFactory::CapturePluginVector plugins = CaptureFactory::instance()->enumeratePlugins();
00056         if (plugins.size() < 1) {
00057             std::cout << "Could not find any capture plugins." << std::endl;
00058             return 0;
00059         }
00060 
00061         // Display capture plugins
00062         std::cout << "Available Plugins: ";
00063         outputEnumeratedPlugins(plugins);
00064         std::cout << std::endl;
00065 
00066         // Enumerate possible capture devices
00067         CaptureFactory::CaptureDeviceVector devices = CaptureFactory::instance()->enumerateDevices();
00068         if (devices.size() < 1) {
00069             std::cout << "Could not find any capture devices." << std::endl;
00070             return 0;
00071         }
00072 
00073         // Check command line argument for which device to use
00074         int selectedDevice = defaultDevice(devices);
00075         if (argc > 1) {
00076             selectedDevice = atoi(argv[1]);
00077         }
00078         if (selectedDevice >= (int)devices.size()) {
00079             selectedDevice = defaultDevice(devices);
00080         }
00081 
00082         // Display capture devices
00083         std::cout << "Enumerated Capture Devices:" << std::endl;
00084         outputEnumeratedDevices(devices, selectedDevice);
00085         std::cout << std::endl;
00086 
00087         // Create capture object from camera
00088         Capture *cap = CaptureFactory::instance()->createCapture(devices[selectedDevice]);
00089         std::string uniqueName = devices[selectedDevice].uniqueName();
00090 
00091         // Handle capture lifecycle and start video capture
00092         // Note that loadSettings/saveSettings are not supported by all plugins
00093         if (cap) {
00094             std::stringstream settingsFilename;
00095             settingsFilename << "camera_settings_" << uniqueName << ".xml";
00096 
00097             cap->start();
00098             cap->setResolution(640, 480);
00099 
00100             if (cap->loadSettings(settingsFilename.str())) {
00101                 std::cout << "Loading settings: " << settingsFilename.str() << std::endl;
00102             }
00103 
00104             std::stringstream title;
00105             title << "SampleCvTestbed (" << cap->captureDevice().captureType() << ")";
00106 
00107             CvTestbed::Instance().StartVideo(cap, title.str().c_str());
00108 
00109             if (cap->saveSettings(settingsFilename.str())) {
00110                 std::cout << "Saving settings: " << settingsFilename.str() << std::endl;
00111             }
00112 
00113             cap->stop();
00114             delete cap;
00115         }
00116         else if (CvTestbed::Instance().StartVideo(0, argv[0])) {
00117         }
00118         else {
00119             std::cout << "Could not initialize the selected capture backend." << std::endl;
00120         }
00121 
00122         return 0;
00123     }
00124     catch (const std::exception &e) {
00125         std::cout << "Exception: " << e.what() << endl;
00126     }
00127     catch (...) {
00128         std::cout << "Exception: unknown" << std::endl;
00129     }
00130 }


ar_track_alvar
Author(s): Scott Niekum
autogenerated on Thu Jun 6 2019 21:12:54