SampleCamCalib.cpp
Go to the documentation of this file.
00001 #include <iostream>
00002 #include "Camera.h"
00003 #include "CvTestbed.h"
00004 #include "Shared.h"
00005 using namespace alvar;
00006 using namespace std;
00007 
00008 const int calib_count_max=50;
00009 const int etalon_rows=6;
00010 const int etalon_columns=8;
00011 std::stringstream calibrationFilename;
00012 
00013 void videocallback(IplImage *image)
00014 {
00015     static bool calibrated = false;
00016     static int calib_count=0;
00017     static Camera cam;
00018     static ProjPoints pp;
00019     static int64 prev_tick=0;
00020     static bool initialized = false;
00021 
00022     if (!initialized) {
00023       cam.SetRes(image->width, image->height);
00024       prev_tick = cvGetTickCount();
00025       initialized = true;
00026     }
00027   
00028     bool flip_image = (image->origin?true:false);
00029     if (flip_image) {
00030         cvFlip(image);
00031         image->origin = !image->origin;
00032     }
00033 
00034     assert(image);
00035     if (!calibrated) {
00036         // If we have already collected enough data to make the calibration
00037         // - We are ready to end the capture loop
00038         // - Calibrate
00039         // - Save the calibration file
00040         if (calib_count >= calib_count_max) {
00041             std::cout<<"Calibrating..."<<endl;
00042             calib_count = 0;
00043             cam.Calibrate(pp);
00044             pp.Reset();
00045             cam.SaveCalib(calibrationFilename.str().c_str());
00046             std::cout<<"Saving calibration: "<<calibrationFilename.str()<<endl;
00047             calibrated = true;
00048         } 
00049         // If we are still collecting calibration data
00050         // - For every 1.5s add calibration data from detected 7*9 chessboard (and visualize it if true)
00051         else {
00052             int64 tick = cvGetTickCount();
00053             if ((tick - prev_tick) > (cvGetTickFrequency() * 1000 * 1000 * 1.5)) {
00054                 if (pp.AddPointsUsingChessboard(image, 2.8, etalon_rows, etalon_columns, true)) {
00055                     prev_tick = tick;
00056                     calib_count++;
00057                     cout<<calib_count<<"/"<<calib_count_max<<endl;
00058                 }
00059             }
00060         }
00061     } else {
00062         if (pp.AddPointsUsingChessboard(image, 2.5, etalon_rows, etalon_columns, true)) {
00063             Pose pose;
00064             cam.CalcExteriorOrientation(pp.object_points, pp.image_points, &pose);
00065             cam.ProjectPoints(pp.object_points, &pose, pp.image_points);
00066             for (size_t i=0; i<pp.image_points.size(); i++) {
00067                 cvCircle(image, cvPoint((int)pp.image_points[i].x, (int)pp.image_points[i].y), 6, CV_RGB(0, 0, 255));
00068             }
00069             pp.Reset();
00070         }
00071     }
00072 
00073     if (flip_image) {
00074         cvFlip(image);
00075         image->origin = !image->origin;
00076     }
00077 }
00078 
00079 int main(int argc, char *argv[])
00080 {
00081     try {
00082         // Output usage message
00083         std::string filename(argv[0]);
00084         filename = filename.substr(filename.find_last_of('\\') + 1);
00085         std::cout << "SampleCamCalib" << std::endl;
00086         std::cout << "==============" << std::endl;
00087         std::cout << std::endl;
00088         std::cout << "Description:" << std::endl;
00089         std::cout << "  This is an example of how to use the 'Camera' and 'ProjPoints' classes" << std::endl;
00090         std::cout << "  to perform camera calibration. Point the camera to the chessboard" << std::endl;
00091         std::cout << "  calibration pattern (see ALVAR.pdf) from several directions until 50" << std::endl;
00092         std::cout << "  calibration images are collected. A 'calib.xml' file that contains the" << std::endl;
00093         std::cout << "  internal parameters of the camera is generated and can be used by other" << std::endl;
00094         std::cout << "  applications that require a calibrated camera." << std::endl;
00095         std::cout << std::endl;
00096         std::cout << "Usage:" << std::endl;
00097         std::cout << "  " << filename << " [device]" << 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 << std::endl;
00102         std::cout << "Keyboard Shortcuts:" << std::endl;
00103         std::cout << "  q: quit" << std::endl;
00104         std::cout << std::endl;
00105 
00106         // Initialise CvTestbed
00107         CvTestbed::Instance().SetVideoCallback(videocallback);
00108 
00109         // Enumerate possible capture plugins
00110         CaptureFactory::CapturePluginVector plugins = CaptureFactory::instance()->enumeratePlugins();
00111         if (plugins.size() < 1) {
00112             std::cout << "Could not find any capture plugins." << std::endl;
00113             return 0;
00114         }
00115 
00116         // Display capture plugins
00117         std::cout << "Available Plugins: ";
00118         outputEnumeratedPlugins(plugins);
00119         std::cout << std::endl;
00120 
00121         // Enumerate possible capture devices
00122         CaptureFactory::CaptureDeviceVector devices = CaptureFactory::instance()->enumerateDevices();
00123         if (devices.size() < 1) {
00124             std::cout << "Could not find any capture devices." << std::endl;
00125             return 0;
00126         }
00127 
00128         // Check command line argument for which device to use
00129         int selectedDevice = defaultDevice(devices);
00130         if (argc > 1) {
00131             selectedDevice = atoi(argv[1]);
00132         }
00133         if (selectedDevice >= (int)devices.size()) {
00134             selectedDevice = defaultDevice(devices);
00135         }
00136         
00137         // Display capture devices
00138         std::cout << "Enumerated Capture Devices:" << std::endl;
00139         outputEnumeratedDevices(devices, selectedDevice);
00140         std::cout << std::endl;
00141         
00142         // Create capture object from camera
00143         Capture *cap = CaptureFactory::instance()->createCapture(devices[selectedDevice]);
00144         std::string uniqueName = devices[selectedDevice].uniqueName();
00145 
00146         // Handle capture lifecycle and start video capture
00147         // Note that loadSettings/saveSettings are not supported by all plugins
00148         if (cap) {
00149             std::stringstream settingsFilename;
00150             settingsFilename << "camera_settings_" << uniqueName << ".xml";
00151             calibrationFilename << "camera_calibration_" << uniqueName << ".xml";
00152             
00153             cap->start();
00154             cap->setResolution(640, 480);
00155             
00156             if (cap->loadSettings(settingsFilename.str())) {
00157                 std::cout << "Loading settings: " << settingsFilename.str() << std::endl;
00158             }
00159 
00160             std::stringstream title;
00161             title << "SampleCamCalib (" << cap->captureDevice().captureType() << ")";
00162 
00163             CvTestbed::Instance().StartVideo(cap, title.str().c_str());
00164 
00165             if (cap->saveSettings(settingsFilename.str())) {
00166                 std::cout << "Saving settings: " << settingsFilename.str() << std::endl;
00167             }
00168 
00169             cap->stop();
00170             delete cap;
00171         }
00172         else if (CvTestbed::Instance().StartVideo(0, argv[0])) {
00173         }
00174         else {
00175             std::cout << "Could not initialize the selected capture backend." << std::endl;
00176         }
00177 
00178         return 0;
00179     }
00180     catch (const std::exception &e) {
00181         std::cout << "Exception: " << e.what() << endl;
00182     }
00183     catch (...) {
00184         std::cout << "Exception: unknown" << std::endl;
00185     }
00186 }


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