Commit 5beefcfa authored by baisheng lai's avatar baisheng lai

fix a bug of multiCamCalib.cpp

parent 2b28181d
...@@ -126,6 +126,7 @@ public: ...@@ -126,6 +126,7 @@ public:
@patternHeight the physical height of pattern, in user defined unit. @patternHeight the physical height of pattern, in user defined unit.
@showExtration whether show extracted features and feature filtering. @showExtration whether show extracted features and feature filtering.
@nMiniMatches minimal number of matched features for a frame. @nMiniMatches minimal number of matched features for a frame.
@flags Calibration flags
@criteria optimization stopping criteria. @criteria optimization stopping criteria.
@detector feature detector that detect feature points in pattern and images. @detector feature detector that detect feature points in pattern and images.
@descriptor feature descriptor. @descriptor feature descriptor.
...@@ -133,9 +134,9 @@ public: ...@@ -133,9 +134,9 @@ public:
*/ */
multiCameraCalibration(int cameraType, int nCameras, const std::string& fileName, float patternWidth, multiCameraCalibration(int cameraType, int nCameras, const std::string& fileName, float patternWidth,
float patternHeight, int showExtration = 0, int nMiniMatches = 20, int flags = 0, float patternHeight, int showExtration = 0, int nMiniMatches = 20, int flags = 0,
TermCriteria criteria = TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 300, 1e-7), TermCriteria criteria = TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 100, 1e-5),
Ptr<FeatureDetector> detector = AKAZE::create(AKAZE::DESCRIPTOR_MLDB, 0, 3, 0.002f), Ptr<FeatureDetector> detector = AKAZE::create(AKAZE::DESCRIPTOR_MLDB, 0, 3, 0.005f),
Ptr<DescriptorExtractor> descriptor = AKAZE::create(AKAZE::DESCRIPTOR_MLDB,0, 3, 0.002f), Ptr<DescriptorExtractor> descriptor = AKAZE::create(AKAZE::DESCRIPTOR_MLDB,0, 3, 0.005f),
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce-L1")); Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce-L1"));
/* @brief load images /* @brief load images
......
...@@ -6,7 +6,7 @@ using namespace std; ...@@ -6,7 +6,7 @@ using namespace std;
using namespace cv; using namespace cv;
const char * usage = const char * usage =
"\n example command line for multi-camera calibrsation by using random pattern \n" "\n example command line for multi-camera calibration by using random pattern \n"
" multiCamCalib -nc 5 -pw 800 -ph 600 -ct 1 -fe 0 -nm 10 multi_camera.xml \n" " multiCamCalib -nc 5 -pw 800 -ph 600 -ct 1 -fe 0 -nm 10 multi_camera.xml \n"
"\n" "\n"
" the file multi_camera.xml is generated by imagelist_creator as \n" " the file multi_camera.xml is generated by imagelist_creator as \n"
......
#include"opencv2/omnidir.hpp" #include"opencv2/omnidir.hpp"
#include"opencv2/core/core.hpp" #include"opencv2/core/core.hpp"
#include"opencv2/imgproc/imgproc.hpp" #include"opencv2/imgproc/imgproc.hpp"
#include"opencv2/highgui/highgui.hpp" #include "opencv2/highgui/highgui.hpp"
#include <vector> #include <vector>
#include <iostream> #include <iostream>
#include <string> #include <string>
...@@ -247,7 +247,7 @@ int main(int argc, char** argv) ...@@ -247,7 +247,7 @@ int main(int argc, char** argv)
Mat K, D, xi, idx; Mat K, D, xi, idx;
vector<Vec3d> rvecs, tvecs; vector<Vec3d> rvecs, tvecs;
double _xi, rms; double _xi, rms;
TermCriteria criteria(3, 100, 1e-4); TermCriteria criteria(3, 200, 1e-8);
rms = omnidir::calibrate(objectPoints, imagePoints, imageSize, K, xi, D, rvecs, tvecs, flags, criteria, idx); rms = omnidir::calibrate(objectPoints, imagePoints, imageSize, K, xi, D, rvecs, tvecs, flags, criteria, idx);
_xi = xi.at<double>(0); _xi = xi.at<double>(0);
saveCameraParams(outputFilename, imageSize, boardSize, square_width, square_height, flags, K, D, _xi, saveCameraParams(outputFilename, imageSize, boardSize, square_width, square_height, flags, K, D, _xi,
......
#include "opencv2/omnidir.hpp" #include"opencv2/omnidir.hpp"
#include "opencv2/core/core.hpp" #include"opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp" #include"opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp" #include "opencv2/highgui/highgui.hpp"
#include <vector> #include <vector>
#include <iostream> #include <iostream>
...@@ -12,7 +12,7 @@ using namespace std; ...@@ -12,7 +12,7 @@ using namespace std;
const char * usage = const char * usage =
"\n example command line for calibrate a pair of omnidirectional camera.\n" "\n example command line for calibrate a pair of omnidirectional camera.\n"
" omniStereoCalibration -w 6 -h 7 -sw 80 -sh 80 image_list_1.xml image_list_2.xml\n" " omniStereoCalibration -w 8 -h 6 -sw 2.4399 -sh 2.4399 imagelist_left.xml imagelist_right.xml\n"
" \n" " \n"
" the file image_list_1.xml and image_list_2.xml generated by imagelist_creator as\n" " the file image_list_1.xml and image_list_2.xml generated by imagelist_creator as\n"
"imagelist_creator image_list_1.xml *.*"; "imagelist_creator image_list_1.xml *.*";
...@@ -53,7 +53,7 @@ static void calcChessboardCorners(Size boardSize, double square_width, double sq ...@@ -53,7 +53,7 @@ static void calcChessboardCorners(Size boardSize, double square_width, double sq
static bool detecChessboardCorners(const vector<string>& list1, vector<string>& list_detected_1, static bool detecChessboardCorners(const vector<string>& list1, vector<string>& list_detected_1,
const vector<string>& list2, vector<string>& list_detected_2, const vector<string>& list2, vector<string>& list_detected_2,
vector<Mat>& image_points_1, vector<Mat>& image_points_2, Size boardSize, Size& imageSize) vector<Mat>& image_points_1, vector<Mat>& image_points_2, Size boardSize, Size& imageSize1, Size& imageSize2)
{ {
image_points_1.resize(0); image_points_1.resize(0);
image_points_2.resize(0); image_points_2.resize(0);
...@@ -81,7 +81,11 @@ static bool detecChessboardCorners(const vector<string>& list1, vector<string>& ...@@ -81,7 +81,11 @@ static bool detecChessboardCorners(const vector<string>& list1, vector<string>&
} }
} }
if (!img_l.empty()) if (!img_l.empty())
imageSize = img_l.size(); imageSize1 = img_l.size();
if (!img_r.empty())
{
imageSize2 = img_r.size();
}
if (image_points_1.size() < 3) if (image_points_1.size() < 3)
return false; return false;
...@@ -104,8 +108,7 @@ static bool readStringList( const string& filename, vector<string>& l ) ...@@ -104,8 +108,7 @@ static bool readStringList( const string& filename, vector<string>& l )
return true; return true;
} }
static void saveCameraParams( const string & filename, Size imageSize, Size boardSize, double square_width, static void saveCameraParams( const string & filename, const int flags, const Mat& cameraMatrix1, const Mat& cameraMatrix2, const Mat& distCoeffs1,
double square_height, 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 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 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) const Mat& idx, const double rms, const vector<Mat>& imagePoints1, const vector<Mat>& imagePoints2)
...@@ -208,7 +211,7 @@ static void saveCameraParams( const string & filename, Size imageSize, Size boar ...@@ -208,7 +211,7 @@ static void saveCameraParams( const string & filename, Size imageSize, Size boar
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
Size boardSize, imageSize; Size boardSize, imageSize1, imageSize2;
int flags = 0; int flags = 0;
double square_width, square_height; double square_width, square_height;
const char* outputFilename = "out_camera_params_stereo.xml"; const char* outputFilename = "out_camera_params_stereo.xml";
...@@ -283,7 +286,7 @@ int main(int argc, char** argv) ...@@ -283,7 +286,7 @@ int main(int argc, char** argv)
// find corners in images // find corners in images
// some images may be failed in automatic corner detection, passed cases are in detec_list // 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, if(!detecChessboardCorners(image_list1, detec_list_1, image_list2, detec_list_2,
imagePoints1, imagePoints2, boardSize, imageSize)) imagePoints1, imagePoints2, boardSize, imageSize1, imageSize2))
return fprintf(stderr, "Not enough corner detected images\n"), -1; return fprintf(stderr, "Not enough corner detected images\n"), -1;
// calculate object coordinates // calculate object coordinates
...@@ -300,13 +303,12 @@ int main(int argc, char** argv) ...@@ -300,13 +303,12 @@ int main(int argc, char** argv)
vector<Vec3d> rvecs, tvecs; vector<Vec3d> rvecs, tvecs;
Vec3d rvec, tvec; Vec3d rvec, tvec;
double _xi1, _xi2, rms; double _xi1, _xi2, rms;
TermCriteria criteria(3, 100, 1e-4); TermCriteria criteria(3, 200, 1e-8);
rms = omnidir::stereoCalibrate(objectPoints, imagePoints1, imagePoints2, imageSize, K1, xi1, D1, rms = omnidir::stereoCalibrate(objectPoints, imagePoints1, imagePoints2, imageSize1, imageSize2, K1, xi1, D1,
K2, xi2, D2, rvec, tvec, rvecs, tvecs, flags, criteria, idx); K2, xi2, D2, rvec, tvec, rvecs, tvecs, flags, criteria, idx);
_xi1 = xi1.at<double>(0); _xi1 = xi1.at<double>(0);
_xi2 = xi2.at<double>(0); _xi2 = xi2.at<double>(0);
saveCameraParams(outputFilename, imageSize, boardSize, square_width, square_height, flags, saveCameraParams(outputFilename, flags, K1, K2, D1, D2, _xi1, _xi2, rvec, tvec, rvecs, tvecs,
K1, K2, D1, D2, _xi1, _xi2, rvec, tvec, rvecs, tvecs, detec_list_1, detec_list_2, idx, rms, detec_list_1, detec_list_2, idx, rms, imagePoints1, imagePoints2);
imagePoints1, imagePoints2);
} }
\ No newline at end of file
...@@ -139,6 +139,7 @@ void multiCameraCalibration::loadImages() ...@@ -139,6 +139,7 @@ void multiCameraCalibration::loadImages()
std::vector<std::vector<std::string> > filesEachCameraFull(_nCamera); std::vector<std::vector<std::string> > filesEachCameraFull(_nCamera);
std::vector<std::vector<int> > timestampFull(_nCamera); std::vector<std::vector<int> > timestampFull(_nCamera);
std::vector<std::vector<int> > timestampAvailable(_nCamera);
for (int i = 1; i < (int)file_list.size(); ++i) for (int i = 1; i < (int)file_list.size(); ++i)
{ {
...@@ -170,8 +171,12 @@ void multiCameraCalibration::loadImages() ...@@ -170,8 +171,12 @@ void multiCameraCalibration::loadImages()
{ {
image = imread(filesEachCameraFull[camera][imgIdx], IMREAD_GRAYSCALE); image = imread(filesEachCameraFull[camera][imgIdx], IMREAD_GRAYSCALE);
std::vector<Mat> imgObj = finder.computeObjectImagePointsForSingle(image); std::vector<Mat> imgObj = finder.computeObjectImagePointsForSingle(image);
if ((int)imgObj[0].total() > _nMiniMatches)
{
_imagePointsForEachCamera[camera].push_back(imgObj[0]); _imagePointsForEachCamera[camera].push_back(imgObj[0]);
_objectPointsForEachCamera[camera].push_back(imgObj[1]); _objectPointsForEachCamera[camera].push_back(imgObj[1]);
timestampAvailable[camera].push_back(timestampFull[camera][imgIdx]);
}
} }
// calibrate // calibrate
...@@ -215,12 +220,10 @@ void multiCameraCalibration::loadImages() ...@@ -215,12 +220,10 @@ void multiCameraCalibration::loadImages()
//} //}
for (int i = 0; i < (int)_omEachCamera[camera].size(); ++i) for (int i = 0; i < (int)_omEachCamera[camera].size(); ++i)
{
if ((int)_objectPointsForEachCamera[camera][idx.at<int>(i)].total() > _nMiniMatches)
{ {
int cameraVertex, timestamp, photoVertex; int cameraVertex, timestamp, photoVertex;
cameraVertex = camera; cameraVertex = camera;
timestamp = timestampFull[camera][idx.at<int>(i)]; timestamp = timestampAvailable[camera][idx.at<int>(i)];
photoVertex = this->getPhotoVertex(timestamp); photoVertex = this->getPhotoVertex(timestamp);
...@@ -242,7 +245,7 @@ void multiCameraCalibration::loadImages() ...@@ -242,7 +245,7 @@ void multiCameraCalibration::loadImages()
this->_edgeList.push_back(edge(cameraVertex, photoVertex, idx.at<int>(i), transform)); this->_edgeList.push_back(edge(cameraVertex, photoVertex, idx.at<int>(i), transform));
} }
} std::cout << "initialized for camera " << camera << " rms = " << rms << std::endl;
} }
} }
...@@ -355,6 +358,7 @@ double multiCameraCalibration::optimizeExtrinsics() ...@@ -355,6 +358,7 @@ double multiCameraCalibration::optimizeExtrinsics()
} }
extrinParam = extrinParam + G.reshape(1, 1); extrinParam = extrinParam + G.reshape(1, 1);
change = norm(G) / norm(extrinParam); change = norm(G) / norm(extrinParam);
//double error = computeProjectError(extrinParam); //double error = computeProjectError(extrinParam);
} }
......
...@@ -1638,7 +1638,7 @@ void cv::omnidir::internal::encodeParameters(InputArray K, InputArrayOfArrays om ...@@ -1638,7 +1638,7 @@ void cv::omnidir::internal::encodeParameters(InputArray K, InputArrayOfArrays om
CV_Assert(distoaration.total() == 4 && distoaration.type() == CV_64F); CV_Assert(distoaration.total() == 4 && distoaration.type() == CV_64F);
int n = (int)omAll.total(); int n = (int)omAll.total();
Mat _omAll = omAll.getMat(), _tAll = tAll.getMat(); Mat _omAll = omAll.getMat(), _tAll = tAll.getMat();
Mat tmp = Mat(_omAll.at<Vec3d>(0)).reshape(1,3).clone();
Matx33d _K = K.getMat(); Matx33d _K = K.getMat();
Vec4d _D = (Vec4d)distoaration.getMat(); Vec4d _D = (Vec4d)distoaration.getMat();
parameters.create(1, 10+6*n,CV_64F); parameters.create(1, 10+6*n,CV_64F);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment