RSGraphBased.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #include "RSGraphBased.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #ifdef NICE_USELIB_OPENMP
  5. #include <omp.h>
  6. #endif
  7. #include "felzenszwalb/image.h"
  8. #include "felzenszwalb/misc.h"
  9. #include "felzenszwalb/segment-image.h"
  10. #include "felzenszwalb/pnmfile.h"
  11. using namespace std;
  12. using namespace NICE;
  13. using namespace OBJREC;
  14. using namespace felzenszwalb;
  15. ///////////////////// ///////////////////// /////////////////////
  16. // CONSTRUCTORS / DESTRUCTORS
  17. ///////////////////// ///////////////////// /////////////////////
  18. RSGraphBased::RSGraphBased() : RegionSegmentationMethod()
  19. {
  20. this->parameter_k = 500;
  21. this->parameter_min_size = 200;
  22. this->parameter_sigma = 1.5;
  23. }
  24. RSGraphBased::RSGraphBased( const NICE::Config * _conf )
  25. {
  26. this->initFromConfig ( _conf );
  27. }
  28. RSGraphBased::~RSGraphBased()
  29. {
  30. }
  31. void RSGraphBased::initFromConfig( const NICE::Config* _conf, const std::string & _confSection )
  32. {
  33. //thr = _conf->gD( _confSection, "threshold", 5.0);
  34. this->parameter_k = _conf->gD(_confSection, "k", 500);
  35. this->parameter_min_size = _conf->gI(_confSection, "min_size", 200);
  36. this->parameter_sigma = _conf->gD(_confSection, "sigma", 1.5);
  37. }
  38. ///////////////////// ///////////////////// /////////////////////
  39. // SEGMENTATION STUFF
  40. ///////////////////// ///////////////////// //////////////////
  41. int RSGraphBased::segRegions ( const NICE::Image & img, NICE::Matrix & mask) const
  42. {
  43. NICE::ColorImage cimg(img, img, img);
  44. return segRegions(cimg, mask);
  45. }
  46. int RSGraphBased::segRegions ( const NICE::ColorImage & img, NICE::Matrix & mask) const
  47. {
  48. const unsigned int imageWidth = img.width();
  49. const unsigned int imageHeight = img.height();
  50. // Kopieren der Werte aus dem ColorImage -> image<rgb>
  51. image<rgb> *inputImage = new image<rgb>( imageWidth, imageHeight, false );
  52. #pragma omp parallel for
  53. for ( unsigned int y = 0; y < imageHeight; y++ )
  54. {
  55. for ( unsigned int x = 0; x < imageWidth; x++ )
  56. {
  57. imRef( inputImage, x, y ).r = img.getPixelQuick( x, y, 0 );
  58. imRef( inputImage, x, y ).g = img.getPixelQuick( x, y, 1 );
  59. imRef( inputImage, x, y ).b = img.getPixelQuick( x, y, 2 );
  60. }
  61. }
  62. // Eingabebild segmentieren
  63. int num_ccs; // Anzahl der segmentierten Regionen
  64. image<rgb> *tempResultImage = segment_image( inputImage, parameter_sigma, parameter_k, parameter_min_size, &num_ccs );
  65. // Kopieren der Werte aus dem image<rgb> -> ColorImage
  66. NICE::ColorImage resultImage( imageWidth, imageHeight );
  67. #pragma omp parallel for
  68. for ( unsigned int y = 0; y < imageHeight; y++ )
  69. {
  70. for ( unsigned int x = 0; x < imageWidth; x++ )
  71. {
  72. resultImage.setPixelQuick( x, y, 0, imRef(tempResultImage, x, y ).r );
  73. resultImage.setPixelQuick( x, y, 1, imRef(tempResultImage, x, y ).g );
  74. resultImage.setPixelQuick( x, y, 2, imRef(tempResultImage, x, y ).b );
  75. }
  76. }
  77. // Speicher freigeben
  78. delete inputImage;
  79. inputImage = NULL;
  80. delete tempResultImage;
  81. tempResultImage = NULL;
  82. transformSegmentedImg( resultImage, mask);
  83. return num_ccs;
  84. }
  85. ///////////////////// INTERFACE PERSISTENT /////////////////////
  86. // interface specific methods for store and restore
  87. ///////////////////// INTERFACE PERSISTENT /////////////////////
  88. void RSGraphBased::restore ( std::istream & is, int format )
  89. {
  90. //delete everything we knew so far...
  91. this->clear();
  92. if ( is.good() )
  93. {
  94. std::string tmp;
  95. is >> tmp; //class name
  96. if ( ! this->isStartTag( tmp, "RSGraphBased" ) )
  97. {
  98. std::cerr << " WARNING - attempt to restore RSGraphBased, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  99. throw;
  100. }
  101. bool b_endOfBlock ( false ) ;
  102. while ( !b_endOfBlock )
  103. {
  104. is >> tmp; // start of block
  105. if ( this->isEndTag( tmp, "RSGraphBased" ) )
  106. {
  107. b_endOfBlock = true;
  108. continue;
  109. }
  110. tmp = this->removeStartTag ( tmp );
  111. if ( tmp.compare("parameter_k") == 0 )
  112. {
  113. is >> this->parameter_k;
  114. is >> tmp; // end of block
  115. tmp = this->removeEndTag ( tmp );
  116. }
  117. else if ( tmp.compare("parameter_min_size") == 0 )
  118. {
  119. is >> this->parameter_min_size;
  120. is >> tmp; // end of block
  121. tmp = this->removeEndTag ( tmp );
  122. }
  123. else if ( tmp.compare("parameter_sigma") == 0 )
  124. {
  125. is >> this->parameter_sigma;
  126. is >> tmp; // end of block
  127. tmp = this->removeEndTag ( tmp );
  128. }
  129. else
  130. {
  131. std::cerr << "WARNING -- unexpected RSGraphBased object -- " << tmp << " -- for restoration... aborting" << std::endl;
  132. throw;
  133. }
  134. }
  135. }
  136. else
  137. {
  138. std::cerr << "RSGraphBased::restore -- InStream not initialized - restoring not possible!" << std::endl;
  139. throw;
  140. }
  141. }
  142. void RSGraphBased::store ( std::ostream & os, int format ) const
  143. {
  144. if (os.good())
  145. {
  146. // show starting point
  147. os << this->createStartTag( "RSGraphBased" ) << std::endl;
  148. os << this->createStartTag( "parameter_k" ) << std::endl;
  149. os << this->parameter_k << std::endl;
  150. os << this->createEndTag( "parameter_k" ) << std::endl;
  151. os << this->createStartTag( "parameter_min_size" ) << std::endl;
  152. os << this->parameter_min_size << std::endl;
  153. os << this->createEndTag( "parameter_min_size" ) << std::endl;
  154. os << this->createStartTag( "parameter_sigma" ) << std::endl;
  155. os << this->parameter_sigma << std::endl;
  156. os << this->createEndTag( "parameter_sigma" ) << std::endl;
  157. // done
  158. os << this->createEndTag( "RSGraphBased" ) << std::endl;
  159. }
  160. else
  161. {
  162. std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
  163. }
  164. }
  165. void RSGraphBased::clear ()
  166. {
  167. //TODO
  168. }