Image Component Library (ICL)
OpenSurfLib.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLCV/src/ICLCV/OpenSurfLib.h **
10 ** Module : ICLCV **
11 ** Authors: Christof Elbrechter (+see below) **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 /***********************************************************
32 * Please note that this file contains the whole OpenSURF *
33 * library. We decided to include the library by-source *
34 * in order to be able to ship its functionality easier *
35 * *
36 * --- OpenSURF --- *
37 * This library is distributed under the GNU GPL. Please *
38 * use the contact form at http://www.chrisevansdev.com *
39 * for more information. *
40 * *
41 * C. Evans, Research Into Robust Visual Features, *
42 * MSc University of Bristol, 2008. *
43 * *
44 ************************************************************/
45 
46 #pragma once
47 
48 #include <vector>
49 #include <ICLCore/OpenCV.h>
50 //#include <opencv/cv.h>
51 
52 #include <ICLCV/SurfFeature.h>
53 
54 namespace icl{
55 
56  namespace cv{
57 
58  namespace opensurf{
59 
61  typedef std::vector<SurfFeature> IpVec;
62  typedef std::vector<SurfMatch> IpPairVec;
63 
65  class ResponseLayer;
66  class FastHessian;
70  ICLCV_API void getMatches(IpVec &ipts1, IpVec &ipts2, IpPairVec &matches);
71 
72  ICLCV_API int translateCorners(IpPairVec &matches, const CvPoint src_corners[4], CvPoint dst_corners[4]);
73 
75 
76  ICLCV_API IplImage *Integral(IplImage *img);
77 
78 
80 
81  ICLCV_API float BoxIntegral(IplImage *img, int row, int col, int rows, int cols);
82 
83 
84 
85 
87 
92  class ICLCV_API Kmeans {
93  public:
94 
96  ~Kmeans() {};
97 
99  Kmeans() {};
100 
102  void Run(IpVec *ipts, int clusters, bool init = false);
103 
105  void SetIpoints(IpVec *ipts);
106 
108  void InitRandomClusters(int n);
109 
111  bool AssignToClusters();
112 
114  void RepositionClusters();
115 
117  float Distance(Ipoint &ip1, Ipoint &ip2);
118 
121 
124 
125  };
126 
129  public:
130 
131  int width, height, step, filter;
132  float *responses;
133  unsigned char *laplacian;
134 
135  inline ResponseLayer(int width, int height, int step, int filter){
136  assert(width > 0 && height > 0);
137 
138  this->width = width;
139  this->height = height;
140  this->step = step;
141  this->filter = filter;
142 
143  responses = new float[width*height];
144  laplacian = new unsigned char[width*height];
145 
146  memset(responses,0,sizeof(float)*width*height);
147  memset(laplacian,0,sizeof(unsigned char)*width*height);
148  }
149 
150  inline ~ResponseLayer(){
151  if (responses) delete [] responses;
152  if (laplacian) delete [] laplacian;
153  }
154 
155  inline unsigned char getLaplacian(unsigned int row, unsigned int column)
156  {
157  return laplacian[row * width + column];
158  }
159 
160  inline unsigned char getLaplacian(unsigned int row, unsigned int column, ResponseLayer *src)
161  {
162  int scale = this->width / src->width;
163 
164 #ifdef RL_DEBUG
165  assert(src->getCoords(row, column) == this->getCoords(scale * row, scale * column));
166 #endif
167 
168  return laplacian[(scale * row) * width + (scale * column)];
169  }
170 
171  inline float getResponse(unsigned int row, unsigned int column)
172  {
173  return responses[row * width + column];
174  }
175 
176  inline float getResponse(unsigned int row, unsigned int column, ResponseLayer *src)
177  {
178  int scale = this->width / src->width;
179 
180 #ifdef RL_DEBUG
181  assert(src->getCoords(row, column) == this->getCoords(scale * row, scale * column));
182 #endif
183 
184  return responses[(scale * row) * width + (scale * column)];
185  }
186 
187 #ifdef RL_DEBUG
188  std::vector<std::pair<int, int>> coords;
189 
190  inline std::pair<int,int> getCoords(unsigned int row, unsigned int column)
191  {
192  return coords[row * width + column];
193  }
194 
195  inline std::pair<int,int> getCoords(unsigned int row, unsigned int column, ResponseLayer *src)
196  {
197  int scale = this->width / src->width;
198  return coords[(scale * row) * width + (scale * column)];
199  }
200 #endif
201  };
202 
204  class ICLCV_API Surf {
205 
206  public:
207 
209  Surf(IplImage *img, std::vector<Ipoint> &ipts);
210 
212  void getDescriptors(bool bUpright = false);
213 
214  private:
215 
217  void getOrientation();
218 
220  void getDescriptor(bool bUpright = false);
221 
223  inline float gaussian(int x, int y, float sig);
224  inline float gaussian(float x, float y, float sig);
225 
227  inline float haarX(int row, int column, int size);
228  inline float haarY(int row, int column, int size);
229 
231  float getAngle(float X, float Y);
232 
233 
235  IplImage *img;
236 
239 
241  int index;
242  };
243 
244  static const int OCTAVES = 5;
245  static const int INTERVALS = 4;
246  static const float THRES = 0.0004f;
247  static const int INIT_SAMPLE = 2;
248 
250  ICLCV_API void surfDetDes(IplImage *img, /* image to find Ipoints in */
251  std::vector<Ipoint> &ipts, /* reference to vector of Ipoints */
252  bool upright = false, /* run in rotation invariant mode? */
253  int octaves = OCTAVES, /* number of octaves to calculate */
254  int intervals = INTERVALS, /* number of intervals per octave */
255  int init_sample = INIT_SAMPLE, /* initial sampling step */
256  float thres = THRES /* blob response threshold */);
257 
259  ICLCV_API void surfDet(IplImage *img, /* image to find Ipoints in */
260  std::vector<Ipoint> &ipts, /* reference to vector of Ipoints */
261  int octaves = OCTAVES, /* number of octaves to calculate */
262  int intervals = INTERVALS, /* number of intervals per octave */
263  int init_sample = INIT_SAMPLE, /* initial sampling step */
264  float thres = THRES /* blob response threshold */);
265 
267  ICLCV_API void surfDes(IplImage *img, /* image to find Ipoints in */
268  std::vector<Ipoint> &ipts, /* reference to vector of Ipoints */
269  bool upright = false); /* run in rotation invariant mode? */
270 
271 
273  ICLCV_API void error(const char *msg);
274 
276  ICLCV_API void showImage(const IplImage *img);
277 
279  ICLCV_API void showImage(char *title, const IplImage *img);
280 
281  // Convert image to single channel 32F
282  ICLCV_API IplImage* getGray(const IplImage *img);
283 
285  ICLCV_API void drawIpoint(IplImage *img, Ipoint &ipt, int tailSize = 0);
286 
288  ICLCV_API void drawIpoints(IplImage *img, std::vector<Ipoint> &ipts, int tailSize = 0);
289 
291  ICLCV_API void drawWindows(IplImage *img, std::vector<Ipoint> &ipts);
292 
293  // Draw the FPS figure on the image (requires at least 2 calls)
294  ICLCV_API void drawFPS(IplImage *img);
295 
297  ICLCV_API void drawPoint(IplImage *img, Ipoint &ipt);
298 
300  ICLCV_API void drawPoints(IplImage *img, std::vector<Ipoint> &ipts);
301 
303  ICLCV_API void saveSurf(char *filename, std::vector<Ipoint> &ipts);
304 
306  ICLCV_API void loadSurf(char *filename, std::vector<Ipoint> &ipts);
307 
309  inline int fRound(float flt) { return (int) floor(flt+0.5f); }
310 
311  } // end of namespace opensurf
312 
313  } // end of namespace cv
314 
315 } // end of namespace icl
ICLCV_API void surfDes(IplImage *img, std::vector< Ipoint > &ipts, bool upright=false)
Library function describes interest points in vector.
Surf Feation class.
Definition: OpenSurfLib.h:204
IpVec clusters
Vector stores cluster centers.
Definition: OpenSurfLib.h:123
ICLCV_API float BoxIntegral(IplImage *img, int row, int col, int rows, int cols)
Computes the sum of pixels within the rectangle.
undocument this line if you encounter any issues!
Definition: Any.h:37
static const float THRES
Definition: OpenSurfLib.h:246
ICLCV_API void showImage(const IplImage *img)
Show the provided image and wait for keypress.
IplImage * img
Integral image where Ipoints have been detected.
Definition: OpenSurfLib.h:235
ICLCV_API int translateCorners(IpPairVec &matches, const CvPoint src_corners[4], CvPoint dst_corners[4])
ICLCV_API IplImage * Integral(IplImage *img)
Computes the integral image of image img. Assumes source image to be a.
ICLCV_API void getMatches(IpVec &ipts1, IpVec &ipts2, IpPairVec &matches)
IpVec & ipts
Ipoints vector.
Definition: OpenSurfLib.h:238
ICLCV_API void saveSurf(char *filename, std::vector< Ipoint > &ipts)
Save the SURF features to file.
ICLCV_API void drawPoints(IplImage *img, std::vector< Ipoint > &ipts)
Draw a Point at all features.
static const int OCTAVES
Definition: OpenSurfLib.h:244
Response Layer class.
Definition: OpenSurfLib.h:128
float * responses
Definition: OpenSurfLib.h:132
SurfFeature Ipoint
Definition: OpenSurfLib.h:60
ICLCV_API void drawIpoint(IplImage *img, Ipoint &ipt, int tailSize=0)
Draw a single feature on the image.
static const int INTERVALS
Definition: OpenSurfLib.h:245
IpVec * ipts
Vector stores ipoints for this run.
Definition: OpenSurfLib.h:120
#define ICLCV_API
Definition: CompatMacros.h:177
static const int INIT_SAMPLE
Definition: OpenSurfLib.h:247
std::vector< SurfFeature > IpVec
Definition: OpenSurfLib.h:61
ICLQt_API core::Img< T > filter(const core::Img< T > &image, const std::string &filter)
applies a filter operation on the source image (affinity for float)
unsigned char getLaplacian(unsigned int row, unsigned int column)
Definition: OpenSurfLib.h:155
int width
Definition: OpenSurfLib.h:131
ICLCV_API void drawWindows(IplImage *img, std::vector< Ipoint > &ipts)
Draw descriptor windows around Ipoints in the provided vector.
Kmeans clustering.
Definition: OpenSurfLib.h:92
unsigned char * laplacian
Definition: OpenSurfLib.h:133
Generic SURF Feature type.
Definition: SurfFeature.h:42
ICLCV_API IplImage * getGray(const IplImage *img)
ICLCV_API void drawPoint(IplImage *img, Ipoint &ipt)
Draw a Point at feature location.
ICLCV_API void surfDet(IplImage *img, std::vector< Ipoint > &ipts, int octaves=OCTAVES, int intervals=INTERVALS, int init_sample=INIT_SAMPLE, float thres=THRES)
Library function builds vector of interest points.
ICLCV_API void error(const char *msg)
Display error message and terminate program.
float getResponse(unsigned int row, unsigned int column)
Definition: OpenSurfLib.h:171
ICLCV_API void drawIpoints(IplImage *img, std::vector< Ipoint > &ipts, int tailSize=0)
Draw all the Ipoints in the provided vector.
static void scale(const int deg, xcomplex *P)
Definition: PolynomialSolver.h:133
~ResponseLayer()
Definition: OpenSurfLib.h:150
unsigned char getLaplacian(unsigned int row, unsigned int column, ResponseLayer *src)
Definition: OpenSurfLib.h:160
std::vector< SurfMatch > IpPairVec
Definition: OpenSurfLib.h:62
~Kmeans()
Destructor.
Definition: OpenSurfLib.h:96
Kmeans()
Constructor.
Definition: OpenSurfLib.h:99
ICLCV_API void drawFPS(IplImage *img)
ICLCV_API void surfDetDes(IplImage *img, std::vector< Ipoint > &ipts, bool upright=false, int octaves=OCTAVES, int intervals=INTERVALS, int init_sample=INIT_SAMPLE, float thres=THRES)
Library function builds vector of described interest points.
float getResponse(unsigned int row, unsigned int column, ResponseLayer *src)
Definition: OpenSurfLib.h:176
int fRound(float flt)
Round float to nearest integer.
Definition: OpenSurfLib.h:309
ResponseLayer(int width, int height, int step, int filter)
Definition: OpenSurfLib.h:135
int index
Index of current Ipoint in the vector.
Definition: OpenSurfLib.h:241
ICLCV_API void loadSurf(char *filename, std::vector< Ipoint > &ipts)
Load the SURF features from file.