Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TextureTransformEuclidian.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003, 2004 (see file CONTACTS 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 __TextureTransformEuclidian__hh__
26 #define __TextureTransformEuclidian__hh__
27 
28 #include <Base/Common/BIASpragmaStart.hh>
29 #include <Image/TextureTransform.hh>
30 #include <Base/Common/CompareFloatingPoint.hh>
31 
32 namespace BIAS {
33 
34  /** @class TextureTransformEuclidian
35  * @brief analytic properties of Euclidian image warp (rot.+displ.)
36  *
37  * See BIAS::TextureTransform for explanation of methods.
38  * @author koeser 04/2008
39  **/
41  public:
43  P_.newsize(3);
44  P_[0] = 0;
45  P_[1] = 0;
46  P_[2] = 0;
47  }
48 
50 
51  int MapForward(const HomgPoint2D& src, HomgPoint2D& sink) const {
52  const double& sx = src[0];
53  const double& sy = src[1];
54  sink[0] = A_[0][0]*sx + A_[0][1]*sy + tx_;
55  sink[1] = A_[1][0]*sx + A_[1][1]*sy + ty_;
56  sink[2] = 1;
57  return 0;
58  }
59 
60  int MapBackward(const HomgPoint2D& sink, HomgPoint2D& src) const {
61  src[0] = Ainv_[0][0]*sink[0] + Ainv_[0][1]*sink[1] + txinv_;
62  src[1] = Ainv_[1][0]*sink[0] + Ainv_[1][1]*sink[1] + tyinv_;
63  src[2] = 1;
64 #ifdef BIAS_DEBUG
65  HomgPoint2D testsink;
66  MapForward(src, testsink);
67  if (!Equal(testsink[0]+1e-4,sink[0]+1e-4, 1e-4) ||
68  !Equal(testsink[1]+1e-4,sink[1]+1e-4, 1e-4)) {
69  BIASERR(testsink<<" and "<<sink<<" are not equal with P="<<P_
70  <<" and src="<<src<<" and A="<<A_);
71  BIASABORT;
72  }
73 #endif
74  return 0;
75  }
76 
78  Matrix2x2<double>& Jac) const {
79  Jac = A_;
80  return 0;
81  }
82 
84  Matrix2x2<double>& Jac) const {
85  Jac = Ainv_;
86  return 0;
87  }
88 
89  /** @brief local warp is the same at any image position */
90  virtual bool TextureJacobianIsConstant() const { return true;};
91 
93  Jac.newsize(2,3);
94 
95  const double sinphi = sin(P_[0]);
96  const double cosphi = cos(P_[0]);
97  const double x = src[0]-origin_[0];
98  const double y = src[1]-origin_[1];
99 
100  Jac[0][0] = -sinphi*x-cosphi*y;
101  Jac[0][1] = 1;
102  Jac[0][2] = 0;
103 
104  Jac[1][0] = cosphi*x-sinphi*y;
105  Jac[1][1] = 0;
106  Jac[1][2] = 1;
107 
108  return 0;
109  }
110 
112  Jac.newsize(2,3);
113  const double sinphi = sin(P_[0]);
114  const double cosphi = cos(P_[0]);
115 
116  const double c13 = P_[1];
117  const double c23 = P_[2];
118  const double x = src[0]-origin_[0];
119  const double y = src[1]-origin_[1];
120  // dx/dP_
121  Jac[0][0] = -sinphi*x+cosphi*y-cosphi*c23+c13*sinphi;
122  Jac[0][1] = -cosphi;
123  Jac[0][2] = -sinphi;
124 
125  // dy/dP_
126  Jac[1][0] = -cosphi*x-sinphi*y+sinphi*c23+c13*cosphi;
127  Jac[1][1] = sinphi;
128  Jac[1][2] = -cosphi;
129 
130  return 0;
131  }
132 
133  /** @brief the jacobian depends on x */
134  bool ParameterJacobianIsConstant() const { return false;};
135 
136 
137  /** @brief rotation(rad), scale-1, dx,dy. All parameters with respect
138  to origin_! x' = A*(x-origin)+d+origin
139  where A==(s+1)[cos -sin; sin cos]
140  */
141  void SetParameters(const Vector<double>& p) {
142  BIASASSERT(p.Size()==3);
143  P_ = p;
144  A_[0][0] = A_[1][1] = cos(p[0]);
145  A_[0][1] = -sin(p[0]);
146  A_[1][0] = -A_[0][1];
147 
149 
150  tx_ = p[1]-offset[0];
151  ty_ = p[2]-offset[1];
152 
153  Ainv_ = A_.Invert();
154  txinv_ = -(Ainv_[0][0]*tx_ + Ainv_[0][1]*ty_);
155  tyinv_ = -(Ainv_[1][0]*tx_ + Ainv_[1][1]*ty_);
156  }
157 
160  A1[0][0] = A_[0][0];
161  A1[0][1] = A_[0][1];
162  A1[1][0] = A_[1][0];
163  A1[1][1] = A_[1][1];
164 
165  A1[0][2] = tx_;
166  A1[1][2] = ty_;
167 
168  A2[0][0] = A2[1][1] = cos(deltaP[0]);
169  A2[0][1] = -sin(deltaP[0]);
170  A2[1][0] = -A2[0][1];
171 
172 
173  A2[0][2] = deltaP[1]-(A2[0][0]-1.0)*origin_[0]-A2[0][1]*origin_[1];
174  A2[1][2] = deltaP[2]-A2[1][0]*origin_[0]-(A2[1][1]-1.0)*origin_[1];
175 
176  A1 = A2.Invert() * A1;
177 
178  Vector<double> p(3);
179  p[0] = P_[0]-deltaP[0];
180  p[1] = A1[0][2] + (A1[0][0]-1.0)*origin_[0] + A1[0][1]*origin_[1];
181  p[2] = A1[1][2] + A1[1][0]*origin_[0] + (A1[1][1]-1.0)*origin_[1];
182  SetParameters(p);
183  }
184 
185  virtual TextureTransformEuclidian* Clone() const {
186  return new TextureTransformEuclidian(*this);
187  }
188 
190 
191  Jac.newsize(3,3);
192  Jac.SetZero();
193 
194  const double cosine = cos(P_[0]);
195  const double sine = sin(P_[0]);
196 
197  Jac[0][0] = -1.0; // alpha -> -alpha
198 
199  // tx -> -1/sigma * (cos * tx + sin * ty)
200  Jac[1][0] = sine*P_[2]-cosine*P_[2];
201 
202  Jac[1][1] = -cosine;
203  Jac[1][2] = -sine;
204  // ty -> +1/sigma * (sin * tx - cos * ty)
205  Jac[2][0] = cosine*P_[1]+sine*P_[2];
206 
207  Jac[2][1] = sine;
208  Jac[2][2] = -cosine;
209 
210  return 0;
211  }
212 
213  /** @brief origin relative to which rotation and scale is performed */
214  void SetOrigin(const Vector2<double>& origin) {
215  origin_ = origin;
216  SetParameters(P_);
217  }
218 
219  /** @brief origin relative to which rotation and scale is performed */
220  const Vector2<double>& GetOrigin() const {
221  return origin_;
222  }
223 
224  protected:
225  /// cached local warp and inverse
227  /// cached displacement and inverse
228  double tx_, ty_, txinv_, tyinv_;
229  /// origin relative to which rotation and scale is performed
231  };
232 
233 } // end namespace BIAS
234 
235 #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
virtual TextureTransformEuclidian * Clone() const
virtual covariant copy constructor, caller must eventually destroy the created object ...
Vector< double > P_
current set of parameters, see SetParameters for meaning
Matrix< T > & newsize(Subscript M, Subscript N)
Definition: cmat.h:269
void ComposeWithInverseDeltaP(const Vector< double > &deltaP)
concatenate *this and an inverse transform with param deltaP and save new parameter vector to *this...
virtual bool TextureJacobianIsConstant() const
local warp is the same at any image position
class for representing parameterized image warps, such as homography, displacement, ...
void SetOrigin(const Vector2< double > &origin)
origin relative to which rotation and scale is performed
unsigned int Size() const
length of the vector
Definition: Vector.hh:143
Vector< T > & newsize(Subscript N)
Definition: vec.h:220
double tx_
cached displacement and inverse
void SetZero()
Sets all values to zero.
Definition: Matrix.hh:856
int TextureJacobianBackward(const HomgPoint2D &sink, Matrix2x2< double > &Jac) const
shape change of the local region when mapping backward
int ParameterJacobianForward(Matrix< double > &Jac, const HomgPoint2D &src)
transformed position change when parameters change
Matrix2x2< double > A_
cached local warp and inverse
int MapForward(const HomgPoint2D &src, HomgPoint2D &sink) const
map a point in image &quot;source&quot; to a point in image &quot;sink&quot;
bool ParameterJacobianIsConstant() const
the jacobian depends on x
Vector2< double > origin_
origin relative to which rotation and scale is performed
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_...
const Vector2< double > & GetOrigin() const
origin relative to which rotation and scale is performed
int ParameterInversionJacobian(Matrix< double > &Jac) const
compute parameters for inverse operation and obtain the jacobian of the inverse parameters with respe...
void SetParameters(const Vector< double > &p)
rotation(rad), scale-1, dx,dy.
int TextureJacobianForward(const HomgPoint2D &src, Matrix2x2< double > &Jac) const
shape change of the local region when mapping forward
int ParameterJacobianBackward(Matrix< double > &Jac, const HomgPoint2D &src)
transformed position change when parameters change
int MapBackward(const HomgPoint2D &sink, HomgPoint2D &src) const
map a point in image &quot;source&quot; to a point in image &quot;sink&quot;
analytic properties of Euclidian image warp (rot.