vendor: OpenCV 5.0.0 snapshot at 755e50675d97db9b7d449d8bd6b09888646f6c6e
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
#include "opencv2/ccalib/omnidir.hpp"
|
||||
#include "opencv2/ccalib/multicalib.hpp"
|
||||
#include "opencv2/ccalib/randpattern.hpp"
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
|
||||
const char * usage =
|
||||
"\n example command line for multi-camera calibration by using random pattern \n"
|
||||
" multi_cameras_calibration -nc 5 -pw 800 -ph 600 -ct 1 -fe 0 -nm 25 -v 0 multi_camera_omnidir.xml \n"
|
||||
"\n"
|
||||
" 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"
|
||||
" 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"
|
||||
" -v # whether show verbose information \n"
|
||||
" 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)
|
||||
{
|
||||
float patternWidth = 0.0f, patternHeight = 0.0f;
|
||||
int nCamera = 0, nMiniMatches = 0, cameraType = 0;
|
||||
const char* outputFilename = "multi-camera-results.xml";
|
||||
const char* inputFilename = 0;
|
||||
int showFeatureExtraction = 0, verbose = 0;
|
||||
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;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
else if( s[0] != '-')
|
||||
{
|
||||
inputFilename = s;
|
||||
}
|
||||
else
|
||||
{
|
||||
return fprintf( stderr, "Unknown option %s\n", s ), -1;
|
||||
}
|
||||
}
|
||||
|
||||
// do multi-camera calibration
|
||||
multicalib::MultiCameraCalibration multiCalib(cameraType, nCamera, inputFilename, patternWidth, patternHeight, verbose, showFeatureExtraction, nMiniMatches);
|
||||
|
||||
multiCalib.loadImages();
|
||||
multiCalib.initialize();
|
||||
multiCalib.optimizeExtrinsics();
|
||||
// the above three lines can be replaced by multiCalib.run();
|
||||
|
||||
|
||||
multiCalib.writeParameters(outputFilename);
|
||||
}
|
||||
@@ -0,0 +1,224 @@
|
||||
#include "opencv2/ccalib/omnidir.hpp"
|
||||
#include "opencv2/core.hpp"
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include "opencv2/geometry.hpp"
|
||||
#include "opencv2/calib.hpp"
|
||||
#include "opencv2/objdetect.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <time.h>
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
static void calcChessboardCorners(const Size &boardSize, const Size2d &squareSize, Mat& corners)
|
||||
{
|
||||
// corners has type of CV_64FC3
|
||||
corners.release();
|
||||
int n = boardSize.width * boardSize.height;
|
||||
corners.create(n, 1, CV_64FC3);
|
||||
Vec3d *ptr = corners.ptr<Vec3d>();
|
||||
for (int i = 0; i < boardSize.height; ++i)
|
||||
{
|
||||
for (int j = 0; j < boardSize.width; ++j)
|
||||
{
|
||||
ptr[i*boardSize.width + j] = Vec3d(double(j * squareSize.width), double(i * squareSize.height), 0.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static bool detecChessboardCorners(const vector<string>& list, vector<string>& list_detected,
|
||||
vector<Mat>& imagePoints, Size boardSize, Size& imageSize)
|
||||
{
|
||||
imagePoints.resize(0);
|
||||
list_detected.resize(0);
|
||||
int n_img = (int)list.size();
|
||||
Mat img;
|
||||
for(int i = 0; i < n_img; ++i)
|
||||
{
|
||||
cout << list[i] << "... ";
|
||||
Mat points;
|
||||
img = imread(list[i], IMREAD_GRAYSCALE);
|
||||
bool found = findChessboardCorners( img, boardSize, points);
|
||||
if (found)
|
||||
{
|
||||
if (points.type() != CV_64FC2)
|
||||
points.convertTo(points, CV_64FC2);
|
||||
imagePoints.push_back(points);
|
||||
list_detected.push_back(list[i]);
|
||||
}
|
||||
cout << (found ? "FOUND" : "NO") << endl;
|
||||
}
|
||||
if (!img.empty())
|
||||
imageSize = img.size();
|
||||
if (imagePoints.size() < 3)
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool readStringList( const string& filename, vector<string>& l )
|
||||
{
|
||||
l.resize(0);
|
||||
FileStorage fs(filename, FileStorage::READ);
|
||||
if( !fs.isOpened() )
|
||||
return false;
|
||||
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);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void saveCameraParams( const string & filename, int flags, const Mat& cameraMatrix,
|
||||
const Mat& distCoeffs, const double xi, const vector<Vec3d>& rvecs, const vector<Vec3d>& tvecs,
|
||||
vector<string> detec_list, const Mat& idx, const double rms, const vector<Mat>& imagePoints)
|
||||
{
|
||||
FileStorage fs( filename, FileStorage::WRITE );
|
||||
|
||||
time_t tt;
|
||||
time( &tt );
|
||||
struct tm *t2 = localtime( &tt );
|
||||
char buf[1024];
|
||||
strftime( buf, sizeof(buf)-1, "%c", t2 );
|
||||
|
||||
fs << "calibration_time" << buf;
|
||||
|
||||
if ( !rvecs.empty())
|
||||
fs << "nFrames" << (int)rvecs.size();
|
||||
|
||||
if ( flags != 0)
|
||||
{
|
||||
sprintf( buf, "flags: %s%s%s%s%s%s%s%s%s",
|
||||
flags & omnidir::CALIB_USE_GUESS ? "+use_intrinsic_guess" : "",
|
||||
flags & omnidir::CALIB_FIX_SKEW ? "+fix_skew" : "",
|
||||
flags & omnidir::CALIB_FIX_K1 ? "+fix_k1" : "",
|
||||
flags & omnidir::CALIB_FIX_K2 ? "+fix_k2" : "",
|
||||
flags & omnidir::CALIB_FIX_P1 ? "+fix_p1" : "",
|
||||
flags & omnidir::CALIB_FIX_P2 ? "+fix_p2" : "",
|
||||
flags & omnidir::CALIB_FIX_XI ? "+fix_xi" : "",
|
||||
flags & omnidir::CALIB_FIX_GAMMA ? "+fix_gamma" : "",
|
||||
flags & omnidir::CALIB_FIX_CENTER ? "+fix_center" : "");
|
||||
//cvWriteComment( *fs, buf, 0 );
|
||||
}
|
||||
|
||||
fs << "flags" << flags;
|
||||
|
||||
fs << "camera_matrix" << cameraMatrix;
|
||||
fs << "distortion_coefficients" << distCoeffs;
|
||||
fs << "xi" << xi;
|
||||
|
||||
//cvWriteComment( *fs, "names of images that are acturally used in calibration", 0 );
|
||||
fs << "used_imgs" << "[";
|
||||
for (int i = 0; i < (int)idx.total(); ++i)
|
||||
{
|
||||
fs << detec_list[(int)idx.at<int>(i)];
|
||||
}
|
||||
fs << "]";
|
||||
|
||||
if ( !rvecs.empty() && !tvecs.empty() )
|
||||
{
|
||||
Mat rvec_tvec((int)rvecs.size(), 6, CV_64F);
|
||||
for (int i = 0; i < (int)rvecs.size(); ++i)
|
||||
{
|
||||
Mat(rvecs[i]).reshape(1, 1).copyTo(rvec_tvec(Rect(0, i, 3, 1)));
|
||||
Mat(tvecs[i]).reshape(1, 1).copyTo(rvec_tvec(Rect(3, i, 3, 1)));
|
||||
}
|
||||
//cvWriteComment( *fs, "a set of 6-tuples (rotation vector + translation vector) for each view", 0 );
|
||||
fs << "extrinsic_parameters" << rvec_tvec;
|
||||
}
|
||||
|
||||
fs << "rms" << rms;
|
||||
|
||||
if ( !imagePoints.empty() )
|
||||
{
|
||||
Mat imageMat((int)imagePoints.size(), (int)imagePoints[0].total(), CV_64FC2);
|
||||
for (int i = 0; i < (int)imagePoints.size(); ++i)
|
||||
{
|
||||
Mat r = imageMat.row(i).reshape(2, imageMat.cols);
|
||||
Mat imagei(imagePoints[i]);
|
||||
imagei.copyTo(r);
|
||||
}
|
||||
fs << "image_points" << imageMat;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
cv::CommandLineParser parser(argc, argv,
|
||||
"{w||board width}"
|
||||
"{h||board height}"
|
||||
"{sw|1.0|square width}"
|
||||
"{sh|1.0|square height}"
|
||||
"{o|out_camera_params.xml|output file}"
|
||||
"{fs|false|fix skew}"
|
||||
"{fp|false|fix principal point at the center}"
|
||||
"{@input||input file - xml file with a list of the images, created with cpp-example-imagelist_creator tool}"
|
||||
"{help||show help}"
|
||||
);
|
||||
parser.about("This is a sample for omnidirectional camera calibration. Example command line:\n"
|
||||
" omni_calibration -w=6 -h=9 -sw=80 -sh=80 imagelist.xml \n");
|
||||
if (parser.has("help") || !parser.has("w") || !parser.has("h"))
|
||||
{
|
||||
parser.printMessage();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Size boardSize(parser.get<int>("w"), parser.get<int>("h"));
|
||||
Size2d squareSize(parser.get<double>("sw"), parser.get<double>("sh"));
|
||||
int flags = 0;
|
||||
if (parser.get<bool>("fs"))
|
||||
flags |= omnidir::CALIB_FIX_SKEW;
|
||||
if (parser.get<bool>("fp"))
|
||||
flags |= omnidir::CALIB_FIX_CENTER;
|
||||
const string outputFilename = parser.get<string>("o");
|
||||
const string inputFilename = parser.get<string>(0);
|
||||
|
||||
if (!parser.check())
|
||||
{
|
||||
parser.printErrors();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// get image name list
|
||||
vector<string> image_list, detec_list;
|
||||
if(!readStringList(inputFilename, image_list))
|
||||
{
|
||||
cout << "Can not read imagelist" << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// find corners in images
|
||||
// some images may be failed in automatic corner detection, passed cases are in detec_list
|
||||
cout << "Detecting chessboards (" << image_list.size() << ")" << endl;
|
||||
vector<Mat> imagePoints;
|
||||
Size imageSize;
|
||||
if(!detecChessboardCorners(image_list, detec_list, imagePoints, boardSize, imageSize))
|
||||
{
|
||||
cout << "Not enough corner detected images" << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// calculate object coordinates
|
||||
vector<Mat> objectPoints;
|
||||
Mat object;
|
||||
calcChessboardCorners(boardSize, squareSize, object);
|
||||
for(int i = 0; i < (int)detec_list.size(); ++i)
|
||||
objectPoints.push_back(object);
|
||||
|
||||
// run calibration, some images are discarded in calibration process because they are failed
|
||||
// in initialization. Retained image indexes are in idx variable.
|
||||
Mat K, D, xi, idx;
|
||||
vector<Vec3d> rvecs, tvecs;
|
||||
double _xi, rms;
|
||||
TermCriteria criteria(3, 200, 1e-8);
|
||||
rms = omnidir::calibrate(objectPoints, imagePoints, imageSize, K, xi, D, rvecs, tvecs, flags, criteria, idx);
|
||||
_xi = xi.at<double>(0);
|
||||
cout << "Saving camera params to " << outputFilename << endl;
|
||||
saveCameraParams(outputFilename, flags, K, D, _xi,
|
||||
rvecs, tvecs, detec_list, idx, rms, imagePoints);
|
||||
}
|
||||
@@ -0,0 +1,319 @@
|
||||
#include "opencv2/ccalib/omnidir.hpp"
|
||||
#include "opencv2/core.hpp"
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include "opencv2/geometry.hpp"
|
||||
#include "opencv2/objdetect.hpp"
|
||||
#include "opencv2/calib.hpp"
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <time.h>
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
const char * usage =
|
||||
"\n example command line for calibrate a pair of omnidirectional camera.\n"
|
||||
" omni_stereo_calibration -w 8 -h 6 -sw 2.4399 -sh 2.4399 imagelist_left.xml imagelist_right.xml\n"
|
||||
" \n"
|
||||
" the file image_list_1.xml and image_list_2.xml generated by imagelist_creator as\n"
|
||||
"imagelist_creator image_list_1.xml *.*";
|
||||
|
||||
static void help()
|
||||
{
|
||||
printf("\n This is a sample for omnidirectional camera calibration.\n"
|
||||
"Usage: omni_calibration\n"
|
||||
" -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"
|
||||
" [-sw <square_width>] # the width of square in some user-defined units (1 by default)\n"
|
||||
" [-sh <square_height>] # the height of square in some user-defined units (1 by default)\n"
|
||||
" [-o <out_camera_params>] # the output filename for intrinsic [and extrinsic] parameters\n"
|
||||
" [-fs <fix_skew>] # fix skew\n"
|
||||
" [-fp ] # fix the principal point at the center\n"
|
||||
" input_data_1 # input data - text file with a list of the images of the first camera, which is generated by imagelist_creator"
|
||||
" input_data_2 # input data - text file with a list of the images of the second camera, which is generated by imagelist_creator"
|
||||
);
|
||||
printf("\n %s", usage);
|
||||
}
|
||||
|
||||
static void calcChessboardCorners(Size boardSize, double square_width, double square_height,
|
||||
Mat& corners)
|
||||
{
|
||||
// corners has type of CV_64FC3
|
||||
corners.release();
|
||||
int n = boardSize.width * boardSize.height;
|
||||
corners.create(n, 1, CV_64FC3);
|
||||
Vec3d *ptr = corners.ptr<Vec3d>();
|
||||
for (int i = 0; i < boardSize.height; ++i)
|
||||
{
|
||||
for (int j = 0; j < boardSize.width; ++j)
|
||||
{
|
||||
ptr[i*boardSize.width + j] = Vec3d(double(j * square_width), double(i * square_height), 0.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static bool detecChessboardCorners(const vector<string>& list1, vector<string>& list_detected_1,
|
||||
const vector<string>& list2, vector<string>& list_detected_2,
|
||||
vector<Mat>& image_points_1, vector<Mat>& image_points_2, Size boardSize, Size& imageSize1, Size& imageSize2)
|
||||
{
|
||||
image_points_1.resize(0);
|
||||
image_points_2.resize(0);
|
||||
list_detected_1.resize(0);
|
||||
list_detected_2.resize(0);
|
||||
int n_img = (int)list1.size();
|
||||
Mat img_l, img_r;
|
||||
for(int i = 0; i < n_img; ++i)
|
||||
{
|
||||
Mat points_1, points_2;
|
||||
img_l = imread(list1[i], IMREAD_GRAYSCALE);
|
||||
img_r = imread(list2[i], IMREAD_GRAYSCALE);
|
||||
bool found_l = findChessboardCorners( img_l, boardSize, points_1);
|
||||
bool found_r = findChessboardCorners( img_r, boardSize, points_2);
|
||||
if (found_l && found_r)
|
||||
{
|
||||
if (points_1.type() != CV_64FC2)
|
||||
points_1.convertTo(points_1, CV_64FC2);
|
||||
if (points_2.type() != CV_64FC2)
|
||||
points_2.convertTo(points_2, CV_64FC2);
|
||||
image_points_1.push_back(points_1);
|
||||
image_points_2.push_back(points_2);
|
||||
list_detected_1.push_back(list1[i]);
|
||||
list_detected_2.push_back(list2[i]);
|
||||
}
|
||||
}
|
||||
if (!img_l.empty())
|
||||
imageSize1 = img_l.size();
|
||||
if (!img_r.empty())
|
||||
{
|
||||
imageSize2 = img_r.size();
|
||||
}
|
||||
|
||||
if (image_points_1.size() < 3)
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool readStringList( const string& filename, vector<string>& l )
|
||||
{
|
||||
l.resize(0);
|
||||
FileStorage fs(filename, FileStorage::READ);
|
||||
if( !fs.isOpened() )
|
||||
return false;
|
||||
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);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void saveCameraParams( const string & filename, const int flags, const Mat& cameraMatrix1, const Mat& cameraMatrix2, const Mat& distCoeffs1,
|
||||
const Mat& disCoeffs2, const double xi1, const double xi2, const Vec3d rvec, const Vec3d tvec,
|
||||
const vector<Vec3d>& rvecs, const vector<Vec3d>& tvecs, vector<string> detec_list_1, vector<string> detec_list_2,
|
||||
const Mat& idx, const double rms, const vector<Mat>& imagePoints1, const vector<Mat>& imagePoints2)
|
||||
{
|
||||
FileStorage fs( filename, FileStorage::WRITE );
|
||||
|
||||
time_t tt;
|
||||
time( &tt );
|
||||
struct tm *t2 = localtime( &tt );
|
||||
char buf[1024];
|
||||
strftime( buf, sizeof(buf)-1, "%c", t2 );
|
||||
|
||||
fs << "calibration_time" << buf;
|
||||
|
||||
if ( !rvecs.empty())
|
||||
fs << "nFrames" << (int)rvecs.size();
|
||||
|
||||
if ( flags != 0)
|
||||
{
|
||||
sprintf( buf, "flags: %s%s%s%s%s%s%s%s%s",
|
||||
flags & omnidir::CALIB_USE_GUESS ? "+use_intrinsic_guess" : "",
|
||||
flags & omnidir::CALIB_FIX_SKEW ? "+fix_skew" : "",
|
||||
flags & omnidir::CALIB_FIX_K1 ? "+fix_k1" : "",
|
||||
flags & omnidir::CALIB_FIX_K2 ? "+fix_k2" : "",
|
||||
flags & omnidir::CALIB_FIX_P1 ? "+fix_p1" : "",
|
||||
flags & omnidir::CALIB_FIX_P2 ? "+fix_p2" : "",
|
||||
flags & omnidir::CALIB_FIX_XI ? "+fix_xi" : "",
|
||||
flags & omnidir::CALIB_FIX_GAMMA ? "+fix_gamma" : "",
|
||||
flags & omnidir::CALIB_FIX_CENTER ? "+fix_center" : "");
|
||||
//cvWriteComment( *fs, buf, 0 );
|
||||
}
|
||||
|
||||
fs << "flags" << flags;
|
||||
|
||||
fs << "camera_matrix_1" << cameraMatrix1;
|
||||
fs << "distortion_coefficients_1" << distCoeffs1;
|
||||
fs << "xi_1" << xi1;
|
||||
|
||||
fs << "camera_matrix_2" << cameraMatrix2;
|
||||
fs << "distortion_coefficients_2" << disCoeffs2;
|
||||
fs << "xi_2" << xi2;
|
||||
|
||||
Mat om_t(1, 6, CV_64F);
|
||||
Mat(rvec).reshape(1, 1).copyTo(om_t.colRange(0, 3));
|
||||
Mat(tvec).reshape(1, 1).copyTo(om_t.colRange(3, 6));
|
||||
//cvWriteComment( *fs, "6-tuples (rotation vector + translation vector) for each view", 0 );
|
||||
fs << "extrinsic_parameters" << om_t;
|
||||
|
||||
if ( !rvecs.empty() && !tvecs.empty() )
|
||||
{
|
||||
Mat rvec_tvec((int)rvecs.size(), 6, CV_64F);
|
||||
for (int i = 0; i < (int)rvecs.size(); ++i)
|
||||
{
|
||||
Mat(rvecs[i]).reshape(1, 1).copyTo(rvec_tvec(Rect(0, i, 3, 1)));
|
||||
Mat(tvecs[i]).reshape(1, 1).copyTo(rvec_tvec(Rect(3, i, 3, 1)));
|
||||
}
|
||||
//cvWriteComment( *fs, "a set of 6-tuples (rotation vector + translation vector) for each view", 0 );
|
||||
fs << "extrinsic_parameters_1" << rvec_tvec;
|
||||
}
|
||||
|
||||
fs << "rms" << rms;
|
||||
|
||||
//cvWriteComment( *fs, "names of images that are acturally used in calibration", 0 );
|
||||
fs << "used_imgs_1" << "[";
|
||||
for (int i = 0; i < (int)idx.total(); ++i)
|
||||
{
|
||||
fs << detec_list_1[(int)idx.at<int>(i)];
|
||||
}
|
||||
fs << "]";
|
||||
|
||||
fs << "used_imgs_2" << "[";
|
||||
for (int i = 0; i < (int)idx.total(); ++i)
|
||||
{
|
||||
fs << detec_list_2[(int)idx.at<int>(i)];
|
||||
}
|
||||
fs << "]";
|
||||
|
||||
if ( !imagePoints1.empty() )
|
||||
{
|
||||
Mat imageMat((int)imagePoints1.size(), (int)imagePoints1[0].total(), CV_64FC2);
|
||||
for (int i = 0; i < (int)imagePoints1.size(); ++i)
|
||||
{
|
||||
Mat r = imageMat.row(i).reshape(2, imageMat.cols);
|
||||
Mat imagei(imagePoints1[i]);
|
||||
imagei.copyTo(r);
|
||||
}
|
||||
fs << "image_points_1" << imageMat;
|
||||
}
|
||||
|
||||
if ( !imagePoints2.empty() )
|
||||
{
|
||||
Mat imageMat((int)imagePoints2.size(), (int)imagePoints2[0].total(), CV_64FC2);
|
||||
for (int i = 0; i < (int)imagePoints2.size(); ++i)
|
||||
{
|
||||
Mat r = imageMat.row(i).reshape(2, imageMat.cols);
|
||||
Mat imagei(imagePoints2[i]);
|
||||
imagei.copyTo(r);
|
||||
}
|
||||
fs << "image_points_2" << imageMat;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
Size boardSize, imageSize1, imageSize2;
|
||||
int flags = 0;
|
||||
double square_width = 0.0, square_height = 0.0;
|
||||
const char* outputFilename = "out_camera_params_stereo.xml";
|
||||
const char* inputFilename1 = 0;
|
||||
const char* inputFilename2 = 0;
|
||||
vector<Mat> objectPoints;
|
||||
vector<Mat> imagePoints1;
|
||||
vector<Mat> imagePoints2;
|
||||
|
||||
if(argc < 2)
|
||||
{
|
||||
help();
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool fist_flag = true;
|
||||
for(int i = 1; i < argc; i++)
|
||||
{
|
||||
const char* s = argv[i];
|
||||
if( strcmp( s, "-w") == 0)
|
||||
{
|
||||
if( sscanf( argv[++i], "%u", &boardSize.width ) != 1 || boardSize.width <= 0 )
|
||||
return fprintf( stderr, "Invalid board width\n" ), -1;
|
||||
}
|
||||
else if( strcmp( s, "-h" ) == 0 )
|
||||
{
|
||||
if( sscanf( argv[++i], "%u", &boardSize.height ) != 1 || boardSize.height <= 0 )
|
||||
return fprintf( stderr, "Invalid board height\n" ), -1;
|
||||
}
|
||||
else if( strcmp( s, "-sw" ) == 0 )
|
||||
{
|
||||
if( sscanf( argv[++i], "%lf", &square_width ) != 1 || square_width <= 0 )
|
||||
return fprintf(stderr, "Invalid square width\n"), -1;
|
||||
}
|
||||
else if( strcmp( s, "-sh" ) == 0 )
|
||||
{
|
||||
if( sscanf( argv[++i], "%lf", &square_height) != 1 || square_height <= 0 )
|
||||
return fprintf(stderr, "Invalid square height\n"), -1;
|
||||
}
|
||||
else if( strcmp( s, "-o" ) == 0 )
|
||||
{
|
||||
outputFilename = argv[++i];
|
||||
}
|
||||
else if( strcmp( s, "-fs" ) == 0 )
|
||||
{
|
||||
flags |= omnidir::CALIB_FIX_SKEW;
|
||||
}
|
||||
else if( strcmp( s, "-fp" ) == 0 )
|
||||
{
|
||||
flags |= omnidir::CALIB_FIX_CENTER;
|
||||
}
|
||||
else if( s[0] != '-' && fist_flag)
|
||||
{
|
||||
fist_flag = false;
|
||||
inputFilename1 = s;
|
||||
}
|
||||
else if( s[0] != '-' && !fist_flag)
|
||||
{
|
||||
inputFilename2 = s;
|
||||
}
|
||||
else
|
||||
{
|
||||
return fprintf( stderr, "Unknown option %s\n", s ), -1;
|
||||
}
|
||||
}
|
||||
|
||||
// get image name list
|
||||
vector<string> image_list1, detec_list_1, image_list2, detec_list_2;
|
||||
if((!readStringList(inputFilename1, image_list1)) || (!readStringList(inputFilename2, image_list2)))
|
||||
return fprintf( stderr, "Failed to read image list\n"), -1;
|
||||
|
||||
// find corners in images
|
||||
// some images may be failed in automatic corner detection, passed cases are in detec_list
|
||||
if(!detecChessboardCorners(image_list1, detec_list_1, image_list2, detec_list_2,
|
||||
imagePoints1, imagePoints2, boardSize, imageSize1, imageSize2))
|
||||
return fprintf(stderr, "Not enough corner detected images\n"), -1;
|
||||
|
||||
// calculate object coordinates
|
||||
Mat object;
|
||||
calcChessboardCorners(boardSize, square_width, square_height, object);
|
||||
for(int i = 0; i < (int)detec_list_1.size(); ++i)
|
||||
{
|
||||
objectPoints.push_back(object);
|
||||
}
|
||||
|
||||
// run calibration, some images are discarded in calibration process because they are failed
|
||||
// in initialization. Retained image indexes are in idx variable.
|
||||
Mat K1, K2, D1, D2, xi1, xi2, idx;
|
||||
vector<Vec3d> rvecs, tvecs;
|
||||
Vec3d rvec, tvec;
|
||||
double _xi1, _xi2, rms;
|
||||
TermCriteria criteria(3, 200, 1e-8);
|
||||
rms = omnidir::stereoCalibrate(objectPoints, imagePoints1, imagePoints2, imageSize1, imageSize2, K1, xi1, D1,
|
||||
K2, xi2, D2, rvec, tvec, rvecs, tvecs, flags, criteria, idx);
|
||||
_xi1 = xi1.at<double>(0);
|
||||
_xi2 = xi2.at<double>(0);
|
||||
|
||||
saveCameraParams(outputFilename, flags, K1, K2, D1, D2, _xi1, _xi2, rvec, tvec, rvecs, tvecs,
|
||||
detec_list_1, detec_list_2, idx, rms, imagePoints1, imagePoints2);
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
#include "opencv2/ccalib/randpattern.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include "opencv2/geometry.hpp"
|
||||
#include "opencv2/calib.hpp"
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <time.h>
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
|
||||
const char * usage =
|
||||
"\n example command line for calibrate a camera by random pattern. \n"
|
||||
" random_pattern_calibration -pw 600 -ph 850 -mm 20 image_list.xml \n"
|
||||
"\n"
|
||||
" the file image_list.xml is generated by imagelist_creator as\n"
|
||||
"imagelist_creator image_list.xml *.*";
|
||||
static void help()
|
||||
{
|
||||
printf("\n This is a sample for camera calibration by a random pattern.\n"
|
||||
"Usage: random_pattern_calibration\n"
|
||||
" -pw <pattern_width> # the physical width of random pattern\n"
|
||||
" -ph <pattern_height> # the physical height of random pattern\n"
|
||||
" -mm <minimal_match> # minimal number of matches\n"
|
||||
" [-fp ] # fix the principal point at the center \n"
|
||||
" input_data # input data - text file with a list of the images of the board, which is generated by imagelist_creator"
|
||||
);
|
||||
printf("\n %s", usage);
|
||||
}
|
||||
|
||||
static bool readStringList( const string& filename, vector<string>& l )
|
||||
{
|
||||
l.resize(0);
|
||||
FileStorage fs(filename, FileStorage::READ);
|
||||
if( !fs.isOpened() )
|
||||
return false;
|
||||
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);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void saveCameraParams(const string& filename, Size imageSize, float patternWidth,
|
||||
float patternHeight, int flags, const Mat& cameraMatrix, const Mat& distCoeffs,
|
||||
const vector<Mat>& rvecs, const vector<Mat>& tvecs, double rms)
|
||||
{
|
||||
FileStorage fs (filename, FileStorage::WRITE );
|
||||
time_t tt;
|
||||
time( &tt );
|
||||
struct tm *t2 = localtime( &tt );
|
||||
char buf[1024];
|
||||
strftime( buf, sizeof(buf)-1, "%c", t2 );
|
||||
|
||||
fs << "calibration_time" << buf;
|
||||
|
||||
if( !rvecs.empty())
|
||||
fs << "nframes" << (int)rvecs.size();
|
||||
|
||||
fs << "image_width" << imageSize.width;
|
||||
fs << "image_height" << imageSize.height;
|
||||
fs << "pattern_width" << patternWidth;
|
||||
fs << "pattern_height" << patternHeight;
|
||||
|
||||
fs << "flags" <<flags;
|
||||
|
||||
fs << "camera_matrix" << cameraMatrix;
|
||||
fs << "distortion_coefficients" << distCoeffs;
|
||||
|
||||
fs << "rms" << rms;
|
||||
|
||||
if( !rvecs.empty() && !tvecs.empty() )
|
||||
{
|
||||
CV_Assert(rvecs[0].type() == tvecs[0].type());
|
||||
Mat bigmat((int)rvecs.size(), 6, rvecs[0].type());
|
||||
for( int i = 0; i < (int)rvecs.size(); i++ )
|
||||
{
|
||||
Mat r = bigmat(Range(i, i+1), Range(0,3));
|
||||
Mat t = bigmat(Range(i, i+1), Range(3,6));
|
||||
|
||||
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();
|
||||
}
|
||||
//cvWriteComment( *fs, "a set of 6-tuples (rotation vector + translation vector) for each view", 0 );
|
||||
fs << "extrinsic_parameters" << bigmat;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
const char* inputFilename = 0;
|
||||
const char* outputFilename = "out_camera_params.xml";
|
||||
vector<string> imglist;
|
||||
vector<Mat> vecImg;
|
||||
int flags = 0;
|
||||
float patternWidth = 0.0f, patternHeight = 0.0f;
|
||||
int nMiniMatches = 0;
|
||||
if(argc < 2)
|
||||
{
|
||||
help();
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (int i = 1; i < argc; ++i)
|
||||
{
|
||||
const char* s = argv[i];
|
||||
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, "-mm") == 0)
|
||||
{
|
||||
if (sscanf(argv[++i], "%d", &nMiniMatches) != 1 || nMiniMatches < 15)
|
||||
return fprintf( stderr, "Invalid number of minimal matches or number is too small"), -1;
|
||||
}
|
||||
else if( strcmp( s, "-fp" ) == 0 )
|
||||
{
|
||||
flags |= CALIB_FIX_PRINCIPAL_POINT;
|
||||
}
|
||||
else if( s[0] != '-')
|
||||
{
|
||||
inputFilename = s;
|
||||
}
|
||||
else
|
||||
{
|
||||
return fprintf( stderr, "Unknown option %s\n", s ), -1;
|
||||
}
|
||||
}
|
||||
|
||||
readStringList(inputFilename, imglist);
|
||||
// the first image is the pattern
|
||||
Mat pattern = cv::imread(imglist[0], cv::IMREAD_GRAYSCALE);
|
||||
for (int i = 1; i < (int)imglist.size(); ++i)
|
||||
{
|
||||
Mat img;
|
||||
img = cv::imread(imglist[i], cv::IMREAD_GRAYSCALE);
|
||||
vecImg.push_back(img);
|
||||
}
|
||||
|
||||
randpattern::RandomPatternCornerFinder finder(patternWidth, patternHeight, nMiniMatches);
|
||||
finder.loadPattern(pattern);
|
||||
finder.computeObjectImagePoints(vecImg);
|
||||
vector<Mat> objectPoints = finder.getObjectPoints();
|
||||
vector<Mat> imagePoints = finder.getImagePoints();
|
||||
|
||||
Mat K;
|
||||
Mat D;
|
||||
vector<Mat> rvec, tvec;
|
||||
double rms = calibrateCamera(objectPoints, imagePoints, vecImg[0].size(), K, D, rvec, tvec);
|
||||
saveCameraParams(outputFilename, vecImg[0].size(), patternWidth, patternHeight, flags, K, D, rvec, tvec, rms);
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
#include "opencv2/ccalib/randpattern.hpp"
|
||||
|
||||
using namespace cv;
|
||||
|
||||
const char * usage =
|
||||
"\n example command line for generating a random pattern. \n"
|
||||
" random_patterng_generator -iw 600 -ih 850 pattern.png\n"
|
||||
"\n";
|
||||
|
||||
static void help()
|
||||
{
|
||||
printf("\n This is a sample for generating a random pattern that can be used for calibration.\n"
|
||||
"Usage: random_patterng_generator\n"
|
||||
" -iw <image_width> # the width of pattern image\n"
|
||||
" -ih <image_height> # the height of pattern image\n"
|
||||
" filename # the filename for pattern image \n"
|
||||
);
|
||||
printf("\n %s", usage);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
const char* filename = 0;
|
||||
Mat pattern;
|
||||
int width = 0, height = 0;
|
||||
if(argc < 2)
|
||||
{
|
||||
help();
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (int i = 1; i < argc; ++i)
|
||||
{
|
||||
const char* s = argv[i];
|
||||
if(strcmp(s, "-iw") == 0)
|
||||
{
|
||||
if(sscanf(argv[++i], "%d", &width) != 1 || width <= 0)
|
||||
return fprintf( stderr, "Invalid pattern image width\n"), -1;
|
||||
}
|
||||
else if(strcmp(s, "-ih") == 0)
|
||||
{
|
||||
if(sscanf(argv[++i], "%d", &height) != 1 || height <= 0)
|
||||
return fprintf( stderr, "Invalid pattern image height\n"), -1;
|
||||
}
|
||||
else if( s[0] != '-')
|
||||
{
|
||||
filename = s;
|
||||
}
|
||||
else
|
||||
{
|
||||
return fprintf( stderr, "Unknown option %s\n", s ), -1;
|
||||
}
|
||||
}
|
||||
|
||||
randpattern::RandomPatternGenerator generator(width, height);
|
||||
generator.generatePattern();
|
||||
pattern = generator.getPattern();
|
||||
imwrite(filename, pattern);
|
||||
}
|
||||
Reference in New Issue
Block a user