Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
HomographyMapping.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 #include "HomographyMapping.hh"
27 #include <Geometry/RMatrix.hh>
28 #include <Base/Image/ImageConvert.hh>
29 #include <Base/Geometry/HomgPoint2D.hh>
30 
31 using namespace BIAS;
32 
33 template <class InputStorageType, class OutputStorageType>
36 {}
37 
38 
39 template <class InputStorageType, class OutputStorageType>
42 : H_(MatrixIdentity)
43 {}
44 
45 
46 template <class InputStorageType, class OutputStorageType>
49 {
50  (*this) = src;
51 }
52 
53 
54 template <class InputStorageType, class OutputStorageType>
58 {
59  H_ = src.H_;
60  return *this;
61 }
62 
63 
64 template <class InputStorageType, class OutputStorageType>
67  HomgPoint2D& source) const {
68  // compute coordinates in source image with backward hoography
69  H_.Mult(sink, source);
70  source.Homogenize();
71  return 0;
72 }
73 
74 template <class InputStorageType, class OutputStorageType>
76 GetJacobian_(const HomgPoint2D& sink, Matrix2x2<double>& Jacobian) const {
77  // analytic jacobian of homography
78  H_.GetJacobian(sink, Jacobian);
79  return 0;
80 }
81 
82 
83 using namespace std;
84 
85 template <class InputStorageType, class OutputStorageType>
87 GetBoundingBox(unsigned int /* sourcewidth */, unsigned int /* sourceheight */,
88  unsigned int sinkwidth, unsigned int sinkheight,
89  int &tlx, int &tly,
90  int &brx, int &bry) {
91 
92 
93  /**
94  // DISABLED: this code does only work for very special homographies, e.g.
95  // affine transformations! It breaks if infinty and wraparound effects
96  // come into play.
97 
98 
99  double dtlx,dtly, dbrx,dbry;
100 
101  // do a forward mapping of the corners of the source image and compute
102  // convex hull rectangle
103 
104  HomgPoint2D tl(0,0,1), br(sourcewidth, sourceheight,1), bbTL, bbBR;
105  HomgPoint2D bl(0,sourceheight,1), tr(sourcewidth,0,1), bbBL, bbTR;
106 
107  HMatrix HI=H_.Invert();
108  bbTL= HI*tl; bbTL.Homogenize();
109  bbBR= HI*br; bbBR.Homogenize();
110  bbBL= HI*bl; bbBL.Homogenize();
111  bbTR= HI*tr; bbTR.Homogenize();
112 
113  cout <<"source image corners map to "<<bbTL<<" "<<bbBR<<" "<<bbBL<<" "<<
114  bbTR<<endl;
115 
116  // min(x) and min(y)
117  dtlx=(bbTL[0]);
118  if (bbBR[0]<dtlx) dtlx=(bbBR[0]);
119  if (bbBL[0]<dtlx) dtlx=(bbBL[0]);
120  if (bbTR[0]<dtlx) dtlx=(bbTR[0]);
121 
122  dtly=(bbTL[1]);
123  if (bbBR[1]<dtly) dtly=(bbBR[1]);
124  if (bbBL[1]<dtly) dtly=(bbBL[1]);
125  if (bbTR[1]<dtly) dtly=(bbTR[1]);
126 
127  // max(x) and max(y)
128  dbrx=(bbTL[0]);
129  if (bbBR[0]>dbrx) dbrx=(bbBR[0]);
130  if (bbBL[0]>dbrx) dbrx=(bbBL[0]);
131  if (bbTR[0]>dbrx) dbrx=(bbTR[0]);
132 
133  dbry=(bbTL[1]);
134  if (bbBR[1]>dbry) dbry=(bbBR[1]);
135  if (bbBL[1]>dbry) dbry=(bbBL[1]);
136  if (bbTR[1]>dbry) dbry=(bbTR[1]);
137 
138  if (dtlx >= double(INT_MAX)) dtlx = INT_MAX;
139  if (dtly >= double(INT_MAX)) dtly = INT_MAX;
140 
141  // br is outside the boundinx box
142  if (dbrx>double(INT_MAX)) dbrx = INT_MAX;
143 
144  if (dbry>double(INT_MAX)) dbry = INT_MAX;
145 
146  tlx = ( int)dtlx;
147  tly = ( int)dtly;
148  brx = ( int)ceil(dbrx);
149  bry = ( int)ceil(dbry);
150 
151  cout<<"bounding box is "<<tlx<<" "<<tly<<" "<<brx<<" "<<bry<<" "<<endl;
152 
153 
154  */
155 
156  tlx = ( int)0;
157  tly = ( int)0;
158  brx = ( int)sinkwidth-1;
159  bry = ( int)sinkheight-1;
160 
161 
162  return 0;
163 }
164 
165 template <class InputStorageType, class OutputStorageType>
168 {
169  HMatrix HTrans;
170  HTrans.SetIdentity();
171  HTrans[0][2]=offsetX_;
172  HTrans[1][2]=offsetY_;
173  return H_ * HTrans;
174 }
175 
176 namespace BIAS{
177 // TODO FIXME win32 template instantiation problem (JW)
179 template class HomographyMapping<float, float>;
182 
183 #if defined(BUILD_IMAGE_CHAR)
184 // TODO FIXME win32 template instantiation problem (JW)
186 template class HomographyMapping<char, char>;
187 #endif
188 
189 #if defined(BUILD_IMAGE_USHORT)
191 #endif
192 
193 #if defined(BUILD_IMAGE_SHORT)
194 template class HomographyMapping<short, short>;
195 #endif
196 
197 #if defined(BUILD_IMAGE_SHORT)&&defined(BUILD_IMAGE_USHORT)
199 #endif
200 
201 #if defined(BUILD_IMAGE_INT)
202 template class HomographyMapping<int,int>;
203 #endif
204 
205 #if defined(BUILD_IMAGE_USHORT)
207 #endif
208 
209 #if defined(BUILD_IMAGE_USHORT) && defined(BUILD_IMAGE_INT)
211 #endif
212 
213 #if defined(BUILD_IMAGE_DOUBLE)
214 template class HomographyMapping<double,double>;
215 #endif
216 }
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
virtual int GetSourceCoordinates_(const HomgPoint2D &sink, HomgPoint2D &source) const
reimplementation for homography, takes sink and computes coords in source
a 3x3 Matrix describing projective transformations between planes
Definition: HMatrix.hh:39
Maps image src to image sink with homography H (software implementation)
HomgPoint2D & Homogenize()
homogenize class data member elements to W==1 by divison by W
Definition: HomgPoint2D.hh:215
virtual int GetBoundingBox(unsigned int srcwidth, unsigned int srcheight, unsigned int sinkwidth, unsigned int sinkheight, int &tlx, int &tly, int &brx, int &bry)
calculates the bounding box in sink image, where to do the backward mapping.
HomographyMapping< InputStorageType, OutputStorageType > & operator=(const HomographyMapping &src)
required because of const members
HMatrix H_
the homography for backward mapping: run over sink and compute source = H_ * sink ...
virtual int GetJacobian_(const HomgPoint2D &sink, Matrix2x2< double > &Jacobian) const
analytic jacobian
HMatrix GetChangedHomography() const
if Map() is called with newDist=true, the real applied Homography is changed
void SetIdentity()
set the elements of this matrix to the identity matrix (possibly overriding the inherited method) ...
Definition: Matrix3x3.hh:429