18,9 → 18,10 |
#include "CameraCalibrator.h" |
|
// Open chessboard images and extract corner points |
int CameraCalibrator::addChessboardPoints( |
const std::vector<std::string>& filelist, |
cv::Size & boardSize) { |
int |
CameraCalibrator::addChessboardPoints(const std::vector<std::string>& filelist, |
cv::Size & boardSize) |
{ |
|
// the points on the chessboard |
std::vector<cv::Point2f> imageCorners; |
30,8 → 31,10 |
// Initialize the chessboard corners |
// in the chessboard reference frame |
// The corners are at 3D location (X,Y,Z)= (i,j,0) |
for (int i=0; i<boardSize.height; i++) { |
for (int j=0; j<boardSize.width; j++) { |
for (int i = 0; i < boardSize.height; i++) |
{ |
for (int j = 0; j < boardSize.width; j++) |
{ |
|
objectCorners.push_back(cv::Point3f(i, j, 0.0f)); |
} |
41,26 → 44,24 |
cv::Mat image; // to contain chessboard image |
int successes = 0; |
// for all viewpoints |
for (int i=0; i<filelist.size(); i++) { |
for (unsigned int i = 0; i < filelist.size(); i++) |
{ |
|
// Open the image |
image = cv::imread(filelist[i],0); |
|
// Get the chessboard corners |
bool found = cv::findChessboardCorners( |
image, boardSize, imageCorners); |
bool found = cv::findChessboardCorners(image, boardSize, imageCorners); |
|
// Get subpixel accuracy on the corners |
cv::cornerSubPix(image, imageCorners, |
cv::Size(5,5), |
cv::Size(-1,-1), |
cv::TermCriteria(cv::TermCriteria::MAX_ITER + |
cv::TermCriteria::EPS, |
cv::cornerSubPix(image, imageCorners, cv::Size(5, 5), cv::Size(-1, -1), |
cv::TermCriteria(cv::TermCriteria::MAX_ITER + cv::TermCriteria::EPS, |
30, // max number of iterations |
0.1)); // min accuracy |
|
// If we have a good board, add it to our data |
if (imageCorners.size() == boardSize.area()) { |
if (imageCorners.size() == (unsigned int) boardSize.area()) |
{ |
|
// Add image and scene points from one view |
addPoints(imageCorners, objectCorners); |
77,7 → 78,10 |
} |
|
// Add scene points and corresponding image points |
void CameraCalibrator::addPoints(const std::vector<cv::Point2f>& imageCorners, const std::vector<cv::Point3f>& objectCorners) { |
void |
CameraCalibrator::addPoints(const std::vector<cv::Point2f>& imageCorners, |
const std::vector<cv::Point3f>& objectCorners) |
{ |
|
// 2D image points from one view |
imagePoints.push_back(imageCorners); |
87,7 → 91,8 |
|
// Calibrate the camera |
// returns the re-projection error |
double CameraCalibrator::calibrate(cv::Size &imageSize) |
double |
CameraCalibrator::calibrate(cv::Size imageSize) |
{ |
// undistorter must be reinitialized |
mustInitUndistort= true; |
96,8 → 101,7 |
std::vector<cv::Mat> rvecs, tvecs; |
|
// start calibration |
return |
calibrateCamera(objectPoints, // the 3D points |
return calibrateCamera(objectPoints, // the 3D points |
imagePoints, // the image points |
imageSize, // image size |
cameraMatrix, // output camera matrix |
109,14 → 113,16 |
} |
|
// remove distortion in an image (after calibration) |
cv::Mat CameraCalibrator::remap(const cv::Mat &image) { |
cv::Mat |
CameraCalibrator::remap(const cv::Mat &image) |
{ |
|
cv::Mat undistorted; |
|
if (mustInitUndistort) { // called once per calibration |
if (mustInitUndistort) |
{ // called once per calibration |
|
cv::initUndistortRectifyMap( |
cameraMatrix, // computed camera matrix |
cv::initUndistortRectifyMap(cameraMatrix, // computed camera matrix |
distCoeffs, // computed distortion matrix |
cv::Mat(), // optional rectification (none) |
cv::Mat(), // camera matrix to generate undistorted |
129,21 → 135,24 |
} |
|
// Apply mapping functions |
cv::remap(image, undistorted, map1, map2, |
cv::INTER_LINEAR); // interpolation type |
cv::remap(image, undistorted, map1, map2, cv::INTER_LINEAR); // interpolation type |
|
return undistorted; |
} |
|
|
// Set the calibration options |
// 8radialCoeffEnabled should be true if 8 radial coefficients are required (5 is default) |
// tangentialParamEnabled should be true if tangeantial distortion is present |
void CameraCalibrator::setCalibrationFlag(bool radial8CoeffEnabled, bool tangentialParamEnabled) { |
void |
CameraCalibrator::setCalibrationFlag(bool radial8CoeffEnabled, |
bool tangentialParamEnabled) |
{ |
|
// Set the flag used in cv::calibrateCamera() |
flag = 0; |
if (!tangentialParamEnabled) flag += CV_CALIB_ZERO_TANGENT_DIST; |
if (radial8CoeffEnabled) flag += CV_CALIB_RATIONAL_MODEL; |
if (!tangentialParamEnabled) |
flag += CV_CALIB_ZERO_TANGENT_DIST; |
if (radial8CoeffEnabled) |
flag += CV_CALIB_RATIONAL_MODEL; |
} |
|