IDSIFTSampling.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * @file IDSIFTSampling.cpp
  3. * @brief random interest point sampling
  4. * @author Erik Rodner
  5. * @date 02/05/2008
  6. */
  7. #include <iostream>
  8. #include "vislearning/features/localfeatures/IDSIFTSampling.h"
  9. #include "vislearning/features/localfeatures/sift.h"
  10. #include "core/image/ImageTools.h"
  11. using namespace OBJREC;
  12. using namespace std;
  13. using namespace NICE;
  14. IDSIFTSampling::IDSIFTSampling(const Config *conf)
  15. {
  16. threshold = conf->gD("IDSIFTSampling", "threshold", 0.0);
  17. edgeThreshold = conf->gD("IDSIFTSampling", "edge_threshold", 10.0);
  18. minScale = conf->gD("IDSIFTSampling", "min_scale", 1.0);
  19. fixedOrientation = conf->gB("IDSIFTSampling", "fixed_orientation", true);
  20. octaves = conf->gI("IDSIFTSampling", "octaves", 6);
  21. levels = conf->gI("IDSIFTSampling", "levels", 3);
  22. first_octave = conf->gI("IDSIFTSampling", "first_octave", -1);
  23. magnif = conf->gD("IDSIFTSampling", "magnif", 1.5);
  24. deletemode = conf->gB("IDSIFTSampling", "deletemode", true);
  25. srand(time(NULL));
  26. }
  27. IDSIFTSampling::~IDSIFTSampling()
  28. {
  29. }
  30. int IDSIFTSampling::getInterests(const Image & img,
  31. std::vector<Vector> & positions) const
  32. {
  33. int O = octaves;
  34. int const S = levels;
  35. int const omin = first_octave;
  36. float const sigman = .5;
  37. float const sigma0 = 1.6 * powf(2.0f, 1.0f / S);
  38. if (O < 1)
  39. {
  40. O = std::max(int(std::floor(log2(std::min(img.width(), img.height())))
  41. - omin - 3), 1);
  42. }
  43. const unsigned char *blockimg = (unsigned char*) img.getPixelPointer();
  44. if (blockimg == NULL)
  45. {
  46. fprintf(stderr, "FATAL ERROR: do not use subimages !!\n");
  47. exit(-1);
  48. }
  49. float *blockimgfl = new float[img.width() * img.height()];
  50. for (int k = 0; k < img.width() * img.height(); k++)
  51. blockimgfl[k] = blockimg[k];
  52. VL::Sift sift(blockimgfl, img.width(), img.height(), sigman, sigma0, O, S,
  53. omin, -1, S + 1);
  54. sift.process(blockimgfl, img.width(), img.height());
  55. // compute keypoints
  56. sift.detectKeypoints(threshold, edgeThreshold);
  57. int keypointCount = 0;
  58. for (VL::Sift::KeypointsConstIter iter = sift.keypointsBegin(); iter
  59. != sift.keypointsEnd(); ++iter, keypointCount++)
  60. {
  61. NICE::Vector p(3);
  62. p[0] = iter->x;
  63. p[1] = iter->y;
  64. p[2] = iter->s;
  65. positions.push_back(p);
  66. keypointCount++;
  67. }
  68. delete[] blockimgfl;
  69. fprintf(stderr, "IDSIFTSampling:: Number of Keypoints = %d\n",
  70. keypointCount);
  71. if (keypointCount <= 0)
  72. fprintf(stderr, "FATAL ERROR: no keypoints found !!\n");
  73. return 0;
  74. }
  75. int IDSIFTSampling::getInterests(const ImagePyramid & imp,
  76. std::vector<Vector> & positions) const
  77. {
  78. int numLevels = imp.getNumLevels();
  79. if (numLevels > 0)
  80. {
  81. const NICE::Image & img = imp.getLevel(0);
  82. getInterests(img, positions);
  83. return 0;
  84. }
  85. else
  86. {
  87. fprintf(stderr, "IDSIFTSampling: Pyramid is too small (level==0)\n");
  88. return -1;
  89. }
  90. }