IDSIFTSampling.cpp 2.8 KB

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