Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
RectificationViaProjectionMappingBase.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 #include <Base/Common/BIASpragmaStart.hh>
26 
27 #ifndef __RectificationViaProjectionMappingBase_hh__
28 #define __RectificationViaProjectionMappingBase_hh__
29 
30 #include <Image/RectificationBase.hh>
31 #include <Image/BackwardMapping.hh>
32 #include <Image/ProjectionMapping.hh>
33 
34 namespace BIAS
35 {
36 
37  /** Base class for rectification implementations that make use of
38  * projections to represent rectified state and where the projection-centers
39  * in rectified and original state are the same.
40  * Implementation of child classes only have to supply the rectified params
41  * calculation. Aftwerwards they can make use of the ProjectionMapping
42  * facilities that are implemented in this class to rectify images and
43  * evaluate disparity maps into depth maps. Be warned: ProjectionMapping
44  * is a very expansive implementation, yet takles nonlinearities in
45  * coordinate transformation (i.e. distortions).
46  *
47  * \ingroup g_filter
48  * \author bartczak 11/2006
49  */
50  template<class InputStorageType, class OutputStorageType>
52  InputStorageType, OutputStorageType>
53  {
54  public:
55 
56  virtual
58  RectificationViaProjectionMappingBase(const std::string& stringID,
59  const double& Scale = 1.0);
60 
61  /** Scale factor for isotropically scaling the rectified images (and rectified intrinsic parameters).
62  * \attention If targetWidth_ is greater zero the scale factor set using this method is ignored, see
63  * SetTargetWidth().
64  **/
65  inline void
66  SetScale(const double& scale)
67  {
68  scale_ = scale;
69  rectificationParamsAreValid_ = false;
70  }
71 
72  virtual int
73  SetCameraA(BIAS::Camera<InputStorageType>& cam);
74  virtual int
75  SetCameraA(const BIAS::Image<InputStorageType>& img,
76  const BIAS::ProjectionParametersBase* proj);
77  virtual int
78  SetCameraB(BIAS::Camera<InputStorageType>& cam);
79  virtual int
80  SetCameraB(const BIAS::Image<InputStorageType>& img,
81  const BIAS::ProjectionParametersBase* proj);
82 
83  virtual int
84  Rectify();
85 
86  virtual int
87  Disp2Depth(const Image<float>& DisparityMap, Image<float>& DepthMap,
88  unsigned int border);
89 
90  // frick
91  static int
92  Disp2Depth(const Image<float>& DisparityMap, Image<float>& DepthMap,
93  const ProjectionParametersBase* origCamPA,
94  const ProjectionParametersBase* origCamPB,
95  const ProjectionParametersBase* rectCamPA,
96  const ProjectionParametersBase* rectCamPB, unsigned int border);
97 
98  /** Tries to calculate the rectification parameters and returns clones
99  * of internal members. If just one rectification parameter could not
100  * be determined the returned pointer will be NULL and the return value is
101  * less 0.
102  */
103  virtual int
104  GetClonesOfRectificationParameters(ProjectionParametersBase*& rectPPA,
105  ProjectionParametersBase*& rectPPB);
106 
107  /** Determines the type of interpolation used when rectifying.
108  * Types are taken from BackwardMapping.hh
109  */
110  void
111  SetInterpolationMethod(InterpolationMethod interpolationType);
112 
113  /** fill color for second image (first is black) */
114  void
115  SetSecondFill(OutputStorageType d)
116  {
117  secondFill_ = d;
118  }
119 
120  /** \name Rectification Setup I/O */
121  /** @{ */
122  /** Method returns the current rectification setup.
123  * The Camera class is used as a carrier for the camera parameters
124  * stored in the meta data under a single projection.
125  * The image data part is containing the BWM-Look-Up-Tables.
126  * Each image row contains a single BWM-LUT.
127  * The ascii meta data field will contain the StringID of the
128  * rectification, which can be quaried using GetStringID().
129  **/
130  int
131  GetRectificationSetup(Camera<float>& rectSetup);
132 
133  /** Loads rectification setup from Camera data.
134  * \returns -1 if ID stored in the image does not fit the class id.
135  * \attention if Set*() is called with parameters not fitting the loaded
136  * setup, a reinitalisation of the mapping structures is performed
137  * (like in scenarios where the setup was not loaded!).
138  **/
139  int
140  SetRectificationSetup(Camera<float>& rectSetup);
141 
142  /** Returns the displacement maps rather then the rectified images,
143  * Suitable for texture look ups for multiple images of same
144  * configuration.
145  */
146  int
147  GetRectifyingDisplacementMaps(Image<float>& displacementA,
148  Image<float>& displacementB);
149 
150  /** Returns the string id that identifies the rctification classes in
151  * this hierarchy. Suitable for factories from information stored in
152  * Setup files.
153  */
154  const std::string&
155  GetStringID() const
156  {
157  return stringID_;
158  }
159  /** @} */
160 
161  /** Set the override angles, which are individually chosen for the
162  * internal parameters, if the respective override leads to
163  * a smaller fov of the rectified cameras, then the automatically derived
164  * fov. The angles are defined in the rectified coordinate frame of the
165  * first camera and have the same defintion as the angle coordinates used
166  * in ProjectionParametersGreatCircle.
167  **/
168  void
169  SetOverrideAngles(double thetaMinOverride, double thetaMaxOverride,
170  double phiMinOverride, double phiMaxOverride);
171 
172  /** Similar to SetScale but uses the argument width to determine scale
173  * factor.
174  * Method sets the value of targetWidth_ which defaults to zero.
175  * If targetWidth_ is set greater then zero an isotropic scale factor
176  * for the calculated rectified intrinsic parameters is derived so that
177  * both rectified images will have this width.
178  *
179  * \attention If targetWidth_ is greater zero the scale factor set using
180  * SetScale is ignored.
181  *
182  **/
183  void
184  SetTargetWidth(unsigned int targetWidth);
185 
186  /** Method allows to turn on or off the use of look up tables.
187  * Per default use of look up tables is turned on. If it is turned off
188  * loading rectification setups will turn it on!
189  */
190  void
191  UseLookUpTables(bool use);
192 
193  protected:
194  /** \name Rectification Setup I/O */
195  /** @{ */
196  /** Descriptive identifier of the classes in the hierarchy **/
197  std::string stringID_;
198  /** @} */
199 
200  /** Calculating the projection parameters for specific rectification.
201  * Is called by Rectify implementation. Must return 0 upon success, every
202  * other code is handled as failure.
203  */
204  virtual int
205  DetermineRectificationParameters_() = 0;
206 
207  /** this computes ideal parameters and applies the scale */
208  int
209  DetermineScaledRectificationParameters_();
210 
211  /** Projection parameters of the rectified image A */
213 
214  /** Projection parameters of the rectified image B */
216 
217  /// scale computed rectification resolution by this amount
218  double scale_;
219 
220  /** if this value is greater zero the scale the computed rectification
221  * parameters are isotropically scaled by a scale factor so that the image width of
222  * both rectified images matches this value. Its value is set using SetTargetWidth().
223  **/
224  unsigned int targetWidth_;
225  protected:
226  /** Checks for rectificationParamsAreValid_ if not true will
227  * try to succsessfully process DetermineRectificationParameters_().
228  * If this is the case it will set rectificationParamsAreValid_ to true
229  * and return this value. Otherwise will return false.
230  */
231  bool
232  AreRectificationParametersValid_();
233 
234  /** Flag is true when DetermineRectificationParameters_() was succesfull
235  * and no setter was called afterwards.
236  */
238 
240  OutputStorageType secondFill_;
241 
242  /** Classes holding rectification parameters and performing mapping **/
245 
246  /** Tells whether the lut structure and the result images have been initialized.
247  * This flag is set to true by method InitializeMapping_(). It is false after
248  * object creation and is set false when rectificationParamsAreValid_ flag changes
249  * from false to true, which is the case in AreRectificationParametersValid_().
250  **/
252 
253  /** \name Initialization.
254  * The general rectification procedure runs as follows:
255  * -# Receive input data: image and cam-params.
256  * -# Determine the rectification parameters, influenced by scale factors.
257  * -# Initialize the two projection mapping objects that perform the
258  * Mapping (InitializeMapping_()).
259  *
260  * The rectification setup can be stored and loaded. The full
261  * initialization procedure is not necessary if the BWM-LUTs are loaded.
262  * In this case it suffices to initialize the target images
263  * (InitializeSinkImages_()).
264  * If informations about the mapping process have to be aquired without
265  * rectifciation, it can be of use to initialize the objects performing
266  * the mapping. This can be done using
267  * InitializeProjectionMappingObjects_().
268  *
269  * \attention This initializers do not order a rectification parameter
270  * calculation via AreRectificationParametersValid_().
271  */
272  /** @{ */
273  /** Initializes lut structure and the result images.**/
274  void
275  InitializeMapping_();
276  void
277  InitializeSinkImages_();
278  void
279  InitializeProjectionMappingObjects_();
280  /** @}*/
281 
282  /** Value used to override thetaMin result of automatic fov
283  * calculation if thetaMinOverride_ is larger.
284  **/
286 
287  /** Value used to override thetaMax result of automatic fov
288  * calculation if thetaMaxOverride_ is smaller.
289  **/
291 
292  /** Value used to override phiMin result of automatic fov
293  * calculation if phiMinOverride_ is larger.
294  **/
296 
297  /** Value used to override phiMax result of automatic fov
298  * calculation if phiMaxOverride_ is smaller.
299  **/
301 
302  /** Toggles the use of lookup tables.
303  */
305  };
306 
307 }
308 
309 #endif
310 
311 #include <Base/Common/BIASpragmaEnd.hh>
InterpolationMethod
accuracy for resampling
Base class for rectification implementations that make use of projections to represent rectified stat...
double phiMinOverride_
Value used to override phiMin result of automatic fov calculation if phiMinOverride_ is larger...
double scale_
scale computed rectification resolution by this amount
double thetaMaxOverride_
Value used to override thetaMax result of automatic fov calculation if thetaMaxOverride_ is smaller...
unsigned int targetWidth_
if this value is greater zero the scale the computed rectification parameters are isotropically scale...
Maps source pixel to sink pixel of given projections.
base class for rectification implementations and wrappers
std::string stringID_
Descriptive identifier of the classes in the hierarchy.
ProjectionParametersBase * rectPPA_
Projection parameters of the rectified image A.
const std::string & GetStringID() const
Returns the string id that identifies the rctification classes in this hierarchy. ...
void SetScale(const double &scale)
Scale factor for isotropically scaling the rectified images (and rectified intrinsic parameters)...
bool rectificationParamsAreValid_
Flag is true when DetermineRectificationParameters_() was succesfull and no setter was called afterwa...
void SetSecondFill(OutputStorageType d)
fill color for second image (first is black)
bool mappingInitialized_
Tells whether the lut structure and the result images have been initialized.
double phiMaxOverride_
Value used to override phiMax result of automatic fov calculation if phiMaxOverride_ is smaller...
ProjectionMapping< InputStorageType, OutputStorageType > rectMapperA_
Classes holding rectification parameters and performing mapping.
Camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
ProjectionMapping< InputStorageType, OutputStorageType > rectMapperB_
ProjectionParametersBase * rectPPB_
Projection parameters of the rectified image B.
double thetaMinOverride_
Value used to override thetaMin result of automatic fov calculation if thetaMinOverride_ is larger...