00001 #ifndef fl_descriptor_h
00002 #define fl_descriptor_h
00003
00004
00005 #include "fl/convolve.h"
00006 #include "fl/matrix.h"
00007 #include "fl/point.h"
00008 #include "fl/canvas.h"
00009
00010 #include <iostream>
00011 #include <vector>
00012
00013
00014 namespace fl
00015 {
00016
00017
00029 class Comparison
00030 {
00031 public:
00032 virtual ~Comparison ();
00033
00034 virtual Vector<float> preprocess (const Vector<float> & value) const;
00035 virtual float value (const Vector<float> & value1, const Vector<float> & value2, bool preprocessed = false) const = 0;
00036
00037 virtual void read (std::istream & stream);
00038 virtual void write (std::ostream & stream, bool withName = true);
00039 static void addProducts ();
00040 };
00041
00042 class Descriptor;
00043
00048 class ComparisonCombo : public Comparison
00049 {
00050 public:
00051 ComparisonCombo (std::vector<Descriptor *> & descriptors);
00052 ComparisonCombo (std::istream & stream);
00053 virtual ~ComparisonCombo ();
00054
00055 virtual Vector<float> preprocess (const Vector<float> & value) const;
00056 virtual float value (const Vector<float> & value1, const Vector<float> & value2, bool preprocessed = false) const;
00057 virtual float value (int index, const Vector<float> & value1, const Vector<float> & value2, bool preprocessed = false) const;
00058 Vector<float> extract (int index, const Vector<float> & value) const;
00059
00060 virtual void read (std::istream & stream);
00061 virtual void write (std::ostream & stream, bool withName = true);
00062
00063 std::vector<Comparison *> comparisons;
00064 std::vector<int> dimensions;
00065 int totalDimension;
00066 };
00067
00078 class NormalizedCorrelation : public Comparison
00079 {
00080 public:
00081 NormalizedCorrelation (bool subtractMean = true);
00082 NormalizedCorrelation (std::istream & stream);
00083
00084 virtual Vector<float> preprocess (const Vector<float> & value) const;
00085 virtual float value (const Vector<float> & value1, const Vector<float> & value2, bool preprocessed = false) const;
00086
00087 virtual void read (std::istream & stream);
00088 virtual void write (std::ostream & stream, bool withName = true);
00089
00090 bool subtractMean;
00091 };
00092
00098 class MetricEuclidean : public Comparison
00099 {
00100 public:
00101 MetricEuclidean (float upperBound = INFINITY);
00102 MetricEuclidean (std::istream & stream);
00103
00104 virtual float value (const Vector<float> & value1, const Vector<float> & value2, bool preprocessed = false) const;
00105
00106 virtual void read (std::istream & stream);
00107 virtual void write (std::ostream & stream, bool withName = true);
00108
00109 float upperBound;
00110 };
00111
00117 class HistogramIntersection : public Comparison
00118 {
00119 public:
00120 HistogramIntersection () {}
00121 HistogramIntersection (std::istream & stream);
00122 virtual float value (const Vector<float> & value1, const Vector<float> & value2, bool preprocessed = false) const;
00123 };
00124
00129 class ChiSquared : public Comparison
00130 {
00131 public:
00132 ChiSquared () {}
00133 ChiSquared (std::istream & stream);
00134
00135 virtual Vector<float> preprocess (const Vector<float> & value) const;
00136 virtual float value (const Vector<float> & value1, const Vector<float> & value2, bool preprocessed = false) const;
00137 };
00138
00139
00140
00141
00142 class Descriptor
00143 {
00144 public:
00145 Descriptor ();
00146 virtual ~Descriptor ();
00147
00148 virtual Vector<float> value (const Image & image, const PointAffine & point) = 0;
00149 virtual Vector<float> value (const Image & image);
00150 virtual Image patch (const Vector<float> & value) = 0;
00151 virtual Comparison * comparison ();
00152
00153 virtual void read (std::istream & stream);
00154 virtual void write (std::ostream & stream, bool withName = true);
00155
00156 bool monochrome;
00157 int dimension;
00158 float supportRadial;
00159 };
00160
00165 class DescriptorCombo : public Descriptor
00166 {
00167 public:
00168 DescriptorCombo ();
00169 DescriptorCombo (std::istream & stream);
00170 virtual ~DescriptorCombo ();
00171
00172 void add (Descriptor * descriptor);
00173 virtual Vector<float> value (const Image & image, const PointAffine & point);
00174 virtual Vector<float> value (const Image & image);
00175 virtual Image patch (const Vector<float> & value);
00176 Image patch (int index, const Vector<float> & value);
00177 virtual Comparison * comparison ();
00178 virtual void read (std::istream & stream);
00179 virtual void write (std::ostream & stream, bool withName = true);
00180
00181 std::vector<Descriptor *> descriptors;
00182
00183 void * lastBuffer;
00184 double lastTime;
00185 Image grayImage;
00186 };
00187
00191 class DescriptorScale : public Descriptor
00192 {
00193 public:
00194 DescriptorScale (float firstScale = 1, float lastScale = 25, int interQuanta = 40, float quantum = 2);
00195 DescriptorScale (std::istream & stream);
00196 void initialize (float firstScale, float lastScale, float stepSize);
00197
00198 virtual Vector<float> value (const Image & image, const PointAffine & point);
00199 virtual Image patch (const Vector<float> & value);
00200 virtual void read (std::istream & stream);
00201 virtual void write (std::ostream & stream, bool withName = true);
00202
00203 std::vector<Laplacian> laplacians;
00204 };
00205
00210 class DescriptorOrientation : public Descriptor
00211 {
00212 public:
00213 DescriptorOrientation (float supportRadial = 6.0f, int supportPixel = 32, float kernelSize = 2.5f);
00214 DescriptorOrientation (std::istream & stream);
00215 void initialize (float supportRadial, int supportPixel, float kernelSize);
00216
00217 virtual Vector<float> value (const Image & image, const PointAffine & point);
00218 virtual Image patch (const Vector<float> & value);
00219 virtual void read (std::istream & stream);
00220 virtual void write (std::ostream & stream, bool withName = true);
00221
00222 int supportPixel;
00223 float kernelSize;
00224 GaussianDerivativeFirst Gx;
00225 GaussianDerivativeFirst Gy;
00226 };
00227
00232 class DescriptorOrientationHistogram : public Descriptor
00233 {
00234 public:
00235 DescriptorOrientationHistogram (float supportRadial = 4.5f, int supportPixel = 16, float kernelSize = 2.5f, int bins = 36);
00236 DescriptorOrientationHistogram (std::istream & stream);
00237
00238 void computeGradient (const Image & image);
00239
00240 virtual Vector<float> value (const Image & image, const PointAffine & point);
00241 virtual Image patch (const Vector<float> & value);
00242 virtual void read (std::istream & stream);
00243 virtual void write (std::ostream & stream, bool withName = true);
00244
00245 int supportPixel;
00246 float kernelSize;
00247 int bins;
00248 float cutoff;
00249
00250 void * lastBuffer;
00251 double lastTime;
00252 ImageOf<float> I_x;
00253 ImageOf<float> I_y;
00254 };
00255
00271 class DescriptorContrast : public Descriptor
00272 {
00273 public:
00274 DescriptorContrast (float supportRadial = 6.0f, int supportPixel = 32);
00275 DescriptorContrast (std::istream & stream);
00276
00277 virtual Vector<float> value (const Image & image, const PointAffine & point);
00278 virtual Image patch (const Vector<float> & value);
00279 virtual Comparison * comparison ();
00280 virtual void read (std::istream & stream);
00281 virtual void write (std::ostream & stream, bool withName = true);
00282
00283 int supportPixel;
00284 };
00285
00286 class DescriptorFilters : public Descriptor
00287 {
00288 public:
00289 DescriptorFilters ();
00290 DescriptorFilters (std::istream & stream);
00291 virtual ~DescriptorFilters ();
00292
00293 void prepareFilterMatrix ();
00294
00295 virtual Vector<float> value (const Image & image, const PointAffine & point);
00296 virtual Image patch (const Vector<float> & value);
00297 virtual void read (std::istream & stream);
00298 virtual void write (std::ostream & stream, bool withName = true);
00299
00300 std::vector<ConvolutionDiscrete2D> filters;
00301 Matrix<float> filterMatrix;
00302 int patchWidth;
00303 int patchHeight;
00304 };
00305
00306 class DescriptorFiltersTexton : public DescriptorFilters
00307 {
00308 public:
00309 DescriptorFiltersTexton (int angles = 6, int scales = 4, float firstScale = -1, float scaleStep = -1);
00310 DescriptorFiltersTexton (std::istream & stream) : DescriptorFilters (stream) {}
00311 };
00312
00313 class DescriptorPatch : public Descriptor
00314 {
00315 public:
00316 DescriptorPatch (int width = 10, float supportRadial = 4.2);
00317 DescriptorPatch (std::istream & stream);
00318 virtual ~DescriptorPatch ();
00319
00320 virtual Vector<float> value (const Image & image, const PointAffine & point);
00321 virtual Image patch (const Vector<float> & value);
00322 virtual Comparison * comparison ();
00323 virtual void read (std::istream & stream);
00324 virtual void write (std::ostream & stream, bool withName = true);
00325
00326 int width;
00327 };
00328
00329 class DescriptorSchmidScale : public Descriptor
00330 {
00331 public:
00332 DescriptorSchmidScale (float sigma = 1.0);
00333 DescriptorSchmidScale (std::istream & stream);
00334 void initialize ();
00335 virtual ~DescriptorSchmidScale ();
00336
00337 virtual Vector<float> value (const Image & image, const PointAffine & point);
00338 virtual Image patch (const Vector<float> & value);
00339 virtual void read (std::istream & stream);
00340 virtual void write (std::ostream & stream, bool withName = true);
00341
00342 float sigma;
00343 ConvolutionDiscrete2D G;
00344 ConvolutionDiscrete2D Gx;
00345 ConvolutionDiscrete2D Gy;
00346 ConvolutionDiscrete2D Gxx;
00347 ConvolutionDiscrete2D Gxy;
00348 ConvolutionDiscrete2D Gyy;
00349 ConvolutionDiscrete2D Gxxx;
00350 ConvolutionDiscrete2D Gxxy;
00351 ConvolutionDiscrete2D Gxyy;
00352 ConvolutionDiscrete2D Gyyy;
00353 };
00354
00355 class DescriptorSchmid : public Descriptor
00356 {
00357 public:
00358 DescriptorSchmid (int scaleCount = 8, float scaleStep = -1);
00359 DescriptorSchmid (std::istream & stream);
00360 void initialize (int scaleCount);
00361 virtual ~DescriptorSchmid ();
00362
00363 virtual Vector<float> value (const Image & image, const PointAffine & point);
00364 virtual Image patch (const Vector<float> & value);
00365 virtual void read (std::istream & stream);
00366 virtual void write (std::ostream & stream, bool withName = true);
00367
00368 DescriptorSchmidScale * findScale (float sigma);
00369
00370 float scaleStep;
00371 std::vector<DescriptorSchmidScale *> descriptors;
00372 };
00373
00374 class DescriptorSpin : public Descriptor
00375 {
00376 public:
00377 DescriptorSpin (int binsRadial = 5, int binsIntensity = 6, float supportRadial = 3, float supportIntensity = 3);
00378 DescriptorSpin (std::istream & stream);
00379
00380 virtual Vector<float> value (const Image & image, const PointAffine & point);
00381 virtual Image patch (const Vector<float> & value);
00382 virtual Comparison * comparison ();
00383 virtual void read (std::istream & stream);
00384 virtual void write (std::ostream & stream, bool withName = true);
00385
00386 int binsRadial;
00387 int binsIntensity;
00388 float supportIntensity;
00389 };
00390
00395 class DescriptorSIFT : public Descriptor
00396 {
00397 public:
00398 DescriptorSIFT (int width = 4, int angles = 8);
00399 DescriptorSIFT (std::istream & stream);
00400
00401 void computeGradient (const Image & image);
00402
00403 virtual Vector<float> value (const Image & image, const PointAffine & point);
00404 virtual Image patch (const Vector<float> & value);
00405 void patch (const std::string & fileName, const Vector<float> & value);
00406 void patch (Canvas * canvas, const Vector<float> & value, int size);
00407 virtual Comparison * comparison ();
00408 virtual void read (std::istream & stream);
00409 virtual void write (std::ostream & stream, bool withName = true);
00410
00411 int width;
00412 int angles;
00413 int supportPixel;
00414 float sigmaWeight;
00415 float maxValue;
00416
00417 void * lastBuffer;
00418 double lastTime;
00419 ImageOf<float> I_x;
00420 ImageOf<float> I_y;
00421 };
00422
00427 class DescriptorColorHistogram2D : public Descriptor
00428 {
00429 public:
00430 DescriptorColorHistogram2D (int width = 5, float supportRadial = 4.2f);
00431 DescriptorColorHistogram2D (std::istream & stream);
00432 void initialize ();
00433
00434
00435 void clear ();
00436 void addToHistogram (const Image & image, const int x, const int y);
00437 void add (const Image & image, int x, int y);
00438 Vector<float> finish ();
00439
00440 virtual Vector<float> value (const Image & image, const PointAffine & point);
00441 virtual Vector<float> value (const Image & image);
00442 virtual Image patch (const Vector<float> & value);
00443 virtual Comparison * comparison ();
00444 virtual void read (std::istream & stream);
00445 virtual void write (std::ostream & stream, bool withName = true);
00446
00447 int width;
00448 Matrix<bool> valid;
00449 Matrix<float> histogram;
00450 };
00451
00457 class DescriptorColorHistogram3D : public Descriptor
00458 {
00459 public:
00460 DescriptorColorHistogram3D (int width = 5, int height = -1, float supportRadial = 4.2f);
00461 DescriptorColorHistogram3D (std::istream & stream);
00462 ~DescriptorColorHistogram3D ();
00463 void initialize ();
00464
00465
00466 void clear ();
00467 void addToHistogram (const Image & image, const int x, const int y);
00468 void add (const Image & image, int x, int y);
00469 Vector<float> finish ();
00470
00471 virtual Vector<float> value (const Image & image, const PointAffine & point);
00472 virtual Vector<float> value (const Image & image);
00473 virtual Image patch (const Vector<float> & value);
00474 virtual Comparison * comparison ();
00475 virtual void read (std::istream & stream);
00476 virtual void write (std::ostream & stream, bool withName = true);
00477
00478 int width;
00479 int height;
00480 bool * valid;
00481 float * histogram;
00482 };
00483
00489 class DescriptorTextonScale : public Descriptor
00490 {
00491 public:
00492 DescriptorTextonScale (int angles = 4, float firstScale = 1.0f, float lastScale = 4.0f, int extraSteps = 3);
00493 DescriptorTextonScale (std::istream & stream);
00494 void initialize ();
00495
00496 void preprocess (const Image & image);
00497
00498 virtual Vector<float> value (const Image & image, const PointAffine & point);
00499 virtual Vector<float> value (const Image & image);
00500 virtual Image patch (const Vector<float> & value);
00501 virtual void read (std::istream & stream);
00502 virtual void write (std::ostream & stream, bool withName = true);
00503
00504 void * lastBuffer;
00505 double lastTime;
00506 std::vector< ImageOf<float> > responses;
00507
00508 int angles;
00509 float firstScale;
00510 float lastScale;
00511 int steps;
00512
00513 int bankSize;
00514 float scaleRatio;
00515 std::vector<ConvolutionDiscrete2D> filters;
00516 };
00517
00531 class DescriptorLBP : public Descriptor
00532 {
00533 public:
00534 DescriptorLBP (int P = 8, float R = 1.0f, float supportRadial = 4.2f, int supportPixel = 32);
00535 DescriptorLBP (std::istream & stream);
00536 void initialize ();
00537
00538 void preprocess (const Image & image);
00539 void add (const int x, const int y, Vector<float> & result);
00540
00541 virtual Vector<float> value (const Image & image, const PointAffine & point);
00542 virtual Vector<float> value (const Image & image);
00543 virtual Image patch (const Vector<float> & value);
00544 virtual Comparison * comparison ();
00545 virtual void read (std::istream & stream);
00546 virtual void write (std::ostream & stream, bool withName = true);
00547
00548 int P;
00549 float R;
00550 int supportPixel;
00551
00552 void * lastBuffer;
00553 double lastTime;
00554 ImageOf<unsigned char> categoryImage;
00555
00559 struct Interpolate
00560 {
00561 int xl;
00562 int yl;
00563 int xh;
00564 int yh;
00565 float wll;
00566 float wlh;
00567 float whl;
00568 float whh;
00569 bool exact;
00570 };
00571 std::vector<Interpolate> interpolates;
00572 };
00573 }
00574
00575
00576 #endif