multi_cameras_calibration.cpp 4.42 KB
Newer Older
1
#include "opencv2/ccalib/omnidir.hpp"
baisheng lai's avatar
baisheng lai committed
2 3
#include "opencv2/ccalib/multicalib.hpp"
#include "opencv2/ccalib/randpattern.hpp"
4 5 6 7

using namespace std;
using namespace cv;

baisheng lai's avatar
baisheng lai committed
8
const char * usage =
9
"\n example command line for multi-camera calibration by using random pattern \n"
baisheng lai's avatar
baisheng lai committed
10
"   multi_cameras_calibration -nc 5 -pw 800 -ph 600 -ct 1 -fe 0 -nm 25 -v 0 multi_camera_omnidir.xml \n"
11
"\n"
baisheng lai's avatar
baisheng lai committed
12 13 14
" the file multi_camera_omnidir.xml is generated by imagelist_creator as \n"
" imagelist_creator multi_camera_omnidir.xml *.* \n"
" note the first filename in multi_camera_omnidir.xml is the pattern, the rest are photo names,\n"
15 16 17 18 19 20 21 22 23 24 25 26 27 28
" photo names should be in form of cameraIdx-timestamp.*, and cameraIdx starts from 0";

static void help()
{
    printf("\n This is a sample for multi-camera calibration, so far it only support random pattern,\n"
           "see randomPattern.hpp for detail. Pinhole and omnidirectional cameras are both supported, \n"
           "for omnidirectional camera, see omnidir.hpp for detail.\n"
           "Usage: mutiCamCalib \n"
           "    -nc <num_camera> # number of cameras \n"
           "    -pw <pattern_width> # physical width of random pattern \n"
           "    -ph <pattern_height> # physical height of random pattern \n"
           "    -ct <camera_type> # camera type, 0 for pinhole and 1 for omnidirectional \n"
           "    -fe # whether show feature extraction\n"
           "    -nm # number of minimal matches of an image \n"
29
		   "	-v # whether show verbose information \n"
30 31 32 33 34 35 36
           "    input_data # text file with pattern file names and a list of photo names, the file is generated by imagelist_creator \n");
    printf("\n %s", usage);
}


int main(int argc, char** argv)
{
baisheng lai's avatar
baisheng lai committed
37 38
    float patternWidth = 0.0f, patternHeight = 0.0f;
    int nCamera = 0, nMiniMatches = 0, cameraType = 0;
39 40
    const char* outputFilename = "multi-camera-results.xml";
    const char* inputFilename = 0;
baisheng lai's avatar
baisheng lai committed
41
    int showFeatureExtraction = 0, verbose = 0;
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    if (argc < 2)
    {
        help();
        return 1;
    }

    for (int i = 1; i < argc; ++i)
    {
        const char* s = argv[i];
        if (strcmp( s, "-nc") == 0)
        {
            if (sscanf( argv[++i], "%u", &nCamera) != 1 || nCamera <= 0)
            {
                return fprintf(stderr, "Invalid number of cameras \n"), -1;
            }
        }
        else if ( strcmp( s, "-pw" ) == 0 )
        {
            if (sscanf( argv[++i], "%f", &patternWidth) != 1 || patternWidth <=0 )
            {
                return fprintf(stderr, "Invalid pattern width \n"), -1;
            }
        }
        else if ( strcmp( s, "-ph" ) == 0 )
        {
            if (sscanf( argv[++i], "%f", &patternHeight) != 1 || patternHeight <=0 )
            {
                return fprintf(stderr, "Invalid pattern height \n"), -1;
            }
        }
        else if ( strcmp( s, "-ct" ) == 0 )
        {
            if (sscanf( argv[++i], "%u", &cameraType) != 1 || (cameraType !=0 && cameraType !=1 && cameraType !=2) )
            {
                return fprintf(stderr, "Invalid camera type, 0 for pinhole and 1 for omnidirectional \n"), -1;
            }
        }
        else if ( strcmp( s, "-fe" ) == 0 )
        {
            if (sscanf( argv[++i], "%u", &showFeatureExtraction) != 1 || (showFeatureExtraction !=1 && showFeatureExtraction !=0) )
            {
                return fprintf(stderr, "Not bool value, set to 0 or 1 \n"), -1;
            }
        }
        else if ( strcmp( s, "-nm" ) == 0 )
        {
            if (sscanf( argv[++i], "%u", &nMiniMatches) != 1 || nMiniMatches <=0 )
            {
                return fprintf(stderr, "Invalid number of minimal matches \n"), -1;
            }
        }
93 94 95 96 97 98 99
		else if ( strcmp( s, "-v" ) == 0 )
		{
			if (sscanf( argv[++i], "%u", &verbose) != 1 || (verbose !=1 && verbose !=0) )
			{
				return fprintf(stderr, "verbose is not bool value, set to 0 or 1 \n"), -1;
			}
		}
100 101 102 103 104 105 106 107 108 109 110
        else if( s[0] != '-')
        {
            inputFilename = s;
        }
        else
        {
            return fprintf( stderr, "Unknown option %s\n", s ), -1;
        }
    }

    // do multi-camera calibration
baisheng lai's avatar
baisheng lai committed
111
    multicalib::MultiCameraCalibration multiCalib(cameraType, nCamera, inputFilename, patternWidth, patternHeight, verbose, showFeatureExtraction, nMiniMatches);
baisheng lai's avatar
baisheng lai committed
112

113 114 115 116
    multiCalib.loadImages();
    multiCalib.initialize();
    multiCalib.optimizeExtrinsics();
    // the above three lines can be replaced by multiCalib.run();
baisheng lai's avatar
baisheng lai committed
117

118 119

	multiCalib.writeParameters(outputFilename);
120
}