2010-06-13 05:50:29 +02:00
|
|
|
#include "opencv2/core/core.hpp"
|
|
|
|
#include "opencv2/imgproc/imgproc.hpp"
|
|
|
|
#include "opencv2/calib3d/calib3d.hpp"
|
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
using namespace std;
|
|
|
|
|
2010-11-24 02:44:36 +01:00
|
|
|
void help()
|
|
|
|
{
|
|
|
|
printf( "This is a camera calibration sample.\n"
|
|
|
|
"Usage: calibration\n"
|
2011-06-09 10:04:33 +02:00
|
|
|
" -w=<board_width> # the number of inner corners per one of board dimension\n"
|
|
|
|
" -h=<board_height> # the number of inner corners per another board dimension\n"
|
|
|
|
" [-pt]=<pattern> # the type of pattern: chessboard or circles' grid\n"
|
|
|
|
" [-n]=<number_of_frames> # the number of frames to use for calibration\n"
|
2010-11-24 02:44:36 +01:00
|
|
|
" # (if not specified, it will be set to the number\n"
|
|
|
|
" # of board views actually available)\n"
|
2011-06-09 10:04:33 +02:00
|
|
|
" [-d]=<delay> # a minimum delay in ms between subsequent attempts to capture a next view\n"
|
2010-11-24 02:44:36 +01:00
|
|
|
" # (used only for video capturing)\n"
|
2011-06-09 10:04:33 +02:00
|
|
|
" [-s]=<squareSize> # square size in some user-defined units (1 by default)\n"
|
|
|
|
" [-o]=<out_camera_params> # the output filename for intrinsic [and extrinsic] parameters\n"
|
2010-11-24 02:44:36 +01:00
|
|
|
" [-op] # write detected feature points\n"
|
|
|
|
" [-oe] # write extrinsic parameters\n"
|
|
|
|
" [-zt] # assume zero tangential distortion\n"
|
2011-06-09 10:04:33 +02:00
|
|
|
" [-a]=<aspectRatio> # fix aspect ratio (fx/fy)\n"
|
2010-11-24 02:44:36 +01:00
|
|
|
" [-p] # fix the principal point at the center\n"
|
|
|
|
" [-v] # flip the captured images around the horizontal axis\n"
|
2010-11-30 00:16:41 +01:00
|
|
|
" [-V] # use a video file, and not an image list, uses\n"
|
|
|
|
" # [input_data] string for the video file name\n"
|
2010-11-24 02:44:36 +01:00
|
|
|
" [-su] # show undistorted images after calibration\n"
|
2011-06-09 10:04:33 +02:00
|
|
|
" [-input_data]=<data file> # input data, one of the following:\n"
|
2010-11-24 02:44:36 +01:00
|
|
|
" # - text file with a list of the images of the board\n"
|
|
|
|
" # the text file can be generated with imagelist_creator\n"
|
|
|
|
" # - name of video file with a video of the board\n"
|
2011-06-09 10:04:33 +02:00
|
|
|
" [-cameraId]=<camera index># if input_data not specified, a live view from the camera is used\n"
|
|
|
|
" \nExample command line for calibration from a live feed:\n"
|
|
|
|
" ./calibration -w=4 -h=5 -s=0.025 -o=camera.yml -op -oe\n"
|
|
|
|
" \n"
|
|
|
|
" Example command line for calibration from a list of stored images:\n"
|
|
|
|
" imagelist_creator image_list.xml *.png\n"
|
|
|
|
" ./calibration -w=4 -h-5 -s=0.025 -o=camera.yml -op -oe -input_data=image_list.xml\n"
|
|
|
|
" where image_list.xml is the standard OpenCV XML/YAML\n"
|
|
|
|
" use imagelist_creator to create the xml or yaml list\n"
|
|
|
|
" file consisting of the list of strings, e.g.:\n"
|
|
|
|
" \n"
|
|
|
|
"<?xml version=\"1.0\"?>\n"
|
|
|
|
"<opencv_storage>\n"
|
|
|
|
"<images>\n"
|
|
|
|
"view000.png\n"
|
|
|
|
"view001.png\n"
|
|
|
|
"<!-- view002.png -->\n"
|
|
|
|
"view003.png\n"
|
|
|
|
"view010.png\n"
|
|
|
|
"one_extra_view.jpg\n"
|
|
|
|
"</images>\n"
|
|
|
|
"</opencv_storage>\n"
|
|
|
|
"\nWhen the live video from camera is used as input, the following hot-keys may be used:\n"
|
|
|
|
" <ESC>, 'q' - quit the program\n"
|
|
|
|
" 'g' - start capturing images\n"
|
|
|
|
" 'u' - switch undistortion on/off\n");
|
2010-11-24 02:44:36 +01:00
|
|
|
}
|
2010-06-13 05:50:29 +02:00
|
|
|
|
|
|
|
enum { DETECTION = 0, CAPTURING = 1, CALIBRATED = 2 };
|
2011-02-07 08:59:01 +01:00
|
|
|
enum Pattern { CHESSBOARD, CIRCLES_GRID, ASYMMETRIC_CIRCLES_GRID };
|
2010-06-13 05:50:29 +02:00
|
|
|
|
|
|
|
static double computeReprojectionErrors(
|
|
|
|
const vector<vector<Point3f> >& objectPoints,
|
|
|
|
const vector<vector<Point2f> >& imagePoints,
|
|
|
|
const vector<Mat>& rvecs, const vector<Mat>& tvecs,
|
|
|
|
const Mat& cameraMatrix, const Mat& distCoeffs,
|
|
|
|
vector<float>& perViewErrors )
|
|
|
|
{
|
|
|
|
vector<Point2f> imagePoints2;
|
|
|
|
int i, totalPoints = 0;
|
|
|
|
double totalErr = 0, err;
|
|
|
|
perViewErrors.resize(objectPoints.size());
|
|
|
|
|
|
|
|
for( i = 0; i < (int)objectPoints.size(); i++ )
|
|
|
|
{
|
|
|
|
projectPoints(Mat(objectPoints[i]), rvecs[i], tvecs[i],
|
|
|
|
cameraMatrix, distCoeffs, imagePoints2);
|
2010-11-30 11:27:34 +01:00
|
|
|
err = norm(Mat(imagePoints[i]), Mat(imagePoints2), CV_L2);
|
2010-06-13 05:50:29 +02:00
|
|
|
int n = (int)objectPoints[i].size();
|
2010-11-30 11:27:34 +01:00
|
|
|
perViewErrors[i] = (float)std::sqrt(err*err/n);
|
|
|
|
totalErr += err*err;
|
2010-06-13 05:50:29 +02:00
|
|
|
totalPoints += n;
|
|
|
|
}
|
|
|
|
|
2010-11-30 11:27:34 +01:00
|
|
|
return std::sqrt(totalErr/totalPoints);
|
2010-06-13 05:50:29 +02:00
|
|
|
}
|
|
|
|
|
2011-02-07 08:59:01 +01:00
|
|
|
static void calcChessboardCorners(Size boardSize, float squareSize, vector<Point3f>& corners, Pattern patternType = CHESSBOARD)
|
2010-06-13 05:50:29 +02:00
|
|
|
{
|
|
|
|
corners.resize(0);
|
|
|
|
|
2011-02-07 08:59:01 +01:00
|
|
|
switch(patternType)
|
|
|
|
{
|
|
|
|
case CHESSBOARD:
|
|
|
|
case CIRCLES_GRID:
|
|
|
|
for( int i = 0; i < boardSize.height; i++ )
|
|
|
|
for( int j = 0; j < boardSize.width; j++ )
|
|
|
|
corners.push_back(Point3f(float(j*squareSize),
|
|
|
|
float(i*squareSize), 0));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ASYMMETRIC_CIRCLES_GRID:
|
|
|
|
for( int i = 0; i < boardSize.height; i++ )
|
|
|
|
for( int j = 0; j < boardSize.width; j++ )
|
|
|
|
corners.push_back(Point3f(float((2*j + i % 2)*squareSize),
|
|
|
|
float(i*squareSize), 0));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
CV_Error(CV_StsBadArg, "Unknown pattern type\n");
|
|
|
|
}
|
2010-06-13 05:50:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool runCalibration( vector<vector<Point2f> > imagePoints,
|
2011-02-07 08:59:01 +01:00
|
|
|
Size imageSize, Size boardSize, Pattern patternType,
|
2010-06-13 05:50:29 +02:00
|
|
|
float squareSize, float aspectRatio,
|
|
|
|
int flags, Mat& cameraMatrix, Mat& distCoeffs,
|
|
|
|
vector<Mat>& rvecs, vector<Mat>& tvecs,
|
|
|
|
vector<float>& reprojErrs,
|
|
|
|
double& totalAvgErr)
|
|
|
|
{
|
|
|
|
cameraMatrix = Mat::eye(3, 3, CV_64F);
|
|
|
|
if( flags & CV_CALIB_FIX_ASPECT_RATIO )
|
|
|
|
cameraMatrix.at<double>(0,0) = aspectRatio;
|
|
|
|
|
2010-09-07 17:38:48 +02:00
|
|
|
distCoeffs = Mat::zeros(8, 1, CV_64F);
|
2010-06-13 05:50:29 +02:00
|
|
|
|
|
|
|
vector<vector<Point3f> > objectPoints(1);
|
2011-02-07 08:59:01 +01:00
|
|
|
calcChessboardCorners(boardSize, squareSize, objectPoints[0], patternType);
|
2010-08-18 17:35:56 +02:00
|
|
|
|
2010-11-30 00:16:41 +01:00
|
|
|
objectPoints.resize(imagePoints.size(),objectPoints[0]);
|
2010-06-13 05:50:29 +02:00
|
|
|
|
2010-11-30 11:27:34 +01:00
|
|
|
double rms = calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix,
|
2010-11-30 00:16:41 +01:00
|
|
|
distCoeffs, rvecs, tvecs, flags|CV_CALIB_FIX_K4|CV_CALIB_FIX_K5);
|
|
|
|
///*|CV_CALIB_FIX_K3*/|CV_CALIB_FIX_K4|CV_CALIB_FIX_K5);
|
2010-11-30 11:27:34 +01:00
|
|
|
printf("RMS error reported by calibrateCamera: %g\n", rms);
|
2010-06-13 05:50:29 +02:00
|
|
|
|
2010-07-17 12:35:17 +02:00
|
|
|
bool ok = checkRange(cameraMatrix) && checkRange(distCoeffs);
|
2010-06-13 05:50:29 +02:00
|
|
|
|
|
|
|
totalAvgErr = computeReprojectionErrors(objectPoints, imagePoints,
|
|
|
|
rvecs, tvecs, cameraMatrix, distCoeffs, reprojErrs);
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void saveCameraParams( const string& filename,
|
|
|
|
Size imageSize, Size boardSize,
|
|
|
|
float squareSize, float aspectRatio, int flags,
|
|
|
|
const Mat& cameraMatrix, const Mat& distCoeffs,
|
|
|
|
const vector<Mat>& rvecs, const vector<Mat>& tvecs,
|
|
|
|
const vector<float>& reprojErrs,
|
|
|
|
const vector<vector<Point2f> >& imagePoints,
|
|
|
|
double totalAvgErr )
|
|
|
|
{
|
|
|
|
FileStorage fs( filename, FileStorage::WRITE );
|
|
|
|
|
|
|
|
time_t t;
|
|
|
|
time( &t );
|
|
|
|
struct tm *t2 = localtime( &t );
|
|
|
|
char buf[1024];
|
|
|
|
strftime( buf, sizeof(buf)-1, "%c", t2 );
|
|
|
|
|
|
|
|
fs << "calibration_time" << buf;
|
|
|
|
|
|
|
|
if( !rvecs.empty() || !reprojErrs.empty() )
|
|
|
|
fs << "nframes" << (int)std::max(rvecs.size(), reprojErrs.size());
|
|
|
|
fs << "image_width" << imageSize.width;
|
|
|
|
fs << "image_height" << imageSize.height;
|
|
|
|
fs << "board_width" << boardSize.width;
|
|
|
|
fs << "board_height" << boardSize.height;
|
2010-06-24 09:56:57 +02:00
|
|
|
fs << "square_size" << squareSize;
|
2010-06-13 05:50:29 +02:00
|
|
|
|
|
|
|
if( flags & CV_CALIB_FIX_ASPECT_RATIO )
|
|
|
|
fs << "aspectRatio" << aspectRatio;
|
|
|
|
|
|
|
|
if( flags != 0 )
|
|
|
|
{
|
|
|
|
sprintf( buf, "flags: %s%s%s%s",
|
|
|
|
flags & CV_CALIB_USE_INTRINSIC_GUESS ? "+use_intrinsic_guess" : "",
|
|
|
|
flags & CV_CALIB_FIX_ASPECT_RATIO ? "+fix_aspectRatio" : "",
|
|
|
|
flags & CV_CALIB_FIX_PRINCIPAL_POINT ? "+fix_principal_point" : "",
|
|
|
|
flags & CV_CALIB_ZERO_TANGENT_DIST ? "+zero_tangent_dist" : "" );
|
|
|
|
cvWriteComment( *fs, buf, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
fs << "flags" << flags;
|
|
|
|
|
|
|
|
fs << "camera_matrix" << cameraMatrix;
|
|
|
|
fs << "distortion_coefficients" << distCoeffs;
|
|
|
|
|
|
|
|
fs << "avg_reprojection_error" << totalAvgErr;
|
|
|
|
if( !reprojErrs.empty() )
|
|
|
|
fs << "per_view_reprojection_errors" << Mat(reprojErrs);
|
|
|
|
|
|
|
|
if( !rvecs.empty() && !tvecs.empty() )
|
|
|
|
{
|
2011-05-30 10:01:09 +02:00
|
|
|
CV_Assert(rvecs[0].type() == tvecs[0].type());
|
|
|
|
Mat bigmat((int)rvecs.size(), 6, rvecs[0].type());
|
2010-11-25 17:55:46 +01:00
|
|
|
for( int i = 0; i < (int)rvecs.size(); i++ )
|
2010-06-13 05:50:29 +02:00
|
|
|
{
|
|
|
|
Mat r = bigmat(Range(i, i+1), Range(0,3));
|
|
|
|
Mat t = bigmat(Range(i, i+1), Range(3,6));
|
2011-05-30 10:01:09 +02:00
|
|
|
|
|
|
|
CV_Assert(rvecs[i].rows == 3 && rvecs[i].cols == 1);
|
|
|
|
CV_Assert(tvecs[i].rows == 3 && tvecs[i].cols == 1);
|
|
|
|
//*.t() is MatExpr (not Mat) so we can use assignment operator
|
|
|
|
r = rvecs[i].t();
|
|
|
|
t = tvecs[i].t();
|
2010-06-13 05:50:29 +02:00
|
|
|
}
|
|
|
|
cvWriteComment( *fs, "a set of 6-tuples (rotation vector + translation vector) for each view", 0 );
|
|
|
|
fs << "extrinsic_parameters" << bigmat;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !imagePoints.empty() )
|
|
|
|
{
|
2010-11-25 17:55:46 +01:00
|
|
|
Mat imagePtMat((int)imagePoints.size(), imagePoints[0].size(), CV_32FC2);
|
|
|
|
for( int i = 0; i < (int)imagePoints.size(); i++ )
|
2010-06-13 05:50:29 +02:00
|
|
|
{
|
|
|
|
Mat r = imagePtMat.row(i).reshape(2, imagePtMat.cols);
|
2010-07-09 11:09:20 +02:00
|
|
|
Mat imgpti(imagePoints[i]);
|
|
|
|
imgpti.copyTo(r);
|
2010-06-13 05:50:29 +02:00
|
|
|
}
|
|
|
|
fs << "image_points" << imagePtMat;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-13 21:08:24 +02:00
|
|
|
static bool readStringList( const string& filename, vector<string>& l )
|
2010-06-13 05:50:29 +02:00
|
|
|
{
|
|
|
|
l.resize(0);
|
2010-06-13 21:08:24 +02:00
|
|
|
FileStorage fs(filename, FileStorage::READ);
|
|
|
|
if( !fs.isOpened() )
|
2010-06-13 05:50:29 +02:00
|
|
|
return false;
|
2010-06-13 21:08:24 +02:00
|
|
|
FileNode n = fs.getFirstTopLevelNode();
|
|
|
|
if( n.type() != FileNode::SEQ )
|
|
|
|
return false;
|
|
|
|
FileNodeIterator it = n.begin(), it_end = n.end();
|
|
|
|
for( ; it != it_end; ++it )
|
|
|
|
l.push_back((string)*it);
|
2010-06-13 05:50:29 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool runAndSave(const string& outputFilename,
|
|
|
|
const vector<vector<Point2f> >& imagePoints,
|
2011-02-07 08:59:01 +01:00
|
|
|
Size imageSize, Size boardSize, Pattern patternType, float squareSize,
|
2010-06-13 05:50:29 +02:00
|
|
|
float aspectRatio, int flags, Mat& cameraMatrix,
|
|
|
|
Mat& distCoeffs, bool writeExtrinsics, bool writePoints )
|
|
|
|
{
|
|
|
|
vector<Mat> rvecs, tvecs;
|
|
|
|
vector<float> reprojErrs;
|
|
|
|
double totalAvgErr = 0;
|
|
|
|
|
2011-02-07 08:59:01 +01:00
|
|
|
bool ok = runCalibration(imagePoints, imageSize, boardSize, patternType, squareSize,
|
2010-06-13 05:50:29 +02:00
|
|
|
aspectRatio, flags, cameraMatrix, distCoeffs,
|
|
|
|
rvecs, tvecs, reprojErrs, totalAvgErr);
|
|
|
|
printf("%s. avg reprojection error = %.2f\n",
|
|
|
|
ok ? "Calibration succeeded" : "Calibration failed",
|
|
|
|
totalAvgErr);
|
|
|
|
|
|
|
|
if( ok )
|
|
|
|
saveCameraParams( outputFilename, imageSize,
|
|
|
|
boardSize, squareSize, aspectRatio,
|
|
|
|
flags, cameraMatrix, distCoeffs,
|
|
|
|
writeExtrinsics ? rvecs : vector<Mat>(),
|
|
|
|
writeExtrinsics ? tvecs : vector<Mat>(),
|
|
|
|
writeExtrinsics ? reprojErrs : vector<float>(),
|
|
|
|
writePoints ? imagePoints : vector<vector<Point2f> >(),
|
|
|
|
totalAvgErr );
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-09 10:04:33 +02:00
|
|
|
int main( int argc, const char** argv )
|
2010-06-13 05:50:29 +02:00
|
|
|
{
|
2011-06-09 10:04:33 +02:00
|
|
|
help();
|
|
|
|
CommandLineParser parser(argc, argv);
|
|
|
|
|
2010-06-13 05:50:29 +02:00
|
|
|
Size boardSize, imageSize;
|
2011-06-09 10:04:33 +02:00
|
|
|
boardSize.width = parser.get<int>("w");
|
|
|
|
boardSize.height = parser.get<int>("h");
|
|
|
|
float squareSize = parser.get<float>("s", 1.f);
|
|
|
|
float aspectRatio = parser.get<float>("a", 1.f);
|
2010-06-13 05:50:29 +02:00
|
|
|
Mat cameraMatrix, distCoeffs;
|
2011-06-09 10:04:33 +02:00
|
|
|
string outputFilename = parser.get<string>("o","out_camera_data.yml");
|
|
|
|
string inputFilename = parser.get<string>("input_data");
|
|
|
|
int nframes = parser.get<int>("n", 10);
|
|
|
|
bool writeExtrinsics = parser.get<bool>("oe");
|
|
|
|
bool writePoints = parser.get<bool>("op");
|
|
|
|
bool flipVertical = parser.get<bool>("v");
|
|
|
|
bool showUndistorted = parser.get<bool>("su");
|
|
|
|
bool videofile = parser.get<bool>("V");
|
|
|
|
unsigned int delay = parser.get<unsigned int>("d", 1000);
|
|
|
|
unsigned int cameraId = parser.get<unsigned int>("cameraId",0);
|
2010-06-13 05:50:29 +02:00
|
|
|
bool undistortImage = false;
|
|
|
|
int flags = 0;
|
|
|
|
VideoCapture capture;
|
|
|
|
clock_t prevTimestamp = 0;
|
|
|
|
int mode = DETECTION;
|
|
|
|
vector<vector<Point2f> > imagePoints;
|
|
|
|
vector<string> imageList;
|
2010-12-21 10:32:14 +01:00
|
|
|
Pattern pattern = CHESSBOARD;
|
2010-06-13 05:50:29 +02:00
|
|
|
|
2011-06-09 10:04:33 +02:00
|
|
|
if( (boardSize.width < 1) || (boardSize.height < 1))
|
2010-06-13 05:50:29 +02:00
|
|
|
{
|
2010-11-30 00:16:41 +01:00
|
|
|
help();
|
2011-06-09 10:04:33 +02:00
|
|
|
return fprintf( stderr, "Invalid board width or height. It must be more than zero\n" ), -1;
|
2010-06-13 05:50:29 +02:00
|
|
|
}
|
|
|
|
|
2011-06-09 10:04:33 +02:00
|
|
|
if(parser.get<string>("pt")=="circles")
|
|
|
|
pattern = CIRCLES_GRID;
|
|
|
|
else if(parser.get<string>("pt")=="acircles")
|
|
|
|
pattern = ASYMMETRIC_CIRCLES_GRID;
|
|
|
|
if(squareSize <= 0)
|
2010-06-13 05:50:29 +02:00
|
|
|
{
|
2011-06-09 10:04:33 +02:00
|
|
|
help();
|
|
|
|
return fprintf( stderr, "Invalid board square width. It must be more than zero.\n" ), -1;
|
|
|
|
}
|
|
|
|
if(nframes < 4)
|
|
|
|
{
|
|
|
|
help();
|
|
|
|
return printf("Invalid number of images. It must be more than 3\n" ), -1;
|
|
|
|
}
|
|
|
|
if(aspectRatio <= 0)
|
|
|
|
{
|
|
|
|
help();
|
|
|
|
return printf("Invalid aspect ratio. It must be more than zero\n" ), -1;
|
|
|
|
}
|
2010-06-13 05:50:29 +02:00
|
|
|
else
|
2011-06-09 10:04:33 +02:00
|
|
|
flags |= CV_CALIB_FIX_ASPECT_RATIO;
|
|
|
|
if(!delay)
|
|
|
|
{
|
|
|
|
help();
|
|
|
|
return printf("Invalid delay. It must be more than zero.\n" ), -1;
|
2010-06-13 05:50:29 +02:00
|
|
|
}
|
2011-06-09 10:04:33 +02:00
|
|
|
if(parser.get<bool>("zt"))
|
|
|
|
flags |= CV_CALIB_ZERO_TANGENT_DIST;
|
|
|
|
if(parser.get<bool>("p"))
|
|
|
|
flags |= CV_CALIB_FIX_PRINCIPAL_POINT;
|
2010-06-13 05:50:29 +02:00
|
|
|
|
2011-06-09 10:04:33 +02:00
|
|
|
if( !inputFilename.empty() )
|
2010-06-13 05:50:29 +02:00
|
|
|
{
|
2010-11-30 00:16:41 +01:00
|
|
|
if( !videofile && readStringList(inputFilename, imageList) )
|
2010-06-13 21:08:24 +02:00
|
|
|
mode = CAPTURING;
|
|
|
|
else
|
|
|
|
capture.open(inputFilename);
|
2010-06-13 05:50:29 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
capture.open(cameraId);
|
|
|
|
|
|
|
|
if( !capture.isOpened() && imageList.empty() )
|
2011-02-13 05:09:58 +01:00
|
|
|
return fprintf( stderr, "Could not initialize video (%d) capture\n",cameraId ), -2;
|
2010-06-13 05:50:29 +02:00
|
|
|
|
|
|
|
if( !imageList.empty() )
|
|
|
|
nframes = (int)imageList.size();
|
|
|
|
|
|
|
|
namedWindow( "Image View", 1 );
|
|
|
|
|
2011-06-09 10:04:33 +02:00
|
|
|
int i;
|
2010-06-13 05:50:29 +02:00
|
|
|
for(i = 0;;i++)
|
|
|
|
{
|
|
|
|
Mat view, viewGray;
|
|
|
|
bool blink = false;
|
|
|
|
|
|
|
|
if( capture.isOpened() )
|
|
|
|
{
|
|
|
|
Mat view0;
|
|
|
|
capture >> view0;
|
|
|
|
view0.copyTo(view);
|
|
|
|
}
|
|
|
|
else if( i < (int)imageList.size() )
|
|
|
|
view = imread(imageList[i], 1);
|
|
|
|
|
|
|
|
if(!view.data)
|
|
|
|
{
|
|
|
|
if( imagePoints.size() > 0 )
|
|
|
|
runAndSave(outputFilename, imagePoints, imageSize,
|
2011-02-07 08:59:01 +01:00
|
|
|
boardSize, pattern, squareSize, aspectRatio,
|
2010-06-13 05:50:29 +02:00
|
|
|
flags, cameraMatrix, distCoeffs,
|
|
|
|
writeExtrinsics, writePoints);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
imageSize = view.size();
|
|
|
|
|
|
|
|
if( flipVertical )
|
|
|
|
flip( view, view, 0 );
|
|
|
|
|
|
|
|
vector<Point2f> pointbuf;
|
2010-11-01 07:20:36 +01:00
|
|
|
cvtColor(view, viewGray, CV_BGR2GRAY);
|
2010-11-30 00:16:41 +01:00
|
|
|
|
2010-12-21 10:32:14 +01:00
|
|
|
bool found;
|
|
|
|
switch( pattern )
|
|
|
|
{
|
|
|
|
case CHESSBOARD:
|
|
|
|
found = findChessboardCorners( view, boardSize, pointbuf,
|
2011-05-30 08:18:52 +02:00
|
|
|
CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FAST_CHECK | CV_CALIB_CB_NORMALIZE_IMAGE);
|
2010-12-21 10:32:14 +01:00
|
|
|
break;
|
2011-02-07 08:59:01 +01:00
|
|
|
case CIRCLES_GRID:
|
2010-12-21 10:32:14 +01:00
|
|
|
found = findCirclesGrid( view, boardSize, pointbuf );
|
|
|
|
break;
|
2011-02-07 08:59:01 +01:00
|
|
|
case ASYMMETRIC_CIRCLES_GRID:
|
|
|
|
found = findCirclesGrid( view, boardSize, pointbuf, CALIB_CB_ASYMMETRIC_GRID );
|
|
|
|
break;
|
2010-12-21 10:32:14 +01:00
|
|
|
default:
|
|
|
|
return fprintf( stderr, "Unknown pattern type\n" ), -1;
|
|
|
|
}
|
2010-11-30 00:16:41 +01:00
|
|
|
|
|
|
|
// improve the found corners' coordinate accuracy
|
2010-12-21 10:32:14 +01:00
|
|
|
if( pattern == CHESSBOARD && found) cornerSubPix( viewGray, pointbuf, Size(11,11),
|
2010-11-30 00:16:41 +01:00
|
|
|
Size(-1,-1), TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 ));
|
|
|
|
|
|
|
|
if( mode == CAPTURING && found &&
|
|
|
|
(!capture.isOpened() || clock() - prevTimestamp > delay*1e-3*CLOCKS_PER_SEC) )
|
|
|
|
{
|
|
|
|
imagePoints.push_back(pointbuf);
|
|
|
|
prevTimestamp = clock();
|
|
|
|
blink = capture.isOpened();
|
|
|
|
}
|
2010-11-01 07:20:36 +01:00
|
|
|
|
2010-11-30 00:16:41 +01:00
|
|
|
if(found)
|
|
|
|
drawChessboardCorners( view, boardSize, Mat(pointbuf), found );
|
2010-06-13 05:50:29 +02:00
|
|
|
|
|
|
|
string msg = mode == CAPTURING ? "100/100" :
|
|
|
|
mode == CALIBRATED ? "Calibrated" : "Press 'g' to start";
|
|
|
|
int baseLine = 0;
|
|
|
|
Size textSize = getTextSize(msg, 1, 1, 1, &baseLine);
|
2010-07-14 02:32:35 +02:00
|
|
|
Point textOrigin(view.cols - 2*textSize.width - 10, view.rows - 2*baseLine - 10);
|
2010-06-13 05:50:29 +02:00
|
|
|
|
|
|
|
if( mode == CAPTURING )
|
2010-07-14 02:32:35 +02:00
|
|
|
{
|
2010-11-30 00:16:41 +01:00
|
|
|
if(undistortImage)
|
|
|
|
msg = format( "%d/%d Undist", (int)imagePoints.size(), nframes );
|
2010-07-14 02:32:35 +02:00
|
|
|
else
|
2010-11-30 00:16:41 +01:00
|
|
|
msg = format( "%d/%d", (int)imagePoints.size(), nframes );
|
|
|
|
}
|
2010-06-13 05:50:29 +02:00
|
|
|
|
|
|
|
putText( view, msg, textOrigin, 1, 1,
|
|
|
|
mode != CALIBRATED ? Scalar(0,0,255) : Scalar(0,255,0));
|
|
|
|
|
|
|
|
if( blink )
|
|
|
|
bitwise_not(view, view);
|
|
|
|
|
|
|
|
if( mode == CALIBRATED && undistortImage )
|
|
|
|
{
|
|
|
|
Mat temp = view.clone();
|
|
|
|
undistort(temp, view, cameraMatrix, distCoeffs);
|
|
|
|
}
|
|
|
|
|
|
|
|
imshow("Image View", view);
|
2010-10-20 20:49:44 +02:00
|
|
|
int key = 0xff & waitKey(capture.isOpened() ? 50 : 500);
|
2010-06-13 05:50:29 +02:00
|
|
|
|
|
|
|
if( (key & 255) == 27 )
|
|
|
|
break;
|
|
|
|
|
|
|
|
if( key == 'u' && mode == CALIBRATED )
|
|
|
|
undistortImage = !undistortImage;
|
|
|
|
|
|
|
|
if( capture.isOpened() && key == 'g' )
|
|
|
|
{
|
|
|
|
mode = CAPTURING;
|
|
|
|
imagePoints.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( mode == CAPTURING && imagePoints.size() >= (unsigned)nframes )
|
|
|
|
{
|
|
|
|
if( runAndSave(outputFilename, imagePoints, imageSize,
|
2011-02-07 08:59:01 +01:00
|
|
|
boardSize, pattern, squareSize, aspectRatio,
|
2010-06-13 05:50:29 +02:00
|
|
|
flags, cameraMatrix, distCoeffs,
|
|
|
|
writeExtrinsics, writePoints))
|
|
|
|
mode = CALIBRATED;
|
|
|
|
else
|
|
|
|
mode = DETECTION;
|
2010-06-13 21:08:24 +02:00
|
|
|
if( !capture.isOpened() )
|
|
|
|
break;
|
2010-06-13 05:50:29 +02:00
|
|
|
}
|
|
|
|
}
|
2010-09-07 17:38:48 +02:00
|
|
|
|
|
|
|
if( !capture.isOpened() && showUndistorted )
|
|
|
|
{
|
|
|
|
Mat view, rview, map1, map2;
|
|
|
|
initUndistortRectifyMap(cameraMatrix, distCoeffs, Mat(),
|
|
|
|
getOptimalNewCameraMatrix(cameraMatrix, distCoeffs, imageSize, 1, imageSize, 0),
|
|
|
|
imageSize, CV_16SC2, map1, map2);
|
|
|
|
|
|
|
|
for( i = 0; i < (int)imageList.size(); i++ )
|
|
|
|
{
|
|
|
|
view = imread(imageList[i], 1);
|
|
|
|
if(!view.data)
|
|
|
|
continue;
|
|
|
|
//undistort( view, rview, cameraMatrix, distCoeffs, cameraMatrix );
|
|
|
|
remap(view, rview, map1, map2, INTER_LINEAR);
|
|
|
|
imshow("Image View", rview);
|
2010-10-20 20:49:44 +02:00
|
|
|
int c = 0xff & waitKey();
|
2010-09-07 17:38:48 +02:00
|
|
|
if( (c & 255) == 27 || c == 'q' || c == 'Q' )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-13 05:50:29 +02:00
|
|
|
return 0;
|
|
|
|
}
|