LocalFeatureRGBSift.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #ifdef NICE_USELIB_OPENMP
  2. #include <omp.h>
  3. #endif
  4. #include <iostream>
  5. #include "vislearning/features/localfeatures/sift.h"
  6. #include "vislearning/features/localfeatures/LocalFeatureRGBSift.h"
  7. using namespace OBJREC;
  8. using namespace std;
  9. using namespace NICE;
  10. ///////////////////// ///////////////////// /////////////////////
  11. // CONSTRUCTORS / DESTRUCTORS
  12. ///////////////////// ///////////////////// /////////////////////
  13. LocalFeatureRGBSift::LocalFeatureRGBSift() : LocalFeatureSift ()
  14. {
  15. //set deletemode variable of partent object
  16. this->deletemode = false;
  17. }
  18. LocalFeatureRGBSift::LocalFeatureRGBSift ( const NICE::Config * _conf )
  19. {
  20. this->initFromConfig( _conf );
  21. }
  22. LocalFeatureRGBSift::~LocalFeatureRGBSift()
  23. {
  24. }
  25. void LocalFeatureRGBSift::initFromConfig( const NICE::Config* _conf, const std::string& _confSection )
  26. {
  27. //first of all, call method of parent object
  28. LocalFeatureSift::initFromConfig( _conf );
  29. //set deletemode variable of partent object
  30. this->deletemode = false;
  31. }
  32. ///////////////////// ///////////////////// /////////////////////
  33. // FEATURE STUFF
  34. ///////////////////// ///////////////////// //////////////////
  35. int
  36. LocalFeatureRGBSift::getDescriptors ( const NICE::ColorImage & img, VVector & positions, VVector & descriptors ) const
  37. {
  38. this->sortPositions ( positions );
  39. descriptors.clear();
  40. for ( int i = 0; i < ( int ) positions.size(); i++ )
  41. {
  42. NICE::Vector v;
  43. descriptors.push_back ( v );
  44. }
  45. std::vector< NICE::VVector > desc ( 3 );
  46. #ifdef NICE_USELIB_OPENMP
  47. // check whether siftGPU should be used
  48. int numberOfCPU = omp_get_num_procs();
  49. int numberOfThreads = 0;
  50. if ( this->isGpuUsed() )
  51. {
  52. // in case of siftGPU it is possible to use one device
  53. numberOfThreads = 1;
  54. clog << "[log] LocalFeatureRGBSift: no multithreading" << endl;
  55. }
  56. else
  57. {
  58. // in case of siftpp it is possible to use all given cores
  59. numberOfThreads = numberOfCPU;
  60. clog << "[log] LocalFeatureRGBSift: multithreading with (max) " << numberOfCPU << " threads" << endl;
  61. }
  62. #endif
  63. #pragma omp parallel for num_threads(numberOfThreads)
  64. for ( int i = 0; i < 3; i++ )
  65. {
  66. NICE::Image tmp ( img.width(), img.height() );
  67. for ( int y = 0; y < img.height(); y++ )
  68. {
  69. for ( int x = 0; x < img.width(); x++ )
  70. {
  71. tmp.setPixel ( x, y, img.getPixel ( x, y, i ) );
  72. }
  73. }
  74. NICE::VVector pos = positions;
  75. this->computeDesc ( tmp, pos, desc[i] );
  76. }
  77. for ( int i = 0; i < 3; i++ )
  78. {
  79. assert ( desc[i].size() == descriptors.size() );
  80. if ( i == 0 ) {
  81. #pragma omp parallel for num_threads( numberOfCPU )
  82. for ( int j = 0; j < ( int ) desc[i].size(); j++ )
  83. {
  84. descriptors[j] = desc[i][j];
  85. }
  86. }
  87. else
  88. {
  89. #pragma omp parallel for num_threads( numberOfCPU )
  90. for ( int j = 0; j < ( int ) desc[i].size(); j++ )
  91. {
  92. descriptors[j].append ( desc[i][j] );
  93. }
  94. }
  95. }
  96. return positions.size();
  97. }
  98. ///////////////////// INTERFACE PERSISTENT /////////////////////
  99. // interface specific methods for store and restore
  100. ///////////////////// INTERFACE PERSISTENT /////////////////////
  101. void LocalFeatureRGBSift::restore ( std::istream & is, int format )
  102. {
  103. //delete everything we knew so far...
  104. this->clear();
  105. if ( is.good() )
  106. {
  107. std::string tmp;
  108. is >> tmp; //class name
  109. if ( ! this->isStartTag( tmp, "LocalFeatureRGBSift" ) )
  110. {
  111. std::cerr << " WARNING - attempt to restore LocalFeatureRGBSift, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  112. throw;
  113. }
  114. bool b_endOfBlock ( false ) ;
  115. while ( !b_endOfBlock )
  116. {
  117. is >> tmp; // start of block
  118. if ( this->isEndTag( tmp, "LocalFeatureRGBSift" ) )
  119. {
  120. b_endOfBlock = true;
  121. continue;
  122. }
  123. tmp = this->removeStartTag ( tmp );
  124. ///////////////////////////////
  125. // PARENT OBJECT //
  126. ///////////////////////////////
  127. if ( tmp.compare("LocalFeatureSift--Parent") == 0 )
  128. {
  129. // restore parent object
  130. LocalFeatureSift::restore(is);
  131. is >> tmp; // end of block
  132. tmp = this->removeEndTag ( tmp );
  133. }
  134. else
  135. {
  136. std::cerr << "WARNING -- unexpected LocalFeatureRGBSift object -- " << tmp << " -- for restoration... aborting" << std::endl;
  137. throw;
  138. }
  139. }
  140. }
  141. else
  142. {
  143. std::cerr << "LocalFeatureRGBSift::restore -- InStream not initialized - restoring not possible!" << std::endl;
  144. throw;
  145. }
  146. }
  147. void LocalFeatureRGBSift::store ( std::ostream & os, int format ) const
  148. {
  149. if (os.good())
  150. {
  151. // show starting point
  152. os << this->createStartTag( "LocalFeatureRGBSift" ) << std::endl;
  153. ///////////////////////////////
  154. // PARENT OBJECT //
  155. ///////////////////////////////
  156. os << this->createStartTag( "LocalFeatureSift--Parent" ) << std::endl;
  157. LocalFeatureSift::store(os);
  158. os << this->createStartTag( "LocalFeatureSift--Parent" ) << std::endl;
  159. // done
  160. os << this->createEndTag( "LocalFeatureRGBSift" ) << std::endl;
  161. }
  162. else
  163. {
  164. std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
  165. }
  166. }
  167. void LocalFeatureRGBSift::clear ()
  168. {
  169. }