SLAMflex SE  0.1.0
SLAMflex provides detection and tracking of dominant planes for smartphone devices. This plane can then be used to show AR content relative to the plane orientation. The detection of plane is performed in the field of view of the smartphone camera. In subsequent frames it is tracked. The interface returns the plane position and orientation.
ATANCamera.h
Go to the documentation of this file.
1 // *-* c++ *-*
2 // Copyright 2008 Isis Innovation Limited
3 
4 // N-th implementation of a camera model
5 // GK 2007
6 // Evolved a half dozen times from the CVD-like model I was given by
7 // TWD in 2000
8 //
9 // This one uses the ``FOV'' distortion model of
10 // Deverneay and Faugeras, Straight lines have to be straight, 2001
11 //
12 // BEWARE: This camera model caches intermediate results in member variables
13 // Some functions therefore depend on being called in order: i.e.
14 // GetProjectionDerivs() uses data stored from the last Project() or UnProject()
15 // THIS MEANS YOU MUST BE CAREFUL WITH MULTIPLE THREADS
16 // Best bet is to give each thread its own version of the camera!
17 //
18 // Camera parameters are stored in a GVar, but changing the gvar has no effect
19 // until the next call to RefreshParams() or SetImageSize().
20 //
21 // Pixel conventions are as follows:
22 // For Project() and Unproject(),
23 // round pixel values - i.e. (0.0, 0.0) - refer to pixel centers
24 // I.e. the top left pixel in the image covers is centered on (0,0)
25 // and covers the area (-.5, -.5) to (.5, .5)
26 //
27 // Be aware that this is not the same as what opengl uses but makes sense
28 // for acessing pixels using ImageRef, especially ir_rounded.
29 //
30 // What is the UFB?
31 // This is for projecting the visible image area
32 // to a unit square coordinate system, with the top-left at 0,0,
33 // and the bottom-right at 1,1
34 // This is useful for rendering into textures! The top-left pixel is NOT
35 // centered at 0,0, rather the top-left corner of the top-left pixel is at
36 // 0,0!!! This is the way OpenGL thinks of pixel coords.
37 // There's the Linear and the Distorting version -
38 // For the linear version, can use
39 // glMatrixMode(GL_PROJECTION); glLoadIdentity();
40 // glMultMatrix(Camera.MakeUFBLinearFrustumMatrix(near,far));
41 // To render un-distorted geometry with full frame coverage.
42 //
43 
44 #ifndef __ATAN_CAMERA_H
45 #define __ATAN_CAMERA_H
46 
47 #include "TooN.h"
48 #include <cmath>
49 using namespace TooN;
50 #include "vector_image_ref.h"
51 #include "globals.h"
52 
53 
54 
55 
56 class CameraCalibrator;
57 class CalibImage;
58 
59 // The parameters are:
60 // 0 - normalized x focal length
61 // 1 - normalized y focal length
62 // 2 - normalized x offset
63 // 3 - normalized y offset
64 // 4 - w (distortion parameter)
65 
66 class ATANCamera {
67  public:
68  ATANCamera(std::string sName);
69 
70  // Image size get/set: updates the internal projection params to that target image size.
71  void SetImageSize(Vector<2> v2ImageSize);
72  inline void SetImageSize(CVD::ImageRef irImageSize) {SetImageSize(vec(irImageSize));};
73  inline Vector<2> GetImageSize() {return mvImageSize;};
74  void RefreshParams();
75 
76  // Various projection functions
77  Vector<2> Project(const Vector<2>& camframe); // Projects from camera z=1 plane to pixel coordinates, with radial distortion
78  inline Vector<2> Project(CVD::ImageRef ir) { return Project(vec(ir)); }
79  Vector<2> UnProject(const Vector<2>& imframe); // Inverse operation
80  inline Vector<2> UnProject(CVD::ImageRef ir) { return UnProject(vec(ir)); }
81 
82  Vector<2> UFBProject(const Vector<2>& camframe);
83  Vector<2> UFBUnProject(const Vector<2>& camframe);
84  inline Vector<2> UFBLinearProject(const Vector<2>& camframe);
85  inline Vector<2> UFBLinearUnProject(const Vector<2>& fbframe);
86 
87  Matrix<2,2> GetProjectionDerivs(); // Projection jacobian
88 
89  inline bool Invalid() { return mbInvalid;}
90  inline double LargestRadiusInImage() { return mdLargestRadius; }
91  inline double OnePixelDist() { return mdOnePixelDist; }
92 
93  // The z=1 plane bounding box of what the camera can see
94  inline Vector<2> ImplaneTL();
95  inline Vector<2> ImplaneBR();
96 
97  // OpenGL helper function
98  Matrix<4> MakeUFBLinearFrustumMatrix(double near, double far);
99 
100  // Feedback for Camera Calibrator
101  double PixelAspectRatio() { return mvFocal[1] / mvFocal[0];}
102 
103 
104  // Useful for gvar-related reasons (in case some external func tries to read the camera params gvar, and needs some defaults.)
106 
107  protected:
108  Vector<NUMTRACKERCAMPARAMETERS> mgvvCameraParams; // The actual camera parameters
109 
110  Matrix<2, NUMTRACKERCAMPARAMETERS> GetCameraParameterDerivs();
111  void UpdateParams(Vector<NUMTRACKERCAMPARAMETERS> vUpdate);
112  void DisableRadialDistortion();
113 
114  // Cached from the last project/unproject:
115  Vector<2> mvLastCam; // Last z=1 coord
116  Vector<2> mvLastIm; // Last image/UFB coord
117  Vector<2> mvLastDistCam; // Last distorted z=1 coord
118  double mdLastR; // Last z=1 radius
119  double mdLastDistR; // Last z=1 distorted radius
120  double mdLastFactor; // Last ratio of z=1 radii
121  bool mbInvalid; // Was the last projection invalid?
122 
123  // Cached from last RefreshParams:
124  double mdLargestRadius; // Largest R in the image
125  double mdMaxR; // Largest R for which we consider projection valid
126  double mdOnePixelDist; // z=1 distance covered by a single pixel offset (a rough estimate!)
127  double md2Tan; // distortion model coeff
128  double mdOneOver2Tan; // distortion model coeff
129  double mdW; // distortion model coeff
130  double mdWinv; // distortion model coeff
131  double mdDistortionEnabled; // One or zero depending on if distortion is on or off.
132  Vector<2> mvCenter; // Pixel projection center
133  Vector<2> mvFocal; // Pixel focal length
134  Vector<2> mvInvFocal; // Inverse pixel focal length
141 
142  // Radial distortion transformation factor: returns ration of distorted / undistorted radius.
143  inline double rtrans_factor(double r)
144  {
145  if(r < 0.001 || mdW == 0.0)
146  return 1.0;
147  else
148  return (mdWinv* atan(r * md2Tan) / r);
149  };
150 
151  // Inverse radial distortion: returns un-distorted radius from distorted.
152  inline double invrtrans(double r)
153  {
154  if(mdW == 0.0)
155  return r;
156  return(tan(r * mdW) * mdOneOver2Tan);
157  };
158 
159  std::string msName;
160 
161  friend class CameraCalibrator; // friend declarations allow access to calibration jacobian and camera update function.
162  friend class CalibImage;
163 };
164 
165 // Some inline projection functions:
167 {
168  Vector<2> v2Res;
169  v2Res[0] = camframe[0] * mvUFBLinearFocal[0] + mvUFBLinearCenter[0];
170  v2Res[1] = camframe[1] * mvUFBLinearFocal[1] + mvUFBLinearCenter[1];
171  return v2Res;
172 }
173 
175 {
176  Vector<2> v2Res;
177  v2Res[0] = (fbframe[0] - mvUFBLinearCenter[0]) * mvUFBLinearInvFocal[0];
178  v2Res[1] = (fbframe[1] - mvUFBLinearCenter[1]) * mvUFBLinearInvFocal[1];
179  return v2Res;
180 }
181 
182 
183 #endif
184 
double mdLastFactor
Definition: ATANCamera.h:120
double md2Tan
Definition: ATANCamera.h:127
double mdLastR
Definition: ATANCamera.h:118
double mdDistortionEnabled
Definition: ATANCamera.h:131
void SetImageSize(CVD::ImageRef irImageSize)
Definition: ATANCamera.h:72
Vector< 2 > UFBLinearUnProject(const Vector< 2 > &fbframe)
Definition: ATANCamera.h:174
TooN::Vector< 2 > vec(const ImageRef &ir)
bool mbInvalid
Definition: ATANCamera.h:121
double mdWinv
Definition: ATANCamera.h:130
Everything lives inside this namespace.
Definition: allocator.hh:48
Vector< 2 > mvLastIm
Definition: ATANCamera.h:116
double invrtrans(double r)
Definition: ATANCamera.h:152
Vector< 2 > GetImageSize()
Definition: ATANCamera.h:73
Vector< 2 > mvImageSize
Definition: ATANCamera.h:135
double mdW
Definition: ATANCamera.h:129
Vector< 2 > mvInvFocal
Definition: ATANCamera.h:134
Vector< 2 > mvImplaneBR
Definition: ATANCamera.h:140
Vector< 2 > mvCenter
Definition: ATANCamera.h:132
Vector< 2 > mvLastDistCam
Definition: ATANCamera.h:117
Vector< 2 > mvUFBLinearFocal
Definition: ATANCamera.h:136
Vector< 2 > Project(CVD::ImageRef ir)
Definition: ATANCamera.h:78
Vector< 2 > mvImplaneTL
Definition: ATANCamera.h:139
double mdLastDistR
Definition: ATANCamera.h:119
double mdOneOver2Tan
Definition: ATANCamera.h:128
static const Vector< NUMTRACKERCAMPARAMETERS > mvDefaultParams
Definition: ATANCamera.h:105
double mdOnePixelDist
Definition: ATANCamera.h:126
bool Invalid()
Definition: ATANCamera.h:89
ImageRef ir(const TooN::Vector< 2 > &v)
Vector< 2 > mvUFBLinearCenter
Definition: ATANCamera.h:138
double rtrans_factor(double r)
Definition: ATANCamera.h:143
Vector< NUMTRACKERCAMPARAMETERS > mgvvCameraParams
Definition: ATANCamera.h:108
Vector< 2 > mvFocal
Definition: ATANCamera.h:133
double mdLargestRadius
Definition: ATANCamera.h:124
double OnePixelDist()
Definition: ATANCamera.h:91
Vector< 2 > mvLastCam
Definition: ATANCamera.h:115
Vector< 2 > UFBLinearProject(const Vector< 2 > &camframe)
Definition: ATANCamera.h:166
double mdMaxR
Definition: ATANCamera.h:125
double LargestRadiusInImage()
Definition: ATANCamera.h:90
double PixelAspectRatio()
Definition: ATANCamera.h:101
Vector< 2 > UnProject(CVD::ImageRef ir)
Definition: ATANCamera.h:80
Vector< 2 > mvUFBLinearInvFocal
Definition: ATANCamera.h:137