Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
LocalAffineFrame.hh
1 #ifndef _LOCALAFFINEFRAME_HH_
2 #define _LOCALAFFINEFRAME_HH_
3 
4 #include <bias_config.h>
5 
6 #ifdef BIAS_HAVE_XML2
7  #include <Base/Common/XMLBase.hh>
8 #endif
9 
10 #include <Base/Math/Vector.hh>
11 #include <Base/Math/Vector2.hh>
12 #include <Base/Geometry/HomgPoint2D.hh>
13 #include <Base/Image/ImageBase.hh>
14 #include <Base/Math/Matrix.hh>
15 #include <Base/Math/Matrix2x2.hh>
16 #include <Base/Common/CompareFloatingPoint.hh>
17 #include <Geometry/HMatrix.hh>
18 
19 // make very small nonzero default uncertainty for gaussian methods
20 #define LAF_DEFAULT_UNCERTAINTY 1e-10
21 
22 namespace BIAS {
23 
24  /** @class LocalAffineFrame
25  @brief affine transformation of 2D image plane which relates image
26  coordinate system and local affine feature coordinate system
27 
28 
29  @author koeser 11/2007
30  */
31 
32 
33  class BIASGeometry_EXPORT LocalAffineFrame
34 #ifdef BIAS_HAVE_XML2
35  : public BIAS::XMLBase
36 #endif
37  {
38  public:
39 
40  inline LocalAffineFrame(): t_(0,0), A_(BIAS::MatrixIdentity),
41  Cov_(6,6,BIAS::MatrixZero) {
42  for (unsigned int i=0; i<6; i++) Cov_[i][i] = LAF_DEFAULT_UNCERTAINTY;
43  };
44 
45  inline LocalAffineFrame(const LocalAffineFrame &FP) {
46  (*this) = FP;
47  };
48 
50  t_ = FP.t_;
51  A_ = FP.A_;
52  Cov_ = FP.Cov_;
53  return (*this);
54  }
55 
56  /** @brief tranform a point in the image coordinate system into the feature
57  coordinate system */
59  BIAS::Vector2<double> xeu(x[0]/x[2]-t_[0], x[1]/x[2]-t_[1]);
60  return BIAS::HomgPoint2D(A_.Invert() * xeu);
61  }
62 
63  /** @brief tranform a point in the feature coordinate system into the
64  iamge coordinate system */
66  BIAS::Vector2<double> xeu(x[0]/x[2], x[1]/x[2]);
67  return BIAS::HomgPoint2D(A_ * xeu + t_);
68  }
69 
70  /** @brief find closest scaled rotation matrix to linear A (under
71  Frobenius norm) and return */
72  BIAS::LocalAffineFrame GetLocalSimilarityFrame() const;
73 
74  /** @brief return a 6-vector (a11,a12,a21,a22,tx,ty) */
77  const double *pd = A_.GetData();
78  a[0] = *pd++;
79  a[1] = *pd++;
80  a[2] = *pd++;
81  a[3] = *pd++;
82  a[4] = t_[0];
83  a[5] = t_[1];
84  return a;
85  }
86 
87  /** @brief set from a 6-vector (a11,a12,a21,a22,tx,ty), default cov */
88  void SetFromVector(const BIAS::Vector<double>& affineparams) {
89  SetFromVector(affineparams,
91  SetDefaultCov();
92  }
93 
94  /** @brief set from 6-vector (a11,a12,a21,a22,tx,ty) with cov */
95  void SetFromVector(const BIAS::Vector<double>& affineparams,
96  const BIAS::Matrix<double>& cov) {
97  Cov_ = cov;
98  double *pd = A_.GetData();
99  *pd++ = affineparams[0];
100  *pd++ = affineparams[1];
101  *pd++ = affineparams[2];
102  *pd++ = affineparams[3];
103  t_[0] = affineparams[4];
104  t_[1] = affineparams[5];
105  }
106 
107  /** @brief return 6x6 covariance */
108  const BIAS::Matrix<double>& GetCov() const {
109  return Cov_;
110  }
111 
112  /** @brief returns matrix which transforms a point in feature coordinates
113  into a point in image coordinates (LocalToGlobal) */
116  M[0][0] = A_[0][0];
117  M[0][1] = A_[0][1];
118  M[1][0] = A_[1][0];
119  M[1][1] = A_[1][1];
120  M[0][2] = t_[0];
121  M[1][2] = t_[1];
122  M[2][2] = 1;
123  return M;
124  }
125 
126  /** @brief returns matrix which transforms a point in image coordinates
127  into a point in feature coordinates (GlobalToLocal) */
131  M[0][0] = A[0][0];
132  M[0][1] = A[0][1];
133  M[1][0] = A[1][0];
134  M[1][1] = A[1][1];
135  BIAS::Vector2<double> offset = A*t_;
136  M[0][2] = -offset[0];
137  M[1][2] = -offset[1];
138  M[2][2] = 1;
139  return M;
140  }
141 
142  /** @brief construct from homography-style affine matrix (last row 0,0,1)*/
143  void SetFromMatrix(const BIAS::Matrix<double>& affineTransform) {
144  BIASASSERT(affineTransform.num_rows()==3);
145  BIASASSERT(affineTransform.num_cols()==3);
146  BIASASSERT(fabs(affineTransform[2][0])<1e-8);
147  BIASASSERT(fabs(affineTransform[2][1])<1e-8);
148  BIASASSERT(BIAS::Equal(affineTransform[2][2],1.0));
149  A_[0][0] = affineTransform[0][0];
150  A_[0][1] = affineTransform[0][1];
151  A_[1][0] = affineTransform[1][0];
152  A_[1][1] = affineTransform[1][1];
153  t_[0] = affineTransform[0][2];
154  t_[1] = affineTransform[1][2];
155  SetDefaultCov();
156  }
157 
158  /** @brief sets default nonzero cov */
159  inline void SetDefaultCov() {
160  Cov_.SetZero();
161  for (unsigned int i=0; i<6; i++) Cov_[i][i] = LAF_DEFAULT_UNCERTAINTY;
162  }
163 
164  /** @brief return affine matrix */
165  const BIAS::Matrix2x2<double>& GetA() const { return A_; }
166 
167  /** @brief return offset */
168  const BIAS::Vector2<double>& GetT() const { return t_; }
169 
170  /** @brief return affine matrix */
172  const BIAS::Matrix<double>& cov =
174  A_ = A;
175 
176  // erase correlations between offset and old affine params and copy cov
177  for (unsigned int x=0; x<4; x++) {
178  Cov_[x][4] = 0;
179  Cov_[x][5] = 0;
180  Cov_[4][x] = 0;
181  Cov_[5][x] = 0;
182  for (unsigned int y=x; y<4; y++) {
183  Cov_[y][x] = Cov_[x][y] = 0.5*(cov[y][x]+cov[x][y]);
184  }
185  }
186  }
187 
188  /** @brief return offset */
189  void SetT(const BIAS::Vector2<double>& T,
190  const BIAS::Matrix<double>& cov =
192  t_ = T;
193  // erase correlations between old offset and affine params
194  for (unsigned int x=0; x<4; x++) {
195  Cov_[x][4] = 0;
196  Cov_[x][5] = 0;
197  Cov_[4][x] = 0;
198  Cov_[5][x] = 0;
199  }
200  Cov_[4][4] = cov[0][0];
201  Cov_[4][5] = Cov_[5][4] = 0.5*(cov[1][0] + cov[0][1]);
202  Cov_[5][5] = cov[1][1];
203  }
204 
205  /** @brief get global transform to transfer an image point of 1 into the
206  image system of 2, e.g. given two affine features, get affine transform
207  between images for KLT tracker
208  @brief setDefaultCov set digonal small cov or use error propagation
209  from uncertainties of base transformations */
210  LocalAffineFrame GetRelativeTransform(const LocalAffineFrame& F2,
211  bool setDefaultCov = false) const;
212 
213  /** @brief use linear propagation to transform the local affine frame e.g.
214  into another image
215 
216  Definition of homography direction:
217  (*this) has "position" t_. The result will have position H*t_
218 
219  */
220  LocalAffineFrame GetHomographyTransformed(const BIAS::HMatrix& H) const;
221 
222 
223  /** @brief draw a square at the feature's ori position
224  @author koeser */
225  void Draw(BIAS::Image<unsigned char>& targetImage,
226  const double& positionCovScale = 1.0,
227  const unsigned int linethickness=1) const;
228 
229  /** @brief returns fraction of square area [-1;1][-1;1], which is in the
230  image (slow numeric sampling) */
231  double FractionInImage(unsigned int width, unsigned int height,
232  unsigned int x0=0, unsigned int y0=0) const {
233  int count = 0;
234  for (int x=-5; x<=5; x++) {
235  for (int y=-5; y<=5; y++) {
236  HomgPoint2D l(x,y,5);
237  l.Homogenize();
238  l = LocalToGlobal(l);
239  l.Homogenize();
240  if ((l[0]>x0) && (l[1]>y0) && (l[0]<x0+width) && (l[1]<y0+height))
241  count++;
242  }
243  }
244  return double(count)/121.0;
245  }
246 
247 #ifdef BIAS_HAVE_XML2
248  /** @brief specialization of XML block name function */
249  virtual int XMLGetClassName(std::string& TopLevelTag,
250  double& Version) const;
251 
252  /** @brief specialization of XML write function */
253  virtual int XMLOut(const xmlNodePtr Node, BIAS::XMLIO& XMLObject) const;
254 
255  /** @brief specialization of XML read function */
256  virtual int XMLIn(const xmlNodePtr Node, BIAS::XMLIO& XMLObject);
257 #endif
258 
259  protected:
260  /// original image position
262 
263  /// "orientation + area(detA) of feature"
264  /// affine matrix which transforms all pixels of the unit feature (or
265  /// imagine the local feature coordinate system) of one-pixel square
266  /// into the shape of the current feature(into the image coordinate system)
267  /// e.g. y = (1;0) is the 3 o'clock ray to the feature border
268  /// x_i = A_ * x_f + t_;
270 
271  /// 6x6 covariance matrix of the 6 region parameters(a11,a12,a21,a22,tx,ty)
273 
274  friend BIASGeometry_EXPORT std::ostream& operator<<(std::ostream& os,
275  const LocalAffineFrame& fp);
276 
277  friend BIASGeometry_EXPORT std::istream& operator>>(std::istream& is,
278  LocalAffineFrame& fp);
279 
280  }; // end class LocalAffineFrame
281 
282  /** @relates LocalAffineFrame */
283  BIASGeometry_EXPORT std::ostream& operator<<(std::ostream& os,
284  const LocalAffineFrame& fp);
285 
286  /** @relates LocalAffineFrame */
287  BIASGeometry_EXPORT std::istream& operator>>(std::istream& is,
288  LocalAffineFrame& fp);
289 
290 } // end namespace
291 
292 
293 #endif
int Invert(Matrix2x2< T > &result) const
analyticaly inverts matrix
Definition: Matrix2x2.cpp:115
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
BIAS::Vector2< double > t_
original image position
const LocalAffineFrame & operator=(const LocalAffineFrame &FP)
Subscript num_cols() const
Definition: cmat.h:320
a 3x3 Matrix describing projective transformations between planes
Definition: HMatrix.hh:39
affine transformation of 2D image plane which relates image coordinate system and local affine featur...
const BIAS::Matrix< double > & GetCov() const
return 6x6 covariance
HomgPoint2D & Homogenize()
homogenize class data member elements to W==1 by divison by W
Definition: HomgPoint2D.hh:215
void SetA(const BIAS::Matrix2x2< double > &A, const BIAS::Matrix< double > &cov=BIAS::Matrix< double >(4, 4, BIAS::MatrixZero))
return affine matrix
BIAS::HomgPoint2D GlobalToLocal(const BIAS::HomgPoint2D &x) const
tranform a point in the image coordinate system into the feature coordinate system ...
void SetDefaultCov()
sets default nonzero cov
BIAS::HomgPoint2D LocalToGlobal(const BIAS::HomgPoint2D &x) const
tranform a point in the feature coordinate system into the iamge coordinate system ...
BIAS::Matrix2x2< double > A_
&quot;orientation + area(detA) of feature&quot; affine matrix which transforms all pixels of the unit feature (...
Wrapper class for reading and writing XML files based on the XML library libxml2. ...
Definition: XMLIO.hh:72
void SetFromVector(const BIAS::Vector< double > &affineparams)
set from a 6-vector (a11,a12,a21,a22,tx,ty), default cov
void SetFromMatrix(const BIAS::Matrix< double > &affineTransform)
construct from homography-style affine matrix (last row 0,0,1)
LocalAffineFrame(const LocalAffineFrame &FP)
const BIAS::Matrix2x2< double > & GetA() const
return affine matrix
void SetFromVector(const BIAS::Vector< double > &affineparams, const BIAS::Matrix< double > &cov)
set from 6-vector (a11,a12,a21,a22,tx,ty) with cov
void SetT(const BIAS::Vector2< double > &T, const BIAS::Matrix< double > &cov=BIAS::Matrix< double >(2, 2, BIAS::MatrixZero))
return offset
T * GetData()
get the pointer to the data array of the matrix (for faster direct memeory access) ...
Definition: Matrix.hh:185
Base class with interface for xml output.
Definition: XMLBase.hh:56
std::ostream & operator<<(std::ostream &os, const Array2D< T > &arg)
Definition: Array2D.hh:260
bool Equal(const T left, const T right, const T eps)
comparison function for floating point values See http://www.boost.org/libs/test/doc/components/test_...
BIAS::Matrix< double > GetAsMatrix() const
returns matrix which transforms a point in feature coordinates into a point in image coordinates (Loc...
BIAS::Vector< double > GetAsVector() const
return a 6-vector (a11,a12,a21,a22,tx,ty)
double FractionInImage(unsigned int width, unsigned int height, unsigned int x0=0, unsigned int y0=0) const
returns fraction of square area [-1;1][-1;1], which is in the image (slow numeric sampling) ...
BIAS::Matrix< double > Cov_
6x6 covariance matrix of the 6 region parameters(a11,a12,a21,a22,tx,ty)
Subscript num_rows() const
Definition: cmat.h:319
BIAS::Matrix< double > GetAsInverseMatrix() const
returns matrix which transforms a point in image coordinates into a point in feature coordinates (Glo...
const BIAS::Vector2< double > & GetT() const
return offset
class BIASGeometryBase_EXPORT HomgPoint2D
BIASCommon_EXPORT std::istream & operator>>(std::istream &is, BIAS::TimeStamp &ts)
Standard input operator for TimeStamps.
Definition: TimeStamp.cpp:157