39 #include <cvsba/cvsba.h> 58 const std::map<int, Transform> & poses,
59 const std::multimap<int, Link> & links,
60 const std::map<int, CameraModel> & models,
61 std::map<int, cv::Point3f> & points3DMap,
62 const std::map<
int, std::map<int, cv::Point3f> > & wordReferences,
63 std::set<int> * outliers)
70 cvsba::Sba::Params params ;
71 params.type = cvsba::Sba::MOTIONSTRUCTURE;
73 params.minError = this->
epsilon();
74 params.fixedIntrinsics = 5;
75 params.fixedDistortion = 5;
77 sba.setParams(params);
79 std::vector<cv::Mat> cameraMatrix(poses.size());
80 std::vector<cv::Mat> R(poses.size());
81 std::vector<cv::Mat> T(poses.size());
82 std::vector<cv::Mat> distCoeffs(poses.size());
83 std::map<int, int> frameIdToIndex;
85 for(std::map<int, Transform>::const_iterator iter=poses.begin(); iter!=poses.end(); ++iter)
88 std::map<int, CameraModel>::const_iterator iterModel = models.find(iter->first);
89 UASSERT(iterModel != models.end() && iterModel->second.isValidForProjection());
91 frameIdToIndex.insert(std::make_pair(iter->first, oi));
93 cameraMatrix[oi] = iterModel->second.K();
94 if(iterModel->second.D().cols != 5)
96 distCoeffs[oi] = cv::Mat::zeros(1, 5, CV_64FC1);
97 UWARN(
"Camera model %d: Distortion coefficients are not 5, setting all them to 0 (assuming no distortion)", iter->first);
101 distCoeffs[oi] = iterModel->second.D();
104 Transform t = (iter->second * iterModel->second.localTransform()).
inverse();
106 R[oi] = (cv::Mat_<double>(3,3) <<
107 (
double)t.
r11(), (double)t.
r12(), (double)t.
r13(),
108 (double)t.
r21(), (double)t.
r22(), (double)t.
r23(),
109 (double)t.
r31(), (double)t.
r32(), (double)t.
r33());
110 T[oi] = (cv::Mat_<double>(1,3) << (
double)t.
x(), (double)t.
y(), (double)t.
z());
115 cameraMatrix.resize(oi);
118 distCoeffs.resize(oi);
120 UDEBUG(
"points=%d frames=%d", (
int)points3DMap.size(), (int)poses.size());
121 std::vector<cv::Point3f> points(points3DMap.size());
122 std::vector<std::vector<cv::Point2f> > imagePoints(poses.size());
123 std::vector<std::vector<int> > visibility(poses.size());
124 for(
unsigned int i=0; i<poses.size(); ++i)
126 imagePoints[i].resize(wordReferences.size(), cv::Point2f(std::numeric_limits<float>::quiet_NaN(), std::numeric_limits<float>::quiet_NaN()));
127 visibility[i].resize(wordReferences.size(), 0);
130 for(std::map<int, cv::Point3f>::const_iterator kter = points3DMap.begin(); kter!=points3DMap.end(); ++kter)
132 points[i] = kter->second;
134 std::map<int, std::map<int, cv::Point3f> >::const_iterator iter = wordReferences.find(kter->first);
135 if(iter != wordReferences.end())
137 for(std::map<int, cv::Point3f>::const_iterator jter=iter->second.begin(); jter!=iter->second.end(); ++jter)
139 if(frameIdToIndex.find(jter->first) != frameIdToIndex.end())
141 imagePoints[frameIdToIndex.at(jter->first)][i] = cv::Point2f(jter->second.x, jter->second.y);
142 visibility[frameIdToIndex.at(jter->first)][i] = 1;
152 sba.run( points, imagePoints, visibility, cameraMatrix, R, T, distCoeffs);
154 catch(cv::Exception &
e)
156 UERROR(
"Running SBA... error! %s", e.what());
157 return std::map<int, Transform>();
162 std::map<int, Transform> newPoses = poses;
163 for(std::map<int, Transform>::iterator iter=newPoses.begin(); iter!=newPoses.end(); ++iter)
165 Transform t(R[i].at<double>(0,0), R[i].at<double>(0,1), R[i].at<double>(0,2), T[i].at<double>(0),
166 R[i].at<double>(1,0), R[i].at<double>(1,1), R[i].at<double>(1,2), T[i].at<double>(1),
167 R[i].at<double>(2,0), R[i].at<double>(2,1), R[i].at<double>(2,2), T[i].at<double>(2));
173 t = (models.at(iter->first).localTransform() * t).
inverse();
174 t = iter->second.
inverse() * t;
175 iter->second *= t.
to3DoF();
179 iter->second = (models.at(iter->first).localTransform() * t).
inverse();
187 for(std::map<int, cv::Point3f>::iterator kter = points3DMap.begin(); kter!=points3DMap.end(); ++kter)
189 kter->second = points[i++];
195 UERROR(
"RTAB-Map is not built with cvsba!");
196 return std::map<int, Transform>();
GLM_FUNC_DECL genType e()
Basic mathematics functions.
Some conversion functions.
#define UASSERT(condition)
Wrappers of STL for convenient functions.
virtual std::map< int, Transform > optimizeBA(int rootId, const std::map< int, Transform > &poses, const std::multimap< int, Link > &links, const std::map< int, CameraModel > &models, std::map< int, cv::Point3f > &points3DMap, const std::map< int, std::map< int, cv::Point3f > > &wordReferences, std::set< int > *outliers=0)
static ULogger::Level level()
ULogger class and convenient macros.
GLM_FUNC_DECL matType< T, P > inverse(matType< T, P > const &m)