LFSiftPP.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**
  2. * @file LFSiftPP.cpp
  3. * @brief Sift++ interface
  4. * @author Erik Rodner
  5. * @date 11/19/2007
  6. */
  7. #include <iostream>
  8. #include <sstream>
  9. // #ifdef NICE_USELIB_ICE
  10. // #include <image_nonvis.h>
  11. // #endif
  12. #include "vislearning/features/localfeatures/LFSiftPP.h"
  13. using namespace std;
  14. using namespace NICE;
  15. #include "vislearning/features/localfeatures/sift.h"
  16. using namespace OBJREC;
  17. LFSiftPP::LFSiftPP( const Config *conf )
  18. {
  19. threshold = conf->gD("LFSiftPP", "threshold", 0.0);
  20. edgeThreshold = conf->gD("LFSiftPP", "edge_threshold", 10.0);
  21. octaves = conf->gI("LFSiftPP", "octaves", 6);
  22. first_octave = conf->gI("LFSiftPP", "first_octave", -1);
  23. levels = conf->gI("LFSiftPP", "levels", 3);
  24. minScale = conf->gD("LFSiftPP", "min_scale", 1);
  25. maxScale = conf->gD("LFSiftPP", "max_scale", 4);
  26. numScales = conf->gI("LFSiftPP", "num_scales", 10);
  27. numAngles = conf->gI("LFSiftPP", "num_angles", 6 );
  28. std::string descriptorAlignment_s = conf->gS("LFSiftPP", "descriptor_alignment", "detector" );
  29. if ( descriptorAlignment_s == "detector" )
  30. descriptorAlignment = DALGIN_DETECTOR;
  31. else if ( descriptorAlignment_s == "multiple" )
  32. descriptorAlignment = DALIGN_MULTIPLE;
  33. else {
  34. fprintf (stderr, "LFSiftPP: descriptor alignment method unknown !\n");
  35. exit(-1);
  36. }
  37. normalizeFeature = conf->gB("LFSiftPP", "normalize_feature", false );
  38. std::ostringstream os;
  39. os << "siftpp_" << "l" << levels << "_o"
  40. << octaves << "_m" << minScale << "_t"
  41. << threshold << "_e" << edgeThreshold;
  42. if ( ! normalizeFeature )
  43. os << "_nd";
  44. }
  45. LFSiftPP::~LFSiftPP()
  46. {
  47. }
  48. int LFSiftPP::getDescSize () const
  49. {
  50. return 128;
  51. }
  52. int LFSiftPP::extractFeatures ( const NICE::Image & img, VVector & features,
  53. VVector & positions ) const
  54. {
  55. int O = octaves ;
  56. int const S = levels ;
  57. int const omin = first_octave;
  58. float const sigman = .5 ;
  59. float const sigma0 = 1.6 * powf(2.0f, 1.0f / S) ;
  60. if (O < 1) {
  61. O = std::max
  62. (int
  63. (std::floor
  64. (log2
  65. (std::min(img.width(),img.height()))) - omin -3), 1) ;
  66. }
  67. const unsigned char *blockimg = (unsigned char*) img.getPixelPointer();
  68. if ( blockimg == NULL ) {
  69. fprintf (stderr, "FATAL ERROR: do not use subimages !!\n");
  70. exit(-1);
  71. }
  72. float *blockimgfl = new float[img.width() * img.height()];
  73. for ( int k = 0 ; k < img.width() * img.height() ; k++ )
  74. blockimgfl[k] = blockimg[k];
  75. VL::Sift sift( blockimgfl, img.width(), img.height(),
  76. sigman, sigma0, O, S, omin, -1, S+1) ;
  77. sift.process ( blockimgfl, img.width(), img.height() );
  78. // compute keypoints
  79. sift.detectKeypoints(threshold, edgeThreshold) ;
  80. const int descr_size = 128;
  81. VL::float_t *descr_pt = new VL::float_t [descr_size];
  82. VL::float_t angles[4] ;
  83. int keypointCount = 0;
  84. const int maxKeyPoints = std::numeric_limits<int>::max();
  85. NICE::Vector feature (descr_size);
  86. for( VL::Sift::KeypointsConstIter iter = sift.keypointsBegin() ;
  87. iter != sift.keypointsEnd() ; ++iter, keypointCount++ )
  88. {
  89. if ( keypointCount >= maxKeyPoints ) break;
  90. if ( descriptorAlignment == DALGIN_DETECTOR )
  91. {
  92. if ( iter->s < minScale ) continue;
  93. int nangles = sift.computeKeypointOrientations(angles, *iter);
  94. for ( int i = 0 ; i < nangles ; i++ )
  95. {
  96. sift.computeKeypointDescriptor ( descr_pt, *iter, angles[i] );
  97. for ( int j = 0 ; j < descr_size ; j++ )
  98. {
  99. if ( isnan(descr_pt[j]) ) {
  100. fprintf (stderr, "Descriptor with NAN values !!\n");
  101. exit(-1);
  102. } else {
  103. feature[j] = descr_pt[j];
  104. }
  105. }
  106. NICE::Vector p (4);
  107. p[0] = iter->x;
  108. p[1] = iter->y;
  109. p[2] = iter->s;
  110. p[3] = angles[i];
  111. positions.push_back(p);
  112. if ( normalizeFeature )
  113. feature.normalizeL2();
  114. features.push_back ( feature );
  115. }
  116. } else if ( descriptorAlignment == DALIGN_MULTIPLE ) {
  117. double angle_step = 2 * M_PI / numAngles;
  118. double scale_step = (maxScale - minScale) / numScales;
  119. for ( int j = 0 ; j < numAngles ; j++ )
  120. {
  121. double scale = j * scale_step;
  122. for ( int i = 0 ; i < numAngles ; i++ )
  123. {
  124. double angle = angle_step * i;
  125. sift.computeKeypointDescriptor ( descr_pt, *iter, angle );
  126. for ( int j = 0 ; j < descr_size ; j++ )
  127. {
  128. if ( isnan(descr_pt[j]) ) {
  129. fprintf (stderr, "Descriptor with NAN values !!\n");
  130. exit(-1);
  131. } else {
  132. feature[j] = descr_pt[j];
  133. }
  134. }
  135. NICE::Vector p (4);
  136. p[0] = iter->x;
  137. p[1] = iter->y;
  138. p[2] = iter->s;
  139. p[3] = angles[i];
  140. positions.push_back(p);
  141. if ( normalizeFeature )
  142. feature.normalizeL2();
  143. features.push_back ( feature );
  144. }
  145. }
  146. }
  147. }
  148. fprintf (stderr, "LFSiftpp::convert: Number of Keypoints = %d\n", keypointCount );
  149. delete [] blockimgfl;
  150. delete [] descr_pt;
  151. if ( keypointCount <= 0 )
  152. fprintf (stderr, "FATAL ERROR: no keypoints found !!\n");
  153. return 0;
  154. }
  155. void LFSiftPP::visualizeFeatures ( NICE::Image & mark,
  156. const VVector & positions,
  157. size_t color ) const
  158. {
  159. fthrow("LFSiftPP::visualizeFeatures -- not yet implemented due to old ICE version.");
  160. // #ifdef NICE_USELIB_ICE
  161. // ice::Image mark_ice = ice::NewImg ( mark.width(),
  162. // mark.height(), 255 );
  163. // for ( size_t k = 0 ; k < positions.size() ; k++ )
  164. // {
  165. // const NICE::Vector & pos = positions[k];
  166. // ice::Matrix points ( 0, 2 );
  167. // const int size = 6;
  168. // points.Append ( ice::Vector(-size, -size) );
  169. // points.Append ( ice::Vector(-size, size) );
  170. // points.Append ( ice::Vector(size, size) );
  171. // points.Append ( ice::Vector(size, -size) );
  172. //
  173. // ice::Trafo tr;
  174. //
  175. // tr.Scale ( 0, 0, pos[2] );
  176. // tr.Rotate ( 0, 0, pos[3] );
  177. // tr.Shift ( pos[0], pos[1] );
  178. //
  179. // ice::TransformList(tr, points);
  180. //
  181. // for ( int j = 0 ; j < points.rows(); j++ )
  182. // {
  183. // if (points[j][0] < 0 )
  184. // points[j][0] = 0;
  185. // if (points[j][0] >= mark_ice->xsize)
  186. // points[j][0] = mark_ice->xsize - 1;
  187. // if (points[j][1] < 0 )
  188. // points[j][1] = 0;
  189. // if (points[j][1] >= mark_ice->ysize)
  190. // points[j][1] = mark_ice->ysize - 1;
  191. // }
  192. //
  193. // ice::DrawPolygon ( points, color, mark_ice );
  194. // }
  195. //
  196. // for ( unsigned int y = 0 ; y < mark.height(); y++ )
  197. // for ( unsigned int x = 0 ; x < mark.width(); x++ )
  198. // mark.setPixel(x,y, GetVal(mark_ice,x,y));
  199. // #else
  200. // cerr << "uses ice visualization, please install ice or change to NICE visualization" << endl;
  201. // #endif
  202. //TODO check this!
  203. }