RTAB-Map 0.23.10
Real-Time Appearance-Based Mapping
Loading...
Searching...
No Matches
EpipolarGeometry.h
1/*
2Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the Universite de Sherbrooke nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#pragma once
29
30#include "rtabmap/core/rtabmap_core_export.h" // DLL export/import defines
31#include "rtabmap/core/Parameters.h"
33#include <opencv2/core/core.hpp>
34#if CV_MAJOR_VERSION < 5
35#include <opencv2/features2d/features2d.hpp>
36#else
37#include <opencv2/features.hpp>
38#endif
39#include <pcl/point_cloud.h>
40#include <pcl/point_types.h>
41#include <list>
42#include <vector>
43
44namespace rtabmap
45{
46
47class Signature;
48
49class RTABMAP_CORE_EXPORT EpipolarGeometry
50{
51public:
52 EpipolarGeometry(const ParametersMap & parameters = ParametersMap());
53 virtual ~EpipolarGeometry();
54 bool check(const Signature * ssA, const Signature * ssB);
55 void parseParameters(const ParametersMap & parameters);
56
57 int getMatchCountMinAccepted() const {return _matchCountMinAccepted;}
58 double getRansacParam1() const {return _ransacParam1;}
59 double getRansacParam2() const {return _ransacParam2;}
60
61 void setMatchCountMinAccepted(int matchCountMinAccepted) {_matchCountMinAccepted = matchCountMinAccepted;}
62 void setRansacParam1(double ransacParam1) {_ransacParam1 = ransacParam1;}
63 void setRansacParam2(double ransacParam2) {_ransacParam2 = ransacParam2;}
64
65
66 // STATIC STUFF
67 //epipolar geometry
68 static void findEpipolesFromF(
69 const cv::Mat & fundamentalMatrix,
70 cv::Vec3d & e1,
71 cv::Vec3d & e2);
72
73 static cv::Mat findPFromE(
74 const cv::Mat & E,
75 const cv::Mat & x,
76 const cv::Mat & xp);
77
78 // return fundamental matrix
79 // status -> inliers = 1, outliers = 0
80 static cv::Mat findFFromWords(
81 const std::list<std::pair<int, std::pair<cv::KeyPoint, cv::KeyPoint> > > & pairs, // id, kpt1, kpt2
82 std::vector<uchar> & status,
83 double ransacReprojThreshold = 3.0,
84 double ransacConfidence = 0.99);
85
86 // assume a canonical camera (without K)
87 static void findRTFromP(
88 const cv::Mat & p,
89 cv::Mat & r,
90 cv::Mat & t);
91
92 static cv::Mat findFFromCalibratedStereoCameras(double fx, double fy, double cx, double cy, double Tx, double Ty);
93
94
99 template<typename T>
100 static int findPairs(
101 const std::map<int, T> & wordsA,
102 const std::map<int, T> & wordsB,
103 std::list<std::pair<int, std::pair<T, T> > > & pairs,
104 bool ignoreNegativeIds = true)
105 {
106 int realPairsCount = 0;
107 pairs.clear();
108 for(typename std::map<int, T>::const_iterator i=wordsA.begin(); i!=wordsA.end(); ++i)
109 {
110 if(!ignoreNegativeIds || (ignoreNegativeIds && i->first>=0))
111 {
112 std::map<int, cv::KeyPoint>::const_iterator ptB = wordsB.find(i->first);
113 if(ptB != wordsB.end())
114 {
115 pairs.push_back(std::pair<int, std::pair<T, T> >(i->first, std::make_pair(i->second, ptB->second)));
116 ++realPairsCount;
117 }
118 }
119 }
120 return realPairsCount;
121 }
122
127 template<typename T>
128 static int findPairs(
129 const std::multimap<int, T> & wordsA,
130 const std::multimap<int, T> & wordsB,
131 std::list<std::pair<int, std::pair<T, T> > > & pairs,
132 bool ignoreNegativeIds = true)
133 {
134 const std::list<int> & ids = uUniqueKeys(wordsA);
135 typename std::multimap<int, T>::const_iterator iterA;
136 typename std::multimap<int, T>::const_iterator iterB;
137 pairs.clear();
138 int realPairsCount = 0;
139 for(std::list<int>::const_iterator i=ids.begin(); i!=ids.end(); ++i)
140 {
141 if(!ignoreNegativeIds || (ignoreNegativeIds && *i >= 0))
142 {
143 iterA = wordsA.find(*i);
144 iterB = wordsB.find(*i);
145 while(iterA != wordsA.end() && iterB != wordsB.end() && (*iterA).first == (*iterB).first && (*iterA).first == *i)
146 {
147 pairs.push_back(std::pair<int, std::pair<T, T> >(*i, std::make_pair((*iterA).second, (*iterB).second)));
148 ++iterA;
149 ++iterB;
150 ++realPairsCount;
151 }
152 }
153 }
154 return realPairsCount;
155 }
156
161 template<typename T>
162 static int findPairsUnique(
163 const std::multimap<int, T> & wordsA,
164 const std::multimap<int, T> & wordsB,
165 std::list<std::pair<int, std::pair<T, T> > > & pairs,
166 bool ignoreNegativeIds = true)
167 {
168 const std::list<int> & ids = uUniqueKeys(wordsA);
169 int realPairsCount = 0;
170 pairs.clear();
171 for(std::list<int>::const_iterator i=ids.begin(); i!=ids.end(); ++i)
172 {
173 if(!ignoreNegativeIds || (ignoreNegativeIds && *i>=0))
174 {
175 std::list<T> ptsA = uValues(wordsA, *i);
176 std::list<T> ptsB = uValues(wordsB, *i);
177 if(ptsA.size() == 1 && ptsB.size() == 1)
178 {
179 pairs.push_back(std::pair<int, std::pair<T, T> >(*i, std::pair<T, T>(ptsA.front(), ptsB.front())));
180 ++realPairsCount;
181 }
182 else if(ptsA.size()>1 && ptsB.size()>1)
183 {
184 // just update the count
185 realPairsCount += ptsA.size() > ptsB.size() ? ptsB.size() : ptsA.size();
186 }
187 }
188 }
189 return realPairsCount;
190 }
191
196 template<typename T>
197 static int findPairsAll(
198 const std::multimap<int, T> & wordsA,
199 const std::multimap<int, T> & wordsB,
200 std::list<std::pair<int, std::pair<T, T> > > & pairs,
201 bool ignoreNegativeIds = true)
202 {
203 const std::list<int> & ids = uUniqueKeys(wordsA);
204 pairs.clear();
205 int realPairsCount = 0;;
206 for(std::list<int>::const_iterator iter=ids.begin(); iter!=ids.end(); ++iter)
207 {
208 if(!ignoreNegativeIds || (ignoreNegativeIds && *iter>=0))
209 {
210 std::list<T> ptsA = uValues(wordsA, *iter);
211 std::list<T> ptsB = uValues(wordsB, *iter);
212
213 realPairsCount += ptsA.size() > ptsB.size() ? ptsB.size() : ptsA.size();
214
215 for(typename std::list<T>::iterator jter=ptsA.begin(); jter!=ptsA.end(); ++jter)
216 {
217 for(typename std::list<T>::iterator kter=ptsB.begin(); kter!=ptsB.end(); ++kter)
218 {
219 pairs.push_back(std::pair<int, std::pair<T, T> >(*iter, std::pair<T, T>(*jter, *kter)));
220 }
221 }
222 }
223 }
224 return realPairsCount;
225 }
226
227 static cv::Mat linearLSTriangulation(
228 cv::Point3d u, //homogenous image point (u,v,1)
229 cv::Matx34d P, //camera 1 matrix 3x4 double
230 cv::Point3d u1, //homogenous image point in 2nd camera
231 cv::Matx34d P1); //camera 2 matrix 3x4 double
232
233 static cv::Mat iterativeLinearLSTriangulation(
234 cv::Point3d u, //homogenous image point (u,v,1)
235 const cv::Matx34d & P, //camera 1 matrix 3x4 double
236 cv::Point3d u1, //homogenous image point in 2nd camera
237 const cv::Matx34d & P1); //camera 2 matrix 3x4 double
238
239 static double triangulatePoints(
240 const cv::Mat& pt_set1, //2xN double
241 const cv::Mat& pt_set2, //2xN double
242 const cv::Mat& P, // 3x4 double
243 const cv::Mat& P1, // 3x4 double
244 pcl::PointCloud<pcl::PointXYZ>::Ptr & pointcloud,
245 std::vector<double> & reproj_errors);
246
247private:
248 int _matchCountMinAccepted;
249 double _ransacParam1;
250 double _ransacParam2;
251};
252
253} // namespace rtabmap
Wrappers of STL for convenient functions.
std::list< K > uUniqueKeys(const std::multimap< K, V > &mm)
Definition UStl.h:46
std::vector< V > uValues(const std::multimap< K, V > &mm)
Definition UStl.h:100
static int findPairsUnique(const std::multimap< int, T > &wordsA, const std::multimap< int, T > &wordsB, std::list< std::pair< int, std::pair< T, T > > > &pairs, bool ignoreNegativeIds=true)
static int findPairs(const std::multimap< int, T > &wordsA, const std::multimap< int, T > &wordsB, std::list< std::pair< int, std::pair< T, T > > > &pairs, bool ignoreNegativeIds=true)
static int findPairs(const std::map< int, T > &wordsA, const std::map< int, T > &wordsB, std::list< std::pair< int, std::pair< T, T > > > &pairs, bool ignoreNegativeIds=true)
static int findPairsAll(const std::multimap< int, T > &wordsA, const std::multimap< int, T > &wordsB, std::list< std::pair< int, std::pair< T, T > > > &pairs, bool ignoreNegativeIds=true)
Represents a node in RTAB-Map's pose graph.
Definition Signature.h:84
std::map< std::string, std::string > ParametersMap
Parameter keys mapped to their values, as used by every configurable class (see Parameters).
Definition Parameters.h:44