#include <iostream>
{
    static bool calibrated = false;
    static int calib_count=0;
    static int64 prev_tick=0;
    static bool initialized = false;
    if (!initialized) {
      cam.
SetRes(image->width, image->height);
      prev_tick = cvGetTickCount();
      initialized = true;
    }
  
    bool flip_image = (image->origin?true:false);
    if (flip_image) {
        cvFlip(image);
        image->origin = !image->origin;
    }
    assert(image);
    if (!calibrated) {
        
        
        
        
            std::cout<<"Calibrating..."<<endl;
            calib_count = 0;
            calibrated = true;
        } 
        
        
        else {
            int64 tick = cvGetTickCount();
            if ((tick - prev_tick) > (cvGetTickFrequency() * 1000 * 1000 * 1.5)) {
                    prev_tick = tick;
                    calib_count++;
                }
            }
        }
    } else {
            }
        }
    }
    if (flip_image) {
        cvFlip(image);
        image->origin = !image->origin;
    }
}
int main(
int argc, 
char *argv[])
 {
    try {
        
        filename = filename.substr(filename.find_last_of('\\') + 1);
        std::cout << "SampleCamCalib" << std::endl;
        std::cout << "==============" << std::endl;
        std::cout << std::endl;
        std::cout << "Description:" << std::endl;
        std::cout << "  This is an example of how to use the 'Camera' and 'ProjPoints' classes" << std::endl;
        std::cout << "  to perform camera calibration. Point the camera to the chessboard" << std::endl;
        std::cout << "  calibration pattern (see ALVAR.pdf) from several directions until 50" << std::endl;
        std::cout << "  calibration images are collected. A 'calib.xml' file that contains the" << std::endl;
        std::cout << "  internal parameters of the camera is generated and can be used by other" << std::endl;
        std::cout << "  applications that require a calibrated camera." << std::endl;
        std::cout << std::endl;
        std::cout << "Usage:" << std::endl;
        std::cout << "  " << filename << " [device]" << std::endl;
        std::cout << std::endl;
        std::cout << "    device    integer selecting device from enumeration list (default 0)" << std::endl;
        std::cout << "              highgui capture devices are prefered" << std::endl;
        std::cout << std::endl;
        std::cout << "Keyboard Shortcuts:" << std::endl;
        std::cout << "  q: quit" << std::endl;
        std::cout << std::endl;
        
        
        if (plugins.size() < 1) {
            std::cout << "Could not find any capture plugins." << std::endl;
            return 0;
        }
        
        std::cout << "Available Plugins: ";
        std::cout << std::endl;
        
        if (devices.size() < 1) {
            std::cout << "Could not find any capture devices." << std::endl;
            return 0;
        }
        
        if (argc > 1) {
            selectedDevice = atoi(argv[1]);
        }
        if (selectedDevice >= (int)devices.size()) {
        }
        
        
        std::cout << "Enumerated Capture Devices:" << std::endl;
        std::cout << std::endl;
        
        
        std::string uniqueName = devices[selectedDevice].uniqueName();
        
        
        if (cap) {
            std::stringstream settingsFilename;
            settingsFilename << "camera_settings_" << uniqueName << ".xml";
            
            
                std::cout << "Loading settings: " << settingsFilename.str() << std::endl;
            }
            std::stringstream title;
                std::cout << "Saving settings: " << settingsFilename.str() << std::endl;
            }
            delete cap;
        }
        }
        else {
            std::cout << "Could not initialize the selected capture backend." << std::endl;
        }
        return 0;
    }
    catch (const std::exception &e) {
        std::cout << "Exception: " << e.what() << endl;
    }
    catch (...) {
        std::cout << "Exception: unknown" << std::endl;
    }
}