Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Conic2D.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 __CONIC2D_HH__
27 #define __CONIC2D_HH__
28 
29 #include <bias_config.h>
30 #include <Base/Common/W32Compat.hh>
31 #include <math.h>
32 
33 #include <Base/Debug/Error.hh>
34 #include <Base/Debug/Debug.hh>
35 #include <Base/Math/Matrix3x3.hh>
36 #include <Base/Geometry/HomgPoint2D.hh>
37 #include <Base/Geometry/HomgPoint2DCov.hh>
38 #include <Base/Geometry/HomgLine2D.hh>
39 
40 #define CONIC2D_TYPE double
41 
42 #define D_CONIC2D_TAN 0x00000001
43 
44 #define GAUSS2D_CONFIDENCE_20_PERCENT 0.66
45 #define GAUSS2D_CONFIDENCE_39_PERCENT 1.00
46 #define GAUSS2D_CONFIDENCE_50_PERCENT 1.18
47 #define GAUSS2D_CONFIDENCE_68_PERCENT 1.52
48 #define GAUSS2D_CONFIDENCE_90_PERCENT 2.15
49 #define GAUSS2D_CONFIDENCE_95_PERCENT 2.45
50 #define GAUSS2D_CONFIDENCE_99_PERCENT 3.04
51 
52 namespace BIAS {
53 
54  // forward declarations, to reduce compile time, includes are in the cc file
55  template <class T> class Matrix2x2;
56  template <class T> class Image;
57  class Quadric3D;
58  class PMatrix;
59 
60  /** @class Conic2D
61  @ingroup g_geometry
62  @brief A 3x3 matrix representing a conic (cone/plane intersection)
63 
64  A conic is a (contour) line in P2, which resulted from the projection
65  of a quadric (see Quadric3D) of P3. Conics are ellipses, hyperbolas, ...
66  Each point x of the conic C fulfills x^T C x = 0
67  which is a quadratic equation in the components of x.
68  If nothing else is mentioned, we mean point conics, not line conics.
69  @author koeser, 10/2003 */
70 
71  class BIASGeometry_EXPORT Conic2D
72  : public Matrix3x3<CONIC2D_TYPE>, public Debug
73  {
74  public:
75 
76  enum ConicType { Unknown=0, WholePlane, Ellipse, Parabola, Hyperbola,
77  SingleLine, DoubleLine, Point, Empty};
78 
79  /** @brief default constructor sets all fields to zero */
80  Conic2D() {};
81 
82  /** @brief set default matrix values */
83  explicit Conic2D(const MatrixInitType& i)
84  : Matrix3x3<CONIC2D_TYPE>(i){};
85 
86  /** @brief copy constructor uses Conic2D::operator= */
89 
90  /** @brief copy constructor uses Conic2D::operator= */
93 
94  /** @brief check whether this conic is an ellipse */
95  bool IsEllipse() const;
96 
97  /** @brief check whether this conic is a parabola */
98  bool IsParabola() const;
99 
100  /** @brief check whether this conic is a hyperbola */
101  bool IsHyperbola() const;
102 
103  /** @brief check whether this conic is a single line */
104  bool IsSingleLine() const;
105 
106  /** @brief check whether this conic is a double line */
107  bool IsDoubleLine() const;
108 
109  /** @brief check whether this conic is a point */
110  bool IsPoint() const;
111 
112  /** @brief check whether this conic contains no points */
113  bool IsEmpty() const;
114 
115  /** @brief is this a proper conic (parabola/hyperbola/ellipse) with rank3
116  or some of the degenerate cases ? */
117  bool IsProper() const;
118 
119  /** @brief return the type of point conic of (*this) */
120  ConicType GetConicType() const;
121 
122  /** @brief constructs a conic from a point and a covariance matrix
123  @author woelk 06/2005 */
124  void SetPointAndCovariance(const HomgPoint2D& center,
125  const Matrix2x2<CONIC2D_TYPE>& cov,
126  const double& dScale =
127  GAUSS2D_CONFIDENCE_39_PERCENT);
128 
129  /** @brief reconstructs center and covariance matrix from conic
130  @author woelk 06/2005 */
131  bool GetPointAndCovariance(HomgPoint2D& center,
132  Matrix2x2<CONIC2D_TYPE>& cov) const;
133 
134  /** @brief determines if the point is inside/on/outside the conic
135 
136  for "closed" conics, such as ellipses, call this function for
137  a point to determine its position relative to the conic:
138  <0 means: inside the conic
139  >0 means: outside the conic
140  =0 means on the border line
141  @attention: make sure that the conic is normalized
142  LocatePoint internally computes the difference of Mahalanobis distances
143  of point2D from the center and a conic point from the center */
144  inline double LocatePoint(const HomgPoint2D& point2D) const {
145  return ( point2D *((*this)*point2D) ); }
146 
147  /** @brief sets mean of first 2 diag elements to positive value,
148  so that LocatePoint(x)<0 means x is in conic */
149  inline bool Normalize() { double d=(*this)[0][0]+(*this)[1][1];
150  if (fabs(d)<DOUBLE_EPSILON) return false; (*this) /= d; return true;}
151 
152  /** @brief construct an ellipse with explicit parameters
153  @param radius_a radius in (originally) x direction
154  @param radius_b radius in (originally) y direction
155  @param dAngle rotation angle in radiant (math. positive)
156  @center center of ellipse */
157  void SetEllipse(const HomgPoint2D& Center, const double& dAngle,
158  const double& radius_a, const double& radius_b);
159 
160  /** @brief create a degenerate point conic consisting only of one line */
161  void SetSingleLine(HomgLine2D theline);
162 
163  /** @brief create a degenerate point conic consisting of two lines */
164  void SetTwoLines(HomgLine2D theline1,
165  HomgLine2D theline2);
166 
167  /** @brief computes explicit ellipse representation
168 
169  if the given conic is an ellipse, the ellipse parameters are extracted
170  and returned in the functions parameters
171  @param dAngle rotation angle in radiant (math. positive)
172  @param radius_a radius in (originally) x direction
173  @param radius_b radius in (originally) y direction
174  @return returned parameters are only valid if true is returned,
175  otherwise (*this) is no (or a degenerate) ellipse */
176  bool GetEllipseParameters(HomgPoint2D& Center,
177  double& dAngle,
178  double& radius_a,
179  double& radius_b) const;
180 
181  /** returns the center and the two halfaxes of the conic if feasible
182  (f.e. for drawing with the class ImageDraw)
183  @author woelk 06/2005 */
184  bool GetEllipseParameters(double center[2],
185  double a[2], double b[2]) const;
186 
187  /** @brief retrieve one or two lines from a degenerate point conic
188  @return 0 lines contains all lines, other values indicate an error,
189  e.g. the conic does not consist of two lines */
190  int GetLines(std::vector<HomgLine2D>& lines) const;
191 
192  /** @brief if the conic consists of a single point (degenerate case, rank=2)
193  * retrieve this point */
194  int GetPoint(HomgPoint2D& thepoint) const;
195 
196  /** @brief assign a probability of a gaussian distribution to a conic to
197  measure probabilities in the image plane
198 
199  Ellipsoids in 3D space represent iso surfaces of gaussian normal
200  distributions (e.g. of the 99% volume). Projecting a silhouette of such
201  an ellipsoid gives a conic (e.g. an ellipse) in the image.
202  If we want to interpret the conic as the covariance matrix of a gauss
203  within the image plane, we have to convert it in such a way that
204  the ellipse center yields zero and the border (the former conic line)
205  a different value than zero, e.g. a value corresponding to 99%.
206 
207  Afterwards, you can simply compute
208  NormalizationFactor * exp(this->LocatePoint(x))
209  to compute the probability at x
210 
211  @status still have to test this !
212  */
213  bool EllipsoidSilhouetteToGauss(const double& ProbBorder,
214  HomgPoint2D& Center,
215  double& NormalizationFactor);
216 
217  /** @brief draws conic into an image using a brute force method
218  *
219  * Runs across all pixels and checks for zero crossings of LocatePoint.
220  * Really slow, but works for ANY type. If the conic is an ellipse,
221  * a more elegant way of drawing can be found in ImageDraw
222  */
223  void Draw(Image<unsigned char>& img) const;
224 
227  return (*this);
228  };
229 
230  /** @brief constructs a conic that is a projection of the quadric Q using
231  camera matrix P */
232  void SetQuadricProjection(const Quadric3D &Q, const PMatrix& P,
233  bool UseSVD = false);
234 
235  /** @brief returns true if the given Conic intersects with the circle of
236  radius Radius around point C, false otherwise
237 
238  This is a contour line intersection ! Does not test whether circle
239  lies "in" conic or vice versa !
240  @note strongly depends upon numerical robustness of SolveQuartic !
241  You might as well try IntersecConic() to obtain the solutions
242  @status tested */
243  bool IntersectsCircle(const HomgPoint2D& C, double Radius) const;
244 
245  /** @brief returns the dual of this conic, which is the inverse */
246  Conic2D GetDualConic(bool UseSVD = false) const;
247 
248  /** @brief computes signature of matrix,
249  (no. of neg. eigenvalues) - (no. of pos. eigenvalues) or vice versa
250  if there are more pos. eigenvalues */
251  unsigned int GetSignature() const;
252 
253  /** @brief Returns the two points p1 and p2 on the conic subject to the
254  constraint that the two tangents at the points p1 and p2 intersect in
255  the point x.
256  Returns the number of tangent points (1 or 2) or -1 on error
257  @author woelk 06/2005 */
258  int GetTangentPoints(const HomgPoint2D& x, HomgPoint2D& p1,
259  HomgPoint2D& p2) const;
260 
261  /** @brief compute intersection of 2 non-degenerate (full-rank) conics
262  * @author koeser 02/2008
263  * @result 0: intersectionPoints contains all intersections,
264  * other: degenerate case or conics are equal
265  */
266  int IntersectConicProper(const Conic2D& otherconic,
267  std::vector<HomgPoint2D>& intersectionPoints)
268  const;
269 
270  /** @brief compute intersection of 2 conics
271  * @author koeser 02/2008
272  * @result 0: intersectionPoints contains all intersections,
273  * other: degenerate case (infinite solutions)
274  */
275  int IntersectConic(const Conic2D& otherconic,
276  std::vector<HomgPoint2D>& intersectionPoints) const;
277 
278  /** @brief compute intersection of a conic with a line
279  * @author koeser 02/2008
280  * @result 0: intersectionPoints contains all intersections,
281  * other: degenerate case or conics are equal
282  */
283  int IntersectLine(const HomgLine2D& theline,
284  std::vector<HomgPoint2D>& intersectionPoints) const;
285 
286 
287  /** @brief compute approximate offset of a measured point to a known conic
288  *
289  * This uses the linearization of a quadratic equation under the
290  * assumption that the point is close to the conic. With increasing
291  * outside distance to the conic, the offset is underestimated.
292  * See [Kanatani96:Statistical Optimization..., p.169]
293  * @author koeser 02/2009 */
294  int GetLinearizedOffset(const HomgPoint2D& point,
295  HomgPoint2D& distance,
296  const HomgPoint2DCov& pointcov =
298 
299  /** @brief return the squared mahalanobis distance of the point to the conic
300  *
301  * This function can be used for the decision whether a noisy point lies
302  * on a conic curve or not (e.g. threshold the result at 4.0).
303  * @return If point is an observation from a normal distribution with its
304  * mean on the conic curve than the resulting squared mahalanobis
305  * distance is 1D chi-squared distributed. This means e.g. less
306  * than 5% of all such points will have a distance larger than 4
307  * (this is the "2-sigma-band").
308  *
309  * @author koeser */
310  double GetPointDistance(const HomgPoint2D& point,
311  HomgPoint2D& distance,
312  const HomgPoint2DCov& pointcov =
314 
315 
316 
317  protected:
318  /** @return a value classifying the conic:
319  - Discriminant<0: hyperbola
320  - (Discriminant=0: parabola ?)
321  - Discriminant>0: ellipse */
322  double GetDiscriminant() const;
323  };
324 
325  inline std::ostream& operator<<(std::ostream &os, Conic2D::ConicType& t) {
326  switch (t) {
327  case Conic2D::Ellipse :os<<"Ellipse";break;
328  case Conic2D::Parabola :os<<"Parabola";break;
329  case Conic2D::Hyperbola :os<<"Hyperbola";break;
330  case Conic2D::SingleLine:os<<"SingleLine";break;
331  case Conic2D::DoubleLine:os<<"DoubleLine";break;
332  case Conic2D::Point :os<<"Point";break;
333  case Conic2D::Empty :os<<"Empty";break;
334  case Conic2D::WholePlane:os<<"WholePlane";break;
335  case Conic2D::Unknown :os<<"Unknown";break;
336  default:BIASERR("Undefined ConicType !"); BIASABORT;
337  }
338  return os;
339  }
340 
341 }
342 #endif
MatrixInitType
can be passed to matrix constructors to init the matrix with the most often used values ...
Definition: Matrix.hh:59
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
class representing the covariance matrix of a homogenous point 2D
class BIASGeometryBase_EXPORT HomgPoint2DCov
is a &#39;fixed size&#39; quadratic matrix of dim.
Definition: Matrix2x2.hh:48
Conic2D & operator=(const Matrix< CONIC2D_TYPE > &m)
Definition: Conic2D.hh:225
Conic2D(const MatrixInitType &i)
set default matrix values
Definition: Conic2D.hh:83
Conic2D()
default constructor sets all fields to zero
Definition: Conic2D.hh:80
A quadric as a 4x4 matrix describing a surface in 3d projective space defined by a quadratic equation...
Definition: Quadric3D.hh:106
class BIASImageBase_EXPORT Image
Definition: ImageBase.hh:91
double LocatePoint(const HomgPoint2D &point2D) const
determines if the point is inside/on/outside the conic
Definition: Conic2D.hh:144
Ellipse in 2D with dfifferent representations.
Definition: Ellipse.hh:36
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
class BIASGeometry_EXPORT PMatrix
is a &#39;fixed size&#39; quadratic matrix of dim.
Definition: Matrix.hh:54
std::ostream & operator<<(std::ostream &os, const Array2D< T > &arg)
Definition: Array2D.hh:260
matrix class with arbitrary size, indexing is row major.
class BIASMathBase_EXPORT Matrix2x2
Definition: Operators.hh:39
Matrix3x3< T > & operator=(const Matrix3x3< T > &mat)
assignment operator
Definition: Matrix3x3.cpp:704
Conic2D(const Matrix< CONIC2D_TYPE > &m)
copy constructor uses Conic2D::operator=
Definition: Conic2D.hh:91
describes a projective 3D -&gt; 2D mapping in homogenous coordinates
Definition: PMatrix.hh:88
bool Normalize()
sets mean of first 2 diag elements to positive value, so that LocatePoint(x)&lt;0 means x is in conic ...
Definition: Conic2D.hh:149
Conic2D(const Matrix3x3< CONIC2D_TYPE > &m)
copy constructor uses Conic2D::operator=
Definition: Conic2D.hh:87
A 3x3 matrix representing a conic (cone/plane intersection)
Definition: Conic2D.hh:71