Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
GenSynthMatches.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #ifndef __GenSynthMatches_hh__
26 #define __GenSynthMatches_hh__
27 
28 #include <bias_config.h>
29 
30 #include <Base/Common/BIASpragmaStart.hh>
31 
32 #include <iostream>
33 #include <string>
34 #include <vector>
35 
36 #include <Base/Image/Image.hh>
37 #include <Base/Geometry/HomgPoint2D.hh>
38 #include <Base/Geometry/HomgPoint3D.hh>
39 #include <Base/Geometry/KMatrix.hh>
40 #include <Base/Geometry/FMatrixBase.hh>
41 #include <Base/Geometry/EuclideanTransf3D.hh>
42 #include <Base/Geometry/RMatrixBase.hh>
43 #include <Base/Geometry/PMatrixBase.hh>
44 #include <Utils/Param.hh>
45 #include <Base/Math/Random.hh>
46 #include "MatchDataBase.hh"
47 
48 
49 namespace BIAS{
50 
51  class PMatrix;
52  class HomgPoint3D;
53 
54 #define GSM_CAM_MOTION 0x00010000
55 #define GSM_VIEWINGVOL 0x00020000
56 #define GSM_3D_POINTS 0x00040000
57 #define GSM_2D_MATCHES 0x00080000
58 #define GSM_MOVIN_TRANSF 0x00100000
59 #define GSM_MOVIN_3D_POINTS 0x00200000
60 #define GSM_FMATRIX 0x00400000
61 #define GSM_NOISE 0x00800000
62 
63 #define GSM_RETURN_OK 0
64 #define GSM_RETURN_NO_STATIC_3D_POINTS -1
65 #define GSM_RETURN_NO_MOVING_3D_POINTS -2
66 #define GSM_RETURN_STATIC_OBJECT_NOT_VISIBLE -3
67 #define GSM_RETURN_MOVING_OBJECT_NOT_VISIBLE -4
68 #define GSM_RETURN_ERROR_HANDLING_STATIC_POINT -5
69 #define GSM_RETURN_ERROR_HANDLING_MOVING_POINT -6
70 
71  /** @class GenSynthMatches
72  @ingroup g_utils
73  @brief class for generating synthetic matches
74  @author woelk 2004 */
75  class BIASUtils_EXPORT GenSynthMatches : public MatchDataBase
76  {
77  public:
78  GenSynthMatches(Param &para);
79  virtual ~GenSynthMatches();
80 
81  // clears all internal vectors and resets K and Ki
82  // also resets _rand
83  void Reset();
84 
85  /** @brief Create static and moving 3D points
86  see _CreateCamMovement for explanation of parameter */
87  int Create3DPoints(bool fixed_errors = false);
88  ///@brief Replace the static 3D point at index by a newly generated one
89  int CreateStatic3DPoint(int index);
90  ///@brief Replace the moving 3D point at index by a newly generated one
91  int CreateMoving3DPoint(int index);
92  /** returns negative value, if error occurs, 0 on success
93  see _CreateCamMovement for explanation of parameter "fixed_errors" */
94  int CreateMatches(bool points3D_set_by_user = false,
95  bool fixed_errors = false);
96  ///@brief create matches for existing 3D points
97  int CreateMatches(int &index);
98 
99  /** see _CreateCamMovement for docu */
100  inline void CreateCamMovement(const bool fixed_error = false)
101  {_CreateCamMovement(fixed_error);} ;
102 
103  /// fills _MovingTransform
104  inline void CreateMovingTransforms() {_CreateMovingTransforms();};
105 
106  ///@brief set the static 3D points used for computation
107  inline int SetStatic3DPoints(const std::vector<BIAS::HomgPoint3D> &points){
108  _StaticPoints = points;
109  return _StaticPoints.size();
110  }
111  ///@brief read the static 3D points used for computation
112  inline int GetStatic3DPoints(std::vector<BIAS::HomgPoint3D> &points){
113  points = _StaticPoints;
114  return _StaticPoints.size();
115  };
116  ///@brief set the moving 3D points used for computation
117  inline int SetMoving3DPoints(const std::vector<BIAS::HomgPoint3D> &points){
118  _MovingPoints = points;
119  return _MovingPoints.size();
120  };
121  ///@brief read the moving 3D points used for computaion
122  inline int GetMoving3DPoints(std::vector<BIAS::HomgPoint3D> &points){
123  points = _MovingPoints;
124  return _MovingPoints.size();
125  };
126 
127  inline int GetMovingTransforms(std::vector<BIAS::EuclideanTransf3D> &res) {
128  res = _MovingTransform;
129  return _MovingTransform.size();
130  }
131  inline int GetNumMovingObjects() {
132  return *_NumMovingObjects;
133  }
134 
135  void DrawTrue(Image<unsigned char>& im, int minindex=0,
136  int maxindex=1) const;
137  void DrawNoisy(Image<unsigned char>& im, int minindex=0,
138  int maxindex=1) const;
139 
140  /// write in ascii file format
141  int WriteData(std::string file) const;
142  /// read in ascii file format
143  int ReadData(std::string file);
144 
145  /// binary read
146  int Read(const std::string& fname);
147  /// binary read
148  virtual int Read(std::istream& is);
149 
150  /// binary write
151  int Write(const std::string& fname) const;
152  /// binary write
153  virtual int Write(std::ostream& os) const;
154 
155  /// R = _GT_R[imNo];
156  void GetGTR(int imNo, RMatrixBase& R) const;
157  void GetGTR(std::vector<RMatrixBase>& R) const;
158 
159  /// C = _GT_C[imNo];
160  void GetGTC(int imNo, HomgPoint3D& C) const;
161  void GetGTC(std::vector<HomgPoint3D>& C) const;
162 
163  /// P = _GT_P[imNo];
164  void GetGTP(int imNo, PMatrixBase& P) const;
165  void GetGTP(std::vector<PMatrixBase>& P) const;
166 
167  /// p = _GT_Points[imNo];
168  void GetGTPoints(int imNo, std::vector<HomgPoint2D>& p) const;
169  void GetGTPoints(std::vector<std::vector<HomgPoint2D> >& p) const;
170 
171  /// p = _GT_NormalizedPoints[imNo];
172  void GetGTNormalizedPoints(int imNo, std::vector<HomgPoint2D>& p) const;
173  void GetGTNormalizedPoints(std::vector<std::vector<HomgPoint2D> >& p)const;
174 
175  /// p = _StaticPoints;
176  void Get3DPoints(std::vector<HomgPoint3D>& p) const;
177 
178  /// p = _MovingTransform[frame] * _MovingPoints;
179  void Get3DMovingPoints(std::vector<HomgPoint3D>& p,const int frame=0)const;
180 
181  /// inl[i] indicates if Get(GT)Points()[i] is outlier
182  void GetInliers(std::vector<bool>& inl) const;
183 
184  // returns the fundamental matrices for each indep. motion
185  void GetGTNormalizedF(std::vector<std::vector<FMatrixBase> >& F) const;
186 
187  GenSynthMatches &operator=(const MatchDataBase& mdb);
188 
189  friend BIASUtils_EXPORT std::ostream&
190  operator<<(std::ostream& os,const GenSynthMatches& m);
191 
192  friend BIASUtils_EXPORT std::istream&
193  operator>>(std::istream& os, GenSynthMatches& m);
194 
195  inline void GetImageSize(int & width, int & height) const
196  { width = *_ImWidth; height = *_ImHeight; }
197 
198  inline double GetSigmaImagePoints() const
199  { return *_SigmaImagePoints; }
200 
201  /** when getting point list for an image,
202  inidices
203  0 - max_inlier-1 are true matches generated by static
204  object
205  max_inlier - max_outlier-1 are uniform distributed random outliers
206 
207  max_outlier - max_mov_obj-1 are the points generated by the moving
208  objects */
209  void GetPointSourceBorders(int &max_inlier, int &max_outlier,
210  int &max_mov_obj) const
211  { max_inlier = *_NumStaticPoints;
212  max_outlier = *_NumStaticPoints + *_NumOutliers;
213  max_mov_obj = *_NumMovingPoints * *_NumMovingObjects +
214  *_NumStaticPoints + *_NumOutliers; }
215 
216  int CreateGTVRML(const std::string& file_name) const;
217 
218  protected:
220  BIAS::Param *_para; // just needed in WriteData
221 
222  // internal calibration matrix
223  //BIAS::KMatrix _K, _Ki; // _Ki is inverse of _K
224  // vector of rotation matrizes of cameras, true and with gaussian noise
225  // with distribution para _SigmaCamRot?
226  std::vector<BIAS::RMatrixBase> _GT_R;//, _R;
227  // vector of camera centers, true and with gaussian noise
228  // with distribution para _SigmaCamCenter?
229  std::vector<BIAS::HomgPoint3D> _GT_C;//, _C;
230  // vector of projection matrizes, true (_K, _GT_R[i], GT_C[i])
231  // and composed from _K, _R[i] and _C[i]
232  std::vector<BIAS::PMatrixBase> _GT_P;//, _P;
233  // vector of transform between moving and static coo
234  std::vector<BIAS::EuclideanTransf3D> _MovingTransform;
235  // vector of scene points
236  std::vector<BIAS::HomgPoint3D> _StaticPoints;
237  // vector of points from moving object in local coo of moving obj,
238  // with (0, 0, 0) in center of moving obj
239  std::vector<BIAS::HomgPoint3D> _MovingPoints;
240 
241  int *_NumStaticPoints, *_NumOutliers; // *_NumImages, *_NumPoints
242  int *_ImWidth, *_ImHeight;
243  double *_FocalLength, *_PrinciplePointX, *_PrinciplePointY;
244 
245  // noise for different values
246  double *_SigmaImagePoints, *_MaxDispOutliers, *_OutlDirChange;
248  double *_SigmaCamCenterX, *_SigmaCamCenterY, *_SigmaCamCenterZ;
249  double *_SigmaCamRotX, *_SigmaCamRotY, *_SigmaCamRotZ;
250 
251  // minimal and maximal distance of 3D points from camera plane
252  double *_MinCamDistance, *_MaxCamDistance;
253 
254  bool *_UserGivenP;
255  // only used from param file if _UserGivenP=true
256  BIAS::Vector<double> *_MotionVecX, *_MotionVecY, *_MotionVecZ, *_RotVecX,
257  *_RotVecY, *_RotVecZ;
258 
259  // parameters for generating moving points
260  int *_NumMovingPoints, *_NumMovingObjects;
261  // e.g. *_SizeMovingObjX[k] is the x size of the (k)th moving object
262  BIAS::Vector<double> *_SizeMovingObjX, *_SizeMovingObjY, *_SizeMovingObjZ;
263 
264  // describes the motion of the moving objs
265  // e.g. *_MovingObjMotionVecX[k] is the x coo of the center position
266  // of the (k div *_NumMovinObj)th moving object in the
267  // (k div *_NumMovinObj)th image
268  // i.e. *_MovingObjMotionVecX="1 2 3 7 8 9" and *_NumMovinObj=2
269  // means object number 1 moves from x=1 in image 1 to x=2 in image 2
270  // to x=3 in image 3 while object number 2 moves from x=7 in image 1
271  // to x=8 in image 2 to x=9 in image 3
272  BIAS::Vector<double> *_MovingObjMotionVecX, *_MovingObjMotionVecY,
273  *_MovingObjMotionVecZ, *_MovingObjRotVecX, *_MovingObjRotVecY,
275 
276  // the generated match data: GT means ground truth
277  // _GT_Points[k][i] is point number i in image k
278  // _Points are generated using noisy data
279  // for inliers the folowing is true
280  // _GT_Points[k][i]=_GT_P[k]*_StaticPoints[i]
281  // _Points[k][i]=_P[k]*_StaticPoints[i] + Noise(_SigmaImagePoints)
282  std::vector<std::vector<HomgPoint2D> > _GT_Points;
283  std::vector<std::vector<HomgPoint2D> > _GT_NormalizedPoints;
284 
285  /** @brief fills _k, _Ki, _GT_R, _R, GT_C, _C, _GT_P and _P
286 
287  When "fixed_errors" is set to true, the noisy camera position is set
288  such that:
289  - The rotational error is assumed to be given as axis and angle. The
290  axis is choosen randomly and the angle is exactly as indicated by
291  *_SigmaCamRotX (in rad).
292  - The translational error is assumed to be given as an angle. It is
293  choosen such the angle between the epipole in the noise free case and
294  the epipole in the noisey case form exactly the angle indicated by
295  *_SigmaCamCenterX (in rad). The translational error thus alway lies in
296  the plane perpendicular to the ground truth epipole. Its direction in
297  the plane is choosen randomly and its magnitude is choosen such that
298  the angles between the epipoles is exactly *_SigmaCamCenterX (in rad)
299 
300  When "fixed_errors" is set to false, the normal distributed noise with
301  diagonal covariance matrix whose elements are indicated by parameters
302  *_SigmaCamCenter? are added to each camera positions. The camera
303  orientation is computed using the euler angles from *_RotVec?[frame]
304  with additional normal disributed noise as indiceated by
305  *_SigmaCamRot?.
306 
307  @author woelk 01/2007 */
308  void _CreateCamMovement(const bool fixed_error);
309 
310  /// fills _MovinTransforms
311  void _CreateMovingTransforms();
312 
313  /// fills _StaticPoints
314  /// returns -1 if no common viewing volume of all cameras is present
315  int _CreateStatic3DPoints();
316 
317  /// returns -1 if no corner of moving object boundary is seen in images
318  /// or the center of moving objects is not seen in images
319  int _AreMovingObjectsVisible();
320 
321  /// fills _MovingPoints
322  /// returns -1 if no corner of moving object boundary is seen in images
323  /// or the center of moving objects is not seen in images
324  int _CreateMoving3DPoints();
325 
326  /// fills *_NumStaticPoints first entries of _GT_Points, _Points,
327  /// _GT_NormalizedPoints, _NormalizedPoints
328  /// also resizes the above vectors to
329  /// [*_NumImages][*_NumStaticPoints+*_NumMovingObjects+*_NumOutliers]
330  int _CreateStatic2DPoints(int &index);
331  /// fills *_NumStaticPoints first entries of _GT_Points and
332  /// _GT_NormalizedPoints
333  /// also resizes the above vectors to
334  /// [*_NumImages][*_NumStaticPoints+*_NumMovingObjects+*_NumOutliers]
335  int _CreateStaticGT2DPoints(int &index);
336 
337  /// fills _GT_Points, _Points, _GT_NormalizedPoints, _NormalizedPoints
338  /// from entries [*_NumStaticPoints] to [*_NumStaticPoints+*_NumOutliers-1]
339  void _AddUniformDistributedOutliers();
340 
341  /// fills _GT_Points, _Points, _GT_NormalizedPoints, _NormalizedPoints
342  /// from entries [*_NumStaticPoints+_*NumOutliers] to
343  /// [*_NumStaticPoints+*_NumOutliers+*_NumMovingPoints-1]
344  int _CreateMoving2DPoints(int &index);
345  /// fills _GT_Points, _GT_NormalizedPoints
346  /// from entries [*_NumStaticPoints+_*NumOutliers] to
347  /// [*_NumStaticPoints+*_NumOutliers+*_NumMovingPoints-1]
348  int _CreateMovingGT2DPoints(int &index);
349 
350  /// fills all protected pointer values
351  virtual void _AddParameter(Param &para);
352 
353  /// helper function used for visualizing the matches
354  void _Draw(Image<unsigned char>& im, int minindex, int maxindex,
355  const std::vector<std::vector<HomgPoint2D> >* p) const;
356 
357  /// helper function used for visualizing the matches
358  void _DrawPoints(Image<unsigned char>& im, int index,
359  const std::vector<std::vector<HomgPoint2D> >* p) const;
360 
361  inline bool _IsSeen(HomgPoint2D &p, double border=0.0){
362  p.Homogenize();
363  return (p[0]>=border && p[0]<=(((double)*_ImWidth)-border-1.0) &&
364  p[1]>=border && p[1]<=(((double)*_ImHeight)-border-1.0));
365  }
366 
367  bool _CheckParams() const;
368 
369  int CreateVRML_(const std::string& file_name,
370  const std::vector<PMatrix> &P,
371  const std::vector<HomgPoint3D>& p) const;
372 
373  }; // class
374 
375  BIASUtils_EXPORT std::ostream& operator<<(std::ostream& os, const GenSynthMatches& m);
376  //BIASUtils_EXPORT std::istream& operator>>(std::istream& os, GenSynthMatches m);
377 
378 } // namespace
379 
380 
381 #include <Base/Common/BIASpragmaEnd.hh>
382 
383 #endif
std::vector< BIAS::HomgPoint3D > _MovingPoints
BIAS::Vector< double > * _MovingObjRotVecZ
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
int GetMoving3DPoints(std::vector< BIAS::HomgPoint3D > &points)
read the moving 3D points used for computaion
void CreateCamMovement(const bool fixed_error=false)
see _CreateCamMovement for docu
bool _IsSeen(HomgPoint2D &p, double border=0.0)
BIAS::Vector< double > * _SizeMovingObjZ
HomgPoint2D & Homogenize()
homogenize class data member elements to W==1 by divison by W
Definition: HomgPoint2D.hh:215
std::vector< BIAS::RMatrixBase > _GT_R
int SetStatic3DPoints(const std::vector< BIAS::HomgPoint3D > &points)
set the static 3D points used for computation
std::vector< std::vector< HomgPoint2D > > _GT_Points
std::vector< BIAS::HomgPoint3D > _StaticPoints
class for generating synthetic matches
void GetPointSourceBorders(int &max_inlier, int &max_outlier, int &max_mov_obj) const
when getting point list for an image, inidices 0 - max_inlier-1 are true matches generated by static ...
int SetMoving3DPoints(const std::vector< BIAS::HomgPoint3D > &points)
set the moving 3D points used for computation
void CreateMovingTransforms()
fills _MovingTransform
int GetStatic3DPoints(std::vector< BIAS::HomgPoint3D > &points)
read the static 3D points used for computation
class for representing matches, used in GenSynthMatches and biasshowsm
int GetMovingTransforms(std::vector< BIAS::EuclideanTransf3D > &res)
class BIASGeometry_EXPORT PMatrix
void GetImageSize(int &width, int &height) const
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
Definition: HomgPoint3D.hh:61
describes a projective 3D -&gt; 2D mapping in homogenous coordinates
Definition: PMatrixBase.hh:74
std::ostream & operator<<(std::ostream &os, const Array2D< T > &arg)
Definition: Array2D.hh:260
This class Param provides generic support for parameters.
Definition: Param.hh:231
std::vector< BIAS::EuclideanTransf3D > _MovingTransform
double GetSigmaImagePoints() const
Implements a 3D rotation matrix.
Definition: RMatrixBase.hh:44
BIAS::Vector< double > * _RotVecZ
std::vector< BIAS::PMatrixBase > _GT_P
std::vector< BIAS::HomgPoint3D > _GT_C
class for producing random numbers from different distributions
Definition: Random.hh:51
BIASCommon_EXPORT std::istream & operator>>(std::istream &is, BIAS::TimeStamp &ts)
Standard input operator for TimeStamps.
Definition: TimeStamp.cpp:157
class BIASGeometryBase_EXPORT HomgPoint3D
std::vector< std::vector< HomgPoint2D > > _GT_NormalizedPoints