calibration_standalone.cpp
Go to the documentation of this file.
00001 /*
00002  * Calibrate (i.e. find intrinsic and extrinsic calibration parameters) cameras
00003  * using left-right sets of images
00004  *
00005  * Copyright (c) 2009 Radu Bogdan Rusu <rusu -=- cs.tum.edu>
00006  *
00007  * All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions are met:
00011  *
00012  *     * Redistributions of source code must retain the above copyright
00013  *       notice, this list of conditions and the following disclaimer.
00014  *     * Redistributions in binary form must reproduce the above copyright
00015  *       notice, this list of conditions and the following disclaimer in the
00016  *       documentation and/or other materials provided with the distribution.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00022  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028  * POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  * $Id: calibration_standalone.cpp 189 2009-09-19 11:59:29Z veedee $
00031  */
00032 
00033 #include "calibration_standalone.h"
00034 
00035 
00037 // Set some default values for the intrinsic parameters of one of our cameras
00038 void
00039   setInitialIntrinsicParameters (std::vector<SensorDataEntry> &inputData)
00040 {
00041   SensorDataEntry s;
00042   s.sensor = "stoc";
00043   s.camera = "left";
00044   std::vector<SensorDataEntry>::iterator it = find (inputData.begin (), inputData.end (), s);
00045   if (it != inputData.end ())
00046   {
00047     SensorDataEntry *pen = &(*it);
00048     cvmSet (pen->intrinsic, 0, 0, 1338.77747); cvmSet (pen->intrinsic, 0, 1, 0.0);     cvmSet (pen->intrinsic, 0, 2, 552.81012);
00049     cvmSet (pen->intrinsic, 1, 0, 0.0);     cvmSet (pen->intrinsic, 1, 1, 1338.77747); cvmSet (pen->intrinsic, 1, 2, 374.58749);
00050     cvmSet (pen->intrinsic, 2, 0, 0.0);     cvmSet (pen->intrinsic, 2, 1, 0.0);     cvmSet (pen->intrinsic, 2, 2, 1.0);
00051     cvmSet (pen->distortion, 0, 0, 0.04920);    cvmSet (pen->distortion, 0, 1, -0.20187);    cvmSet (pen->distortion, 0, 2, 0.0);  cvmSet (pen->distortion, 0, 3, 0.0);
00052   }
00053 
00054   s.sensor = "stoc";
00055   s.camera = "right";
00056   it = find (inputData.begin (), inputData.end (), s);
00057   if (it != inputData.end ())
00058   {
00059     SensorDataEntry *pen = &(*it);
00060     cvmSet (pen->intrinsic, 0, 0, 1339.43933); cvmSet (pen->intrinsic, 0, 1, 0.0);     cvmSet (pen->intrinsic, 0, 2, 503.67599);
00061     cvmSet (pen->intrinsic, 1, 0, 0.0);     cvmSet (pen->intrinsic, 1, 1, 1339.43933); cvmSet (pen->intrinsic, 1, 2, 394.59033);
00062     cvmSet (pen->intrinsic, 2, 0, 0.0);     cvmSet (pen->intrinsic, 2, 1, 0.0);     cvmSet (pen->intrinsic, 2, 2, 1.0);
00063     cvmSet (pen->distortion, 0, 0, -0.00809);    cvmSet (pen->distortion, 0, 1, -0.09067);    cvmSet (pen->distortion, 0, 2, 0.0);  cvmSet (pen->distortion, 0, 3, 0.0);
00064   }
00065 }
00066 
00067 /* ---[ */
00068 int
00069   main (int argc, char** argv)
00070 {
00071   std::vector<int> badPairs;   // this vector holds any image pair number that doesn't produce enough corners for a given sensor
00072 
00073   double max_admissible_err = FLT_MAX;
00074   if (argc < 2)
00075   {
00076     fprintf (stderr, "Syntax is: %s <file1..N.{jpg,png}*> <options>\n", argv[0]);
00077     fprintf (stderr, "  where options are:\n");
00078     fprintf (stderr, "                     -squares X,Y = use these numbers of squares to define the checkboard pattern\n");
00079     fprintf (stderr, "                     -sq_size X   = square size in mm\n");
00080     fprintf (stderr, "\n");
00081     fprintf (stderr, "                     -ith X   = get only every Xth pair (useful if we have over 100 pairs of recorded images)\n");
00082     fprintf (stderr, "\n");
00083     fprintf (stderr, "                     -max_err X = ignore a pair if the pixel reprojection error is larger than X (default: MAX_FLT)\n");
00084     fprintf (stderr, "\n");
00085     fprintf (stderr, "                     -wait X   = how much time to wait between each image (default : 0 - wait for a keypress)\n");
00086     fprintf (stderr, "\n");
00087     fprintf (stderr, "* note: the number of files will be parsed as folows: NR-SID-CID.png, where:\n");
00088     fprintf (stderr, "                     - NR  = the actual image pair number\n");
00089     fprintf (stderr, "                     - SID = a string identifier which defines the sensor used (e.g. stoc, sr4k, etc)\n");
00090     fprintf (stderr, "                     - CID = a string identifier which defines the camera used (e.g. left, right, intensity, etc)\n");
00091     return (-1);
00092   }
00093 
00094   // Parse the command line arguments for options
00095   int wait = 0;
00096   ParseArgument (argc, argv, "-wait", wait);
00097   
00098   ParseArgument (argc, argv, "-max_err", max_admissible_err);
00099 
00100   int sq_size = SQ_SIZE;
00101   ParseArgument (argc, argv, "-sq_size", sq_size);
00102 
00103   int ith = 1;
00104   ParseArgument (argc, argv, "-ith", ith);
00105 
00106   // Create the checkboard pattern
00107   int nr_sqr_x = NR_SQUARE_X,
00108       nr_sqr_y = NR_SQUARE_Y;
00109   ParseRangeArguments (argc, argv, "-squares", nr_sqr_x, nr_sqr_y);
00110   CvSize checkboard = cvSize (nr_sqr_x, nr_sqr_y);
00111 
00112   fprintf (stderr, "Using %d, %d with %d checkboard.\n", nr_sqr_x, nr_sqr_y, sq_size);
00113 
00114   // Parse the command line arguments for .png files
00115   std::vector<int> pPNGFileIndices;
00116   pPNGFileIndices = ParseFileExtensionArgument (argc, argv, ".png");
00117 
00118   if (pPNGFileIndices.size () == 0)
00119     pPNGFileIndices = ParseFileExtensionArgument (argc, argv, ".jpg");
00120 
00121   std::vector<SensorDataEntry> inputData = createSensorEntries (argc, argv, pPNGFileIndices, checkboard, ith);
00122 
00123   fprintf (stderr, "Maximum admissible pixel reprojection error for an image: "); fprintf (stderr, "%g\n", max_admissible_err);
00124 
00125   setInitialIntrinsicParameters (inputData);
00126 
00127   // Get the extrinsic parameters of each sensor model from the given input images
00128   int nr_valid_pairs = 0;
00129   for (unsigned int i = 0; i < inputData.size (); i++)
00130   {
00131     SensorDataEntry *s = &inputData.at (i);
00132     nr_valid_pairs = computeIntrinsicParameters (inputData.at (i), checkboard, sq_size, badPairs,
00133                                                  max_admissible_err, wait);
00134     if (nr_valid_pairs == -1)
00135       return (-1);
00136 
00137     computeExtrinsicParameters (inputData.at (i), checkboard, nr_valid_pairs);
00138 
00139     fprintf (stderr, " >>> INTRINSIC PARAMETERS for "); fprintf (stderr, "%s-%s\n", s->sensor.c_str (), s->camera.c_str ());
00140     printMatrix (s->intrinsic);
00141     fprintf (stderr, "   Camera Distortion Coefficients\n");
00142     printMatrix (s->distortion);
00143   }
00144 
00145   // Compute the extrinsic parameters between 2 sensors
00146   int nr_pairs = nr_valid_pairs; 
00147   int nums[nr_pairs];
00148   for (int i = 0; i < nr_pairs; i++)
00149     nums[i] = checkboard.height * checkboard.width;
00150 
00151   getExtrinsicBetween ("stoc-left", "stoc-right", checkboard, inputData, nr_pairs, nums);
00152   getExtrinsicBetween ("stoc-right", "stoc-left", checkboard, inputData, nr_pairs, nums);
00153 
00154   finishUp (inputData);
00155   return (0);
00156 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends Defines


camera_calibration_standalone
Author(s): Radu Bogdan Rusu (rusu@cs.tum.edu)
autogenerated on Thu May 23 2013 13:08:30