Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleConic.cpp
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  @example ExampleConic.cpp
27  @relates Conic2D
28  @brief Example for conic usage
29  @ingroup g_examples
30  @author MIP
31 */
32 
33 #include <Base/Common/W32Compat.hh>
34 #include <Base/Geometry/HomgPoint2D.hh>
35 #include <Geometry/Conic2D.hh>
36 #include <Base/Image/Image.hh>
37 #include <Base/Image/ImageIO.hh>
38 #include <Geometry/Quadric3D.hh>
39 #include <Geometry/PMatrix.hh>
40 
41 using namespace BIAS;
42 using namespace std;
43 
44 #define RADIUS 50.0
45 
46 int main()
47 {
48 
49  cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ ExampleConic $$$$$$$$$$$$$$$$$$$$$$"
50  << endl;
51  PMatrix P_current;
52  P_current.SetIdentity();
53 
54  Conic2D Conic, Circle;
55  HomgPoint2D C(320,240), C2(550,240);
56  Conic.SetEllipse(C, 35.0 *M_PI/180.0, 180.0, 70);
57 
58  // ****** intersection of conic and circle *******
59  for (unsigned int i=0; i<15; i++) {
60  C2[0] -= 25.0;
61  Circle.SetEllipse(C2, 0.0, RADIUS, RADIUS);
62  cout << "Circle center is now at "<<C2<<endl;
63  if (Conic.IntersectsCircle(C2, RADIUS)) cout << "Intersection :"<<i<<endl;
64  else cout << "No Intersection !"<<endl;
65  Image<unsigned char> TestImage(640,480);
66  TestImage.Clear(200);
67  Circle.Draw(TestImage);
68  Conic.Draw(TestImage);
69  char filename[1000];
70  sprintf(filename,"testimage%03d",i);
71  ImageIO::Save(filename,TestImage,ImageIO::FF_pgm);
72  }
73 
74 
75  // ********** conic from quadric ****************
76  HomgPoint3D QC(0,0,20); // quadric center
77 
78  Vector3<double> S, G_0(1,0,0), H_0(0,1,0), I_0(0,0,1);
79  BIAS::HomgPoint3D G(QC), H(QC), I(QC);
80 
81 
82  CovMatrix3x3 Cov;
83  Cov.SetIdentity();
84  Cov[0][0] = 100;
85  Cov[1][1] = 64;
86 
87  Matrix3x3<double> VT, tmp;
88 #ifdef BIASASSERT_ISACTIVE
89  int resPCA =
90 #endif
91  Cov.GetPCA(S, VT);
92  BIASASSERT(resPCA == 0);
93  cout <<"Singular values of Cov are "<<S<<endl;
94 
95  tmp = VT;
96  tmp.GetInverse(VT);
97 
98  // get main axes in wcs
99  G_0 = VT * G_0; G_0.Normalize();
100  H_0 = VT * H_0; H_0.Normalize();
101  I_0 = VT * I_0; I_0.Normalize();
102 
103  // VT = VT.Transpose();
104  // BIAS::Vector3<double> D0,D1,D2;
105  // D0 = VT.GetRow(0); D0.Normalize();
106  // D1 = VT.GetRow(1); D1.Normalize();
107  // D2 = VT.GetRow(2); D2.Normalize();
108 
109  for (unsigned int l=0; l<3; l++) {
110  G[l] += sqrt(S[0]) * G_0[l] * GAUSS3D_CONFIDENCE_99_PERCENT;
111  H[l] += sqrt(S[1]) * H_0[l] * GAUSS3D_CONFIDENCE_99_PERCENT;
112  I[l] += sqrt(S[2]) * I_0[l] * GAUSS3D_CONFIDENCE_99_PERCENT;
113  }
114 
115  // G, H and I are now extremal points of the ellipsoid, i.e. they are
116  // each of them is the farest point from the center in the main dirs
117 
118 
119  // check if ellipsoid center projects into conic, MUST always be true
120  Quadric3D TestQ(QC, Cov, GAUSS3D_CONFIDENCE_99_PERCENT );
121  cout << "Quadric is "<< TestQ << endl;
122 
123  Conic2D TestC; TestC.SetQuadricProjection(TestQ, P_current);
124  cout << "Conic is "<<(TestC.IsEllipse()?"an ":"no ")
125  << "ellipse: "<< TestC << endl;
126 
127  BIAS::HomgPoint3D *pPoint = &QC;
129  for (int u=0; u<4; u++) {
130  switch (u) {
131  case 0:pPoint = &QC;break;
132  case 1:pPoint = &G; break;
133  case 2:pPoint = &H; break;
134  case 3:pPoint = &I; break;
135  default:BIASERR("Debug loop out of range !");
136  }
137  Projection = P_current * (*pPoint);
138  Projection.Homogenize();
139 
140  double Location = TestQ.LocatePoint(*pPoint);
141  cout <<"Computing point on quadric ! Location is "<<Location
142  <<", pointindex[u] is "<<u<<endl;;
143 
144  Location = TestC.LocatePoint(Projection);
145  if (Location>DBL_EPSILON) {
146  BIASERR("Covariance ellipsoid projection: Point["<<u<< "] not in"
147  <<" projection area !"<<endl<<(*pPoint)<<" -> "
148  << Projection<<endl<<" Location is "<<Location
149  <<", Conic is "<<TestC
150  <<", Covariance matrix is "<< Cov
151  <<" sigmas are "<<S[0]<<" "<<S[1]<<" "<<S[2]<<endl
152  <<"Ellipsoid center is at "<< QC);
153  if (TestC.IsEllipse()) {
154  BIAS::HomgPoint2D Center;
155  double dAngle, radius_a, radius_b;
156  if (TestC.GetEllipseParameters(Center,dAngle,radius_a,radius_b)) {
157  BIASERR("Ellipse at "<<Center<<" with "<<radius_a<<"/"<<radius_b
158  <<" rotated by "<<dAngle/M_PI*180.0
159  <<" degrees counterclockwise.");
160  } else {
161  BIASERR("Error retrieving explicit ellipse parameters.");
162  }
163  } else {
164  BIASERR("Projection is NO ellipse !");
165  }
166  }
167  }
168 
169 
170  return 0;
171 }
172 
173 
174 
175 
176 
177 
178 
179 
180 
181 
182 
183 
184 
185 
186 
187 
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
bool GetEllipseParameters(HomgPoint2D &Center, double &dAngle, double &radius_a, double &radius_b) const
computes explicit ellipse representation
Definition: Conic2D.cpp:432
bool IsEllipse() const
check whether this conic is an ellipse
Definition: Conic2D.cpp:93
int GetPCA(Vector3< COVMATRIX3X3_TYPE > &S, Matrix3x3< COVMATRIX3X3_TYPE > &VT)
HomgPoint2D & Homogenize()
homogenize class data member elements to W==1 by divison by W
Definition: HomgPoint2D.hh:215
void SetEllipse(const HomgPoint2D &Center, const double &dAngle, const double &radius_a, const double &radius_b)
construct an ellipse with explicit parameters
Definition: Conic2D.cpp:299
A quadric as a 4x4 matrix describing a surface in 3d projective space defined by a quadratic equation...
Definition: Quadric3D.hh:106
double LocatePoint(const HomgPoint2D &point2D) const
determines if the point is inside/on/outside the conic
Definition: Conic2D.hh:144
This class hides the underlying projection model, like projection matrix, spherical camera...
Definition: Projection.hh:70
void SetIdentity()
set the elements of this matrix to the matrix 1 0 0 0 0 1 0 0 0 0 1 0 which is no strict identity (!)...
Definition: Matrix3x4.hh:240
int GetInverse(Matrix3x3< T > &inv) const
Matrix inversion: inverts this and stores resulty in argument inv.
Definition: Matrix3x3.cpp:373
class for 3x3 covariance matrices
Definition: CovMatrix3x3.hh:50
void SetQuadricProjection(const Quadric3D &Q, const PMatrix &P, bool UseSVD=false)
constructs a conic that is a projection of the quadric Q using camera matrix P
Definition: Conic2D.cpp:534
static int Save(const std::string &filename, const ImageBase &img, const enum TFileFormat FileFormat=FF_auto, const bool sync=BIAS_DEFAULT_SYNC, const int c_jpeg_quality=BIAS_DEFAULT_IMAGE_QUALITY, const bool forceNewID=BIAS_DEFAULT_FORCENEWID, const bool &writeMetaData=true)
Export image as file using extrnal libs.
Definition: ImageIO.cpp:725
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
Definition: HomgPoint3D.hh:61
bool IntersectsCircle(const HomgPoint2D &C, double Radius) const
returns true if the given Conic intersects with the circle of radius Radius around point C...
Definition: Conic2D.cpp:580
void Draw(Image< unsigned char > &img) const
draws conic into an image using a brute force method
Definition: Conic2D.cpp:221
describes a projective 3D -&gt; 2D mapping in homogenous coordinates
Definition: PMatrix.hh:88
A 3x3 matrix representing a conic (cone/plane intersection)
Definition: Conic2D.hh:71
T Normalize()
divide this by biggest absolute entry, returns biggest entry
Definition: Matrix3x3.cpp:580
void SetIdentity()
set the elements of this matrix to the identity matrix (possibly overriding the inherited method) ...
Definition: Matrix3x3.hh:429