Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
EpipolarLine.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 
26 #ifndef __EPIPOLARLINE_HH__
27 #define __EPIPOLARLINE_HH__
28 #include "bias_config.h"
29 
30 #include <Base/Geometry/HomgPoint2D.hh>
31 #include <Base/Geometry/HomgLine2D.hh>
32 #include <Base/ImageUtils/EightWaySymmetry.hh>
33 #include <Base/Debug/Debug.hh>
34 #include <Base/Image/Image.hh>
35 #include <Geometry/Projection.hh>
36 
37 #include "EightWaySymmetryHomg.hh"
38 
39 #include <fstream>
40 #include <cmath>
41 
42 #define LOWER 10E-6
43 #define TWICEPI 6.2831853071795864769252867665590058
44 
45 #define D_EL_SLOPE 1<<0
46 #define D_EL_TRANSF 1<<1
47 #define D_EL_STARTEND 1<<2
48 #define D_EL_NEWSTARTEND 1<<3
49 #define D_EL_ROUND 1<<4
50 #define D_EL_NEXTLINE 1<<5
51 #define D_EL_OFFSET 1<<6
52 #define D_EL_BRES 1<<7
53 #define D_EL_BRESROUND 1<<8
54 #define D_EL_INT 1<<9
55 
56 
57 enum TEdge { TopEdge, BottomEdge, LeftEdge, RightEdge, InvalidEdge };
58 
59 BIASGeometry_EXPORT std::ostream& operator<<(std::ostream& os, const enum TEdge edge);
60 
61 namespace BIAS{
62  /** @ingroup g_geometry
63  @brief A class for representing epipolar lines with useful functions like ScanLine().
64 
65  Coordinate system is image coo system with origin in upper left image corner.
66  Epipole and point at infinty is also stored
67  @author Felix Woelk
68  */
69  class BIASGeometry_EXPORT EpipolarLine : public HomgLine2D, public Debug
70  {
71 
72  public:
73 
74  EpipolarLine();
75 
76  EpipolarLine(const EpipolarLine& line);
77 
79 
80  virtual EpipolarLine& operator=(const EpipolarLine& line);
81 
82  virtual ~EpipolarLine();
83 
84  inline void SetEpipole(HomgPoint2D& ep)
85  { Epipole_ = ep; };
86 
87  virtual inline HomgPoint2D& GetEpipole()
88  { return Epipole_; };
89 
90  virtual inline HomgPoint2D GetCopyOfEpipole() const
91  { return Epipole_; };
92 
93  /** return type,
94  E_NE if 0 < slope < 45 degree
95  NE_N if 45 < slop < 90 ... */
96  inline enum TLineType GetLineType() const
97  { return Type_; }
98 
99  /// angle to abszissa (-pi, pi]
100  inline double GetAngle() const
101  { return Angle_; };
102 
103  // returns (unsigned int)ceil(RMin(whole image) - RMin(epipolar line))
104  inline double GetROffset() const
105  { return ROffset_; };
106 
107  inline double GetDx() const
108  { return dx_; };
109 
110  inline double GetDy() const
111  { return dy_; };
112 
114  { return TransformedStart_; };
115 
117  { return TransformedEnd_; };
118 
120  { return TransformedStart_; };
121 
123  { return TransformedEnd_; };
124 
125  ///
126  virtual inline double GetDeltaR() const
127  { return DeltaR_; };
128 
129  /// distance epipole - close intersection with image border
130  virtual inline double GetRMin() const
131  { return RMinLine_; }
132 
133  /// distance epipole - far intersection with image border
134  virtual inline double GetRMax() const
135  { return RMaxLine_; }
136 
137  ///
139  { return FarIntersection_; };
140 
141  ///
143  { return CloseIntersection_; };
144 
145  ///
147  { return FarIntersection_; };
148 
149  ///
151  { return CloseIntersection_; };
152 
153  virtual inline enum TEdge GetFarEdge() const
154  { return FarEdge_; };
155 
156  virtual inline enum TEdge GetCloseEdge() const
157  { return CloseEdge_; };
158 
159  virtual inline unsigned int GetImageWidth() const
160  { return (unsigned int)(rint(maxx_-minx_)+1); };
161 
162  virtual inline unsigned int GetImageHeight() const
163  { return (unsigned int)(rint(maxy_-miny_)+1); };
164 
165  inline void GetImageBoundaries(double& minx, double& miny,
166  double& maxx, double& maxy) const {
167  minx = minx_;
168  miny = miny_;
169  maxx = maxx_;
170  maxy = maxy_;
171  }
172 
173 
174  virtual inline enum HomgPoint2D::EPosition GetEpipolePosition() const
175  { return EpipolePosition_; };
176 
177  /** returns next line with maximal possible distance without pixel loss */
178  virtual void GetNextLine(bool clockwise, EpipolarLine& next,
179  double dist=1.0);
180 
181  /** assumes that data_ is already set
182  @author woelk
183  @return 0 on success, -1 on error (line out of image) */
184  int Recalc(unsigned int width, unsigned int height,
185  HomgPoint2D& epipole, enum HomgPoint2D::EPosition epos);
186 
187  /** assumes that data_ is already set
188  @author koeser
189  @return 0 on success, -1 on error (line out of image) */
190  int Recalc(const double& minx, const double& miny,
191  const double& maxx,const double& maxy,
192  HomgPoint2D& epipole, enum HomgPoint2D::EPosition epos);
193 
194  /** assumes Width, Height_, data_ and Epipole_ are set correct before
195  @author woelk
196  @return 0 on success, -1 on error (line out of image) */
197  int Recalc();
198 
199 
200  virtual void Init(const double& minx, const double& miny,
201  const double& maxx, const double& maxy,
202  HomgPoint2D& epipole, HomgPoint2D& otherpoint,
203  enum HomgPoint2D::EPosition epos);
204 
205 
206  virtual void Init(unsigned int width, unsigned int height,
207  HomgPoint2D& epipole, HomgPoint2D& otherpoint,
208  enum HomgPoint2D::EPosition epos);
209 
210  /** initialize epipolar line with the two endpoints */
211  virtual void Init(unsigned int width, unsigned int height,
212  HomgPoint2D& epipole, HomgPoint2D& otherpoint);
213 
214  /** scans epipolar line using bresenham, midpint alg. and 8-way symmetry
215  using minimal necessairy pixel step
216  fills resulting linear interpolated pixels values in Destination
217  allocates Destination if NULL to necessairy size
218  sets length to length of Destination
219  Destination is filled from ROffset on, only if Destination != NULL
220  assumes that HomgLine2D::Vector3<double>::data_ is set
221  @return distance image-epipole on ep. line, negative value if error
222  @author Felix Woelk */
223  virtual double ScanLine(Image<unsigned char>& source,
224  HomgPoint2D& epipole,
225  enum HomgPoint2D::EPosition epos, double RMin,
226  double RMax,
227  bool forward, unsigned int& length,
228  unsigned char* Destination);
229 
230  /** draws (*this) into image (only from Pinf to epipole*/
231  template <class PixelType>
232  void Draw(Image<PixelType>& im);
233 
234  /** draws infinite line in image, not stopping at epipole */
235  template <class PixelType>
236  void DrawWhole(Image<PixelType>& im);
237 
238  /** @brief project the plane given by two local rays and the camera center
239  * into the image (ANY projection) using cubemapping
240  * @author koeser 12/2006
241  *
242  * Constructs a cubemap with 90 degrees fov for each cube face and
243  * computes the projection of the plane onto the cubefaces (bresenham on
244  * the lines). Forward mapping is used to determine the pixels in the
245  * original image which are marked as being on the line
246  *
247  * */
248  template <class PixelType>
249  static void DrawDistortedLine(const Vector3<double>& epipoleRayOrigImage,
250  const Vector3<double>& infRayOrigImage,
251  Image<PixelType>& image,
252  ProjectionParametersBase* paramsOrig,
253  PixelType* Color = NULL,
254  int lineWidth = 1);
255 
256 
257  /** @brief visualize the epipolar plane/line as a curve in the image for
258  * any Projection
259  * @author koeser 12/2006
260  *
261  * The epipolar plane is defined by the centers of two cameras (baseline)
262  * and some viewing ray in one of the cameras. This function determines
263  * the plane and uses cube-mapping to project it into some image with
264  * given arbitrary projection. Forward mapping is used, therefore
265  * the line may not be absolutely exact.
266  * Calls DrawDistortedLine() after determining paramerters */
267  template <class PixelType>
268  static void ProjectEpipolarPlane(const Projection& thisP,
269  Image<PixelType> &thisIm,
270  const Vector3<double>& otherCamCenter,
271  const Vector3<double>& RayDir,
272  PixelType* Color = NULL,
273  int lineWidth = 1);
274 
275  protected:
277  enum HomgPoint2D::EPosition EpipolePosition_;
279  enum HomgPoint2D::EPosition PInfinityPosition_;
280 
281  enum TLineType Type_;
282  ///int Width_; // width of image
283  ///int Height_; // height of image
284  /// image borders in xdir, typically 0, width-1
285  double minx_, maxx_;
286  /// image borders in xdir, typically 0, height-1
287  double miny_, maxy_;
288  double Angle_; // angle of edge (-pi, pi]
289  double DeltaR_; // distance on line resulting from step = 1 on x axis
290  HomgPoint2D FarIntersection_; // intersection with close image border
291  HomgPoint2D CloseIntersection_; // intersection with far image border
292  enum TEdge FarEdge_, CloseEdge_; //
293  double RMinLine_, RMaxLine_; // range for r values for this line
294  // distance rectified image border, image
295  unsigned int ROffset_;
296  unsigned int Length_;
297  // steps resulting in step=1 on epipolarline, always positive
298  // for transformed line
299  double dx_, dy_;
301  // just for drawing
303 
304  // used for modified bresenham
306  HomgPoint2D TransformedStart_, TransformedEnd_;
309 
310  virtual double ScanLine_(Image<unsigned char>& source,
311  unsigned int ROffset,
312  unsigned int& length, unsigned char *Destination,
313  bool forward);
314  virtual void CalcTransformed_();
315 
316  bool DetermineIntersectionsAndEpipoleDistances_(HomgPoint2D& point);
317 
318  virtual void Interpolate_(enum TLineType& type, unsigned char **ida,
319  int& x, int& y, double& g,
320  unsigned char& result);
321  };
322 
323  /** \struct TIntersect
324  * \brief Used in function DetermineStartEnd_
325  * \internal */
326  struct TIntersect {
327  enum TEdge Edge;
331  };
332 
333 } // namespace
334 
335 #endif // __EPIPOLARLINE_HH__
336 
337 
338 
339 
340 
341 
342 
double minx_
int Width_; // width of image int Height_; // height of image image borders in xdir, typically 0, width-1
virtual HomgPoint2D & GetFarIntersectionWithImageBorder()
HomgPoint2D FarIntersection_
HomgPoint2D Epipole_
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
Used in function DetermineStartEnd_.
virtual unsigned int GetImageWidth() const
HomgPoint2D GetTransformedEnd() const
double GetDy() const
std::ostream & operator<<(std::ostream &os, const AppData &ad)
HomgPoint2D CloseIntersection_
void GetImageBoundaries(double &minx, double &miny, double &maxx, double &maxy) const
double GetAngle() const
angle to abszissa (-pi, pi]
void SetEpipole(HomgPoint2D &ep)
Definition: EpipolarLine.hh:84
virtual double GetRMax() const
distance epipole - far intersection with image border
double miny_
image borders in xdir, typically 0, height-1
virtual double GetDeltaR() const
This class hides the underlying projection model, like projection matrix, spherical camera...
Definition: Projection.hh:70
HomgPoint2D PInfinity_
HomgPoint2D Start_
EightWaySymmetryHomg Sym_
unsigned int Length_
double GetDx() const
a line l = (a b c)^T is a form of the implicit straight line equation 0 = a*x + b*y + c if homogenize...
Definition: HomgLine2D.hh:48
double GetROffset() const
HomgPoint2D & GetTransformedEnd()
HomgPoint2D TransformedStart_
HomgPoint2D & GetTransformedStart()
virtual HomgPoint2D & GetEpipole()
Definition: EpipolarLine.hh:87
unsigned int ROffset_
virtual double GetRMin() const
distance epipole - close intersection with image border
virtual HomgPoint2D GetCopyOfEpipole() const
Definition: EpipolarLine.hh:90
virtual unsigned int GetImageHeight() const
BIAS::HomgPoint2D Point
direction of line if start is in coordinate origin as given by compass
virtual HomgPoint2D GetCopyOfFarIntersectionWithImageBorder() const
HomgPoint2D Current_
Camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
HomgPoint2D GetTransformedStart() const
virtual HomgPoint2D & GetCloseIntersectionWithImageBorder()
HomgPoint2D TransformedCurrent_
TLineType
direction of line if start is in coordinate origin as given by compass
virtual HomgPoint2D GetCopyOfCloseIntersectionWithImageBorder() const
A class for representing epipolar lines with useful functions like ScanLine().
Definition: EpipolarLine.hh:69