LocalFeatureOpponnentSift.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include <iostream>
  2. #include "vislearning/features/localfeatures/sift.h"
  3. #include "vislearning/features/localfeatures/LocalFeatureOpponnentSift.h"
  4. using namespace OBJREC;
  5. using namespace std;
  6. using namespace NICE;
  7. ///////////////////// ///////////////////// /////////////////////
  8. // CONSTRUCTORS / DESTRUCTORS
  9. ///////////////////// ///////////////////// /////////////////////
  10. LocalFeatureOpponnentSift::LocalFeatureOpponnentSift() : LocalFeatureRGBSift ()
  11. {
  12. }
  13. LocalFeatureOpponnentSift::LocalFeatureOpponnentSift ( const NICE::Config * _conf )
  14. {
  15. this->initFromConfig( _conf );
  16. }
  17. LocalFeatureOpponnentSift::~LocalFeatureOpponnentSift()
  18. {
  19. }
  20. void LocalFeatureOpponnentSift::initFromConfig( const NICE::Config* _conf, const std::string& _confSection )
  21. {
  22. //first of all, call method of parent object
  23. LocalFeatureRGBSift::initFromConfig( _conf );
  24. }
  25. ///////////////////// ///////////////////// /////////////////////
  26. // FEATURE STUFF
  27. ///////////////////// ///////////////////// //////////////////
  28. int LocalFeatureOpponnentSift::getDescriptors ( const NICE::ColorImage & cimg,
  29. VVector & positions,
  30. VVector & descriptors ) const
  31. {
  32. NICE::ColorImage opimg ( cimg.width(), cimg.height() );
  33. for ( int y = 0; y < ( int ) cimg.height(); y++ )
  34. {
  35. for ( int x = 0; x < ( int ) cimg.width(); x++ )
  36. {
  37. // Farbkanaele auslesen
  38. int r = cimg.getPixel ( x, y, 0 );
  39. int g = cimg.getPixel ( x, y, 1 );
  40. int b = cimg.getPixel ( x, y, 2 );
  41. // Transformation in den Opponent Farbraum nach Van de Sande
  42. int o1 = ( int ) ( ( ( double ) ( r - g ) + 255.0 ) / 2.0 );
  43. int o2 = ( int ) ( ( ( double ) ( r + g - b ) + 510.0 ) / 4.0 );
  44. int o3 = ( int ) ( ( double ) ( r + g + b ) / 3.0 );
  45. opimg.setPixel ( x, y, o1, o2, o3 );
  46. }
  47. }
  48. return LocalFeatureRGBSift::getDescriptors ( opimg, positions, descriptors );
  49. }
  50. ///////////////////// INTERFACE PERSISTENT /////////////////////
  51. // interface specific methods for store and restore
  52. ///////////////////// INTERFACE PERSISTENT /////////////////////
  53. void LocalFeatureOpponnentSift::restore ( std::istream & is, int format )
  54. {
  55. //delete everything we knew so far...
  56. this->clear();
  57. if ( is.good() )
  58. {
  59. std::string tmp;
  60. is >> tmp; //class name
  61. if ( ! this->isStartTag( tmp, "LocalFeatureOpponnentSift" ) )
  62. {
  63. std::cerr << " WARNING - attempt to restore LocalFeatureOpponnentSift, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  64. throw;
  65. }
  66. bool b_endOfBlock ( false ) ;
  67. while ( !b_endOfBlock )
  68. {
  69. is >> tmp; // start of block
  70. if ( this->isEndTag( tmp, "LocalFeatureOpponnentSift" ) )
  71. {
  72. b_endOfBlock = true;
  73. continue;
  74. }
  75. tmp = this->removeStartTag ( tmp );
  76. ///////////////////////////////
  77. // PARENT OBJECT //
  78. ///////////////////////////////
  79. if ( tmp.compare("LocalFeatureRGBSift--Parent") == 0 )
  80. {
  81. // restore parent object
  82. LocalFeatureRGBSift::restore(is);
  83. is >> tmp; // end of block
  84. tmp = this->removeEndTag ( tmp );
  85. }
  86. else
  87. {
  88. std::cerr << "WARNING -- unexpected LocalFeatureOpponnentSift object -- " << tmp << " -- for restoration... aborting" << std::endl;
  89. throw;
  90. }
  91. }
  92. }
  93. else
  94. {
  95. std::cerr << "LocalFeatureOpponnentSift::restore -- InStream not initialized - restoring not possible!" << std::endl;
  96. throw;
  97. }
  98. }
  99. void LocalFeatureOpponnentSift::store ( std::ostream & os, int format ) const
  100. {
  101. if (os.good())
  102. {
  103. // show starting point
  104. os << this->createStartTag( "LocalFeatureOpponnentSift" ) << std::endl;
  105. ///////////////////////////////
  106. // PARENT OBJECT //
  107. ///////////////////////////////
  108. os << this->createStartTag( "LocalFeatureRGBSift--Parent" ) << std::endl;
  109. LocalFeatureRGBSift::store(os);
  110. os << this->createStartTag( "LocalFeatureRGBSift--Parent" ) << std::endl;
  111. // done
  112. os << this->createEndTag( "LocalFeatureOpponnentSift" ) << std::endl;
  113. }
  114. else
  115. {
  116. std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
  117. }
  118. }
  119. void LocalFeatureOpponnentSift::clear ()
  120. {
  121. }