source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/Rectification/src/Rectify/Ops.h @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 8.5 KB
Line 
1//-*-c++-*-
2
3#ifndef _RECTIFY_EXTRAS_H
4#define _RECTIFY_EXTRAS_H
5
6#include <assert.h>
7
8// This files contains all the code I've had to cannabilise out of my home made libraries.
9// Not a neat job - really most of these datatypes are fundamentally unnecessary and should be replaced by simple 1D arrays
10// But I can't be bothered.
11
12/*******************************/
13/* Images and matrices */
14
15
16
17/*
18
19// EXAMPLE!
20// If you want to use your image type directly then you can use an adaptor as below.
21// This example shows adaption of a user image type MyImage that has equivalent functions
22// recreate, isrgb, getpixel, xdim, ydim
23
24namespace Image {
25
26  class ImageAdaptor {
27  protected:
28
29    MyImage &image;
30
31  public:
32
33    typedef unsigned int value_type;
34
35    ImageAdaptor(MyImage &im) : image(im) {}
36
37    void resize(size_t w, size_t h, bool iscol) {
38      image.recreate(w,h);
39    }
40
41    inline unsigned int& operator()(size_t x, size_t y) { return image.getpix(x,y); }
42    inline const unsigned int& operator()(size_t x, size_t y) const { return image.getpix(x,y); }
43
44    bool isColour() const { return image.isrgb(); }
45
46    inline size_t xsize() const { return image.xdim(); }
47    inline size_t ysize() const { return image.ydim(); }
48  }
49}
50*/
51
52
53
54
55namespace Image {
56
57/*
58  This class demonstrates all the functionality an image class must offer if it is to be supplied to the rectification routines
59  It works as a standalone very very basic image class too.
60  If you have an image class then simply use this as a wrapper (i.e. make the constructor take your image class and redirect
61  all other functions to your class). See above for an example
62 */
63template <class T=unsigned int>
64class Image {
65protected:
66
67  T *data;
68  size_t xs, ys;
69  bool iscolour;
70
71public:
72
73
74  /// The type of object stored in the container
75  typedef T value_type;
76
77  Image() : data(NULL), xs(0), ys(0), iscolour(false) { }
78  Image(size_t w, size_t h, bool colour) : data(new T[w*h]), xs(w), ys(h), iscolour(colour) {
79    assert(w>0 && h>0);
80  }
81
82  ~Image() {
83    if (data!=NULL)
84      delete[] data;
85  }
86  void resize(size_t w, size_t h, bool iscol) {
87    assert(w>0 && h>0);
88    iscolour=iscol;
89    if (w!=xs || h!=ys) {
90      if (data!=NULL)
91        delete[] data;
92
93      xs=w; ys=h;
94      data=new T[xs*ys];
95    }
96  }
97
98  inline T& operator()(size_t x, size_t y) {
99    return data[x+y*xs];
100  }
101  inline const T& operator()(size_t x, size_t y) const {
102    return data[x+y*xs];
103  }
104
105  bool isColour() const { return iscolour; }
106
107  inline size_t xsize() const { return xs; }
108  inline size_t ysize() const { return ys; }
109
110};
111
112}
113
114namespace MatVec {
115
116  template <class T>
117  class Matrix {
118  protected:
119
120    T *data;
121    size_t nrows, ncols, length;
122    bool iscolour;
123
124    void copy(const Matrix<T> &Other) {
125      resize(Other.nrows, Other.ncols, Other.iscolour);
126      if (length!=0) {
127        T *begin=Other.data, *end=Other.data+length, *out=data;
128        for (;begin<end;)
129          *out++=*begin++;
130      }
131    }
132
133  public:
134
135    /// The type of object stored in the container
136    typedef T value_type;
137
138    Matrix() : data(NULL), nrows(0), ncols(0), length(0), iscolour(false) { }
139    Matrix(size_t nrows_, size_t ncols_) : data(new T[nrows_*ncols_]), nrows(nrows_), ncols(ncols_), length(nrows_*ncols) {
140      assert(nrows>0 && ncols>0);
141    }
142    /// Copy constructor. Performs a deep copy
143    Matrix(const Matrix<T> &Other) : data(NULL), nrows(0), ncols(0), length(0) { copy(Other); }
144    /// Allow = assignment
145    Matrix<T>& operator=(const Matrix<T> &Other) { copy(Other); return *this;}
146
147    ~Matrix() {
148      if (data!=NULL)
149        delete[] data;
150    }
151
152    void resize(size_t w, size_t h, bool colour) {
153      iscolour=colour;
154      assert(w>0 && h>0);
155      if (w!=nrows || h!=ncols) {
156        if (data!=NULL)
157          delete[] data;
158
159        nrows=w; ncols=h; length=nrows*ncols;
160        data=new T[nrows*ncols];
161      }
162    }
163
164    inline T& operator()(size_t x, size_t y) {
165      assert(x<nrows && y<ncols);
166      return data[x+y*nrows];
167    }
168    inline const T& operator()(size_t x, size_t y) const {
169      assert(x<nrows && y<ncols);
170      return data[x+y*nrows];
171    }
172
173    inline size_t num_rows() const { return nrows; }
174    inline size_t num_cols() const { return ncols; }
175  };
176
177}
178
179namespace MultiViewGeom {
180
181  template <class T>
182  class FMatrix: public MatVec::Matrix<T> {
183  public:
184
185    FMatrix() : MatVec::Matrix<T>(3,3) {}
186  };
187
188}
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217/*******************************/
218/* Geometry */
219
220
221namespace Geometry {
222
223/*! \brief Very very simple point classes
224*/
225
226template <class T>
227class Point2D {
228private:
229  T vals[2];
230
231public:
232  typedef T value_type;
233
234  Point2D() {}
235  Point2D(T x, T y) { vals[0]=x; vals[1]=y; }
236  Point2D(const Point2D<T> &P) { vals[0]=P.vals[0]; vals[1]=P.vals[1]; }
237  /// Specialised = assignment (for efficiency and to stop cfront errors).
238  Point2D &operator=(const Point2D<T> &P) {
239    vals[0]=P.vals[0]; vals[1]=P.vals[1];
240    return *this;
241  }
242
243  /// Get direct access to x
244  inline T& x() { return vals[0]; }
245  /// Get direct access to x
246  inline T& y() { return vals[1]; }
247  /// Can't overwrite access to x
248  inline const T& x() const { return vals[0]; }
249  /// Can't overwrite access to x
250  inline const T& y() const { return vals[1]; }
251
252  inline size_t size() const { return 2; }
253  inline T& operator[](size_t n) { assert(n<2); return vals[n]; }
254  inline const T& operator[](size_t n) const { assert(n<2); return vals[n]; }
255};
256
257template <class T>
258class Homg2DPoint {
259private:
260  T vals[3];
261
262public:
263  typedef T value_type;
264
265  Homg2DPoint() { vals[0]=0; vals[1]=0; vals[2]=0;}
266  Homg2DPoint(T x, T y, T w) { vals[0]=x; vals[1]=y; vals[2]=w;}
267  Homg2DPoint(const Homg2DPoint<T> &P) { vals[0]=P.vals[0]; vals[1]=P.vals[1]; vals[2]=P.vals[2]; }
268  /// Specialised = assignment (for efficiency and to stop cfront errors).
269  Homg2DPoint &operator=(const Homg2DPoint<T> &P) { vals[0]=P.vals[0]; vals[1]=P.vals[1]; vals[2]=P.vals[2]; return *this; }
270
271  inline T& x() { return vals[0]; }
272  inline T& y() { return vals[1]; }
273  inline T& w() { return vals[2]; }
274  inline const T& x() const { return vals[0]; }
275  inline const T& y() const { return vals[1]; }
276  inline const T& w() const { return vals[2]; }
277
278  inline T& operator[](size_t n) { assert(n<3); return vals[n]; }
279  inline const T& operator[](size_t n) const { assert(n<3); return vals[n]; }
280
281  inline size_t size() const { return 3; }
282};
283
284template <class T>
285class Line2D {
286private:
287  T vals[3];
288
289public:
290  typedef T value_type;
291
292  Line2D() { }
293  Line2D(T a, T b, T c) { vals[0]=a; vals[1]=b; vals[2]=c;}
294  Line2D(const Line2D<T> &P) { vals[0]=P.vals[0]; vals[1]=P.vals[1]; vals[2]=P.vals[2]; }
295  /// Specialised = assignment (for efficiency and to stop cfront errors).
296  Line2D &operator=(const Line2D<T> &P) { vals[0]=P.vals[0]; vals[1]=P.vals[1]; vals[2]=P.vals[2]; return *this; }
297
298  inline T& a() { return vals[0]; }
299  inline T& b() { return vals[1]; }
300  inline T& c() { return vals[2]; }
301  inline const T& a() const { return vals[0]; }
302  inline const T& b() const { return vals[1]; }
303  inline const T& c() const { return vals[2]; }
304
305  inline size_t size() const { return 3; }
306  inline T& operator[](size_t n) { assert(n<3); return vals[n]; }
307  inline const T& operator[](size_t n) const { assert(n<3); return vals[n]; }
308};
309
310}
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337/*******************************/
338/*! Calculate homographies that are consistant with a particular fundamental matrix. These use extra matches to select a homography from the 3 parameter family that produce a mapping that is consistant with the fundamental matrix (i.e. homographies that map points on epipolar lines to points on epipolar lines).
339 */
340namespace HomogConsistF {
341
342  void RANSAC(const std::vector<std::pair<Geometry::Point2D<double>, Geometry::Point2D<double> > > &Matches,
343              std::vector<std::pair<Geometry::Point2D<double>, Geometry::Point2D<double> > > &OutlierFree,
344              MatVec::Matrix<double> &H, const MultiViewGeom::FMatrix<double> &FM);
345
346}
347
348
349// ******************
350// NUMERICAL ROUTINES
351// ******************
352
353namespace MatrixOps {
354
355  extern void EigenValVec_Symmetric(const MatVec::Matrix<double> &A, double *EigenVals, MatVec::Matrix<double> &EigenVecs);
356  extern bool inverse(const MatVec::Matrix<double> &A, MatVec::Matrix<double> &AInv);
357}
358
359namespace LAPACK {
360
361  // allow version with vectors. NOTE: B & X Can safely be the same vector
362  // Overwrites A
363  extern bool LeastSquaresRankDeff(MatVec::Matrix<double>& A, const double *B, double *X);
364
365}
366
367/** This function can be called to get the median from the given iterator range. Assumes is unsorted. */
368namespace MiscMath {
369
370  extern double VectorMedian(double *VecBegin, double *VecEnd);
371
372}
373
374#endif
Note: See TracBrowser for help on using the repository browser.