2010-09-22 03:47:11 +02:00
|
|
|
/*
|
|
|
|
* Processor.cpp
|
|
|
|
*
|
|
|
|
* Created on: Jun 13, 2010
|
|
|
|
* Author: ethan
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Calibration.h"
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
Calibration::Calibration() :
|
|
|
|
patternsize(6, 8)
|
2010-09-22 03:47:11 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
Calibration::~Calibration()
|
|
|
|
{
|
2010-09-22 03:47:11 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2010-11-27 08:59:22 +01:00
|
|
|
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);
|
|
|
|
err = norm(Mat(imagePoints[i]), Mat(imagePoints2), CV_L1);
|
|
|
|
int n = (int)objectPoints[i].size();
|
|
|
|
perViewErrors[i] = err / n;
|
|
|
|
totalErr += err;
|
|
|
|
totalPoints += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return totalErr / totalPoints;
|
2010-09-22 03:47:11 +02:00
|
|
|
}
|
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
void calcChessboardCorners(Size boardSize, float squareSize, vector<Point3f>& corners)
|
|
|
|
{
|
|
|
|
corners.resize(0);
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
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));
|
2010-09-22 03:47:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**from opencv/samples/cpp/calibration.cpp
|
|
|
|
*
|
|
|
|
*/
|
2010-11-27 08:59:22 +01:00
|
|
|
bool runCalibration(vector<vector<Point2f> > imagePoints, Size imageSize, Size boardSize, 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-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
distCoeffs = Mat::zeros(4, 1, CV_64F);
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
vector<vector<Point3f> > objectPoints(1);
|
|
|
|
calcChessboardCorners(boardSize, squareSize, objectPoints[0]);
|
|
|
|
for (size_t i = 1; i < imagePoints.size(); i++)
|
|
|
|
objectPoints.push_back(objectPoints[0]);
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix, distCoeffs, rvecs, tvecs, flags);
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
bool ok = checkRange(cameraMatrix, CV_CHECK_QUIET) && checkRange(distCoeffs, CV_CHECK_QUIET);
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
totalAvgErr
|
|
|
|
= computeReprojectionErrors(objectPoints, imagePoints, rvecs, tvecs, cameraMatrix, distCoeffs, reprojErrs);
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
return ok;
|
2010-09-22 03:47:11 +02:00
|
|
|
}
|
2010-11-27 08:59:22 +01:00
|
|
|
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;
|
|
|
|
fs << "squareSize" << squareSize;
|
|
|
|
|
|
|
|
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())
|
|
|
|
{
|
|
|
|
Mat bigmat(rvecs.size(), 6, CV_32F);
|
|
|
|
for (size_t i = 0; i < rvecs.size(); i++)
|
|
|
|
{
|
|
|
|
Mat r = bigmat(Range(i, i + 1), Range(0, 3));
|
|
|
|
Mat t = bigmat(Range(i, i + 1), Range(3, 6));
|
|
|
|
rvecs[i].copyTo(r);
|
|
|
|
tvecs[i].copyTo(t);
|
|
|
|
}
|
|
|
|
cvWriteComment(*fs, "a set of 6-tuples (rotation vector + translation vector) for each view", 0);
|
|
|
|
fs << "extrinsic_parameters" << bigmat;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!imagePoints.empty())
|
|
|
|
{
|
|
|
|
Mat imagePtMat(imagePoints.size(), imagePoints[0].size(), CV_32FC2);
|
|
|
|
for (size_t i = 0; i < imagePoints.size(); i++)
|
|
|
|
{
|
|
|
|
Mat r = imagePtMat.row(i).reshape(2, imagePtMat.cols);
|
|
|
|
Mat(imagePoints[i]).copyTo(r);
|
|
|
|
}
|
|
|
|
fs << "image_points" << imagePtMat;
|
|
|
|
}
|
2010-09-22 03:47:11 +02:00
|
|
|
}
|
|
|
|
}//anon namespace
|
2010-11-27 08:59:22 +01:00
|
|
|
bool Calibration::detectAndDrawChessboard(int idx, image_pool* pool)
|
|
|
|
{
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-28 21:27:57 +01:00
|
|
|
bool patternfound = false;
|
2010-11-27 08:59:22 +01:00
|
|
|
Mat grey = pool->getGrey(idx);
|
|
|
|
if (grey.empty())
|
|
|
|
return false;
|
|
|
|
vector<Point2f> corners;
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-28 22:24:11 +01:00
|
|
|
patternfound = findChessboardCorners(grey, patternsize, corners,CALIB_CB_FILTER_QUADS + CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE
|
|
|
|
+ CALIB_CB_FAST_CHECK);
|
2010-11-27 08:59:22 +01:00
|
|
|
Mat img = pool->getImage(idx);
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
if (corners.size() < 1)
|
|
|
|
return false;
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
if (patternfound)
|
2010-11-28 21:27:57 +01:00
|
|
|
{
|
|
|
|
cornerSubPix(grey, corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));
|
2010-11-27 08:59:22 +01:00
|
|
|
imagepoints.push_back(corners);
|
2010-11-28 21:27:57 +01:00
|
|
|
}
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
drawChessboardCorners(img, patternsize, Mat(corners), patternfound);
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
imgsize = grey.size();
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
return patternfound;
|
2010-09-22 03:47:11 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
void Calibration::drawText(int i, image_pool* pool, const char* ctext)
|
|
|
|
{
|
|
|
|
// Use "y" to show that the baseLine is about
|
|
|
|
string text = ctext;
|
|
|
|
int fontFace = FONT_HERSHEY_COMPLEX_SMALL;
|
|
|
|
double fontScale = .8;
|
|
|
|
int thickness = .5;
|
|
|
|
|
|
|
|
Mat img = pool->getImage(i);
|
|
|
|
|
|
|
|
int baseline = 0;
|
|
|
|
Size textSize = getTextSize(text, fontFace, fontScale, thickness, &baseline);
|
|
|
|
baseline += thickness;
|
|
|
|
|
|
|
|
// center the text
|
|
|
|
Point textOrg((img.cols - textSize.width) / 2, (img.rows - textSize.height * 2));
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
// draw the box
|
|
|
|
rectangle(img, textOrg + Point(0, baseline), textOrg + Point(textSize.width, -textSize.height), Scalar(0, 0, 255),
|
|
|
|
CV_FILLED);
|
|
|
|
// ... and the baseline first
|
|
|
|
line(img, textOrg + Point(0, thickness), textOrg + Point(textSize.width, thickness), Scalar(0, 0, 255));
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
// then put the text itself
|
|
|
|
putText(img, text, textOrg, fontFace, fontScale, Scalar::all(255), thickness, 8);
|
2010-09-22 03:47:11 +02:00
|
|
|
}
|
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
void Calibration::resetChess()
|
|
|
|
{
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
imagepoints.clear();
|
|
|
|
}
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
void Calibration::calibrate(const char* filename)
|
|
|
|
{
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
vector<Mat> rvecs, tvecs;
|
|
|
|
vector<float> reprojErrs;
|
|
|
|
double totalAvgErr = 0;
|
|
|
|
int flags = 0;
|
|
|
|
flags |= CV_CALIB_FIX_PRINCIPAL_POINT | CV_CALIB_FIX_ASPECT_RATIO;
|
|
|
|
bool writeExtrinsics = true;
|
|
|
|
bool writePoints = true;
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
bool ok = runCalibration(imagepoints, imgsize, patternsize, 1.f, 1.f, flags, K, distortion, rvecs, tvecs, reprojErrs,
|
|
|
|
totalAvgErr);
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
if (ok)
|
|
|
|
{
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
saveCameraParams(filename, imgsize, patternsize, 1.f, 1.f, flags, K, distortion, writeExtrinsics ? rvecs : vector<
|
|
|
|
Mat> (), writeExtrinsics ? tvecs : vector<Mat> (), writeExtrinsics ? reprojErrs : vector<float> (), writePoints
|
|
|
|
? imagepoints : vector<vector<Point2f> > (), totalAvgErr);
|
|
|
|
}
|
2010-09-22 03:47:11 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
int Calibration::getNumberDetectedChessboards()
|
|
|
|
{
|
|
|
|
return imagepoints.size();
|
2010-09-22 03:47:11 +02:00
|
|
|
}
|