RSSlic.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include "RSSlic.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #ifdef NICE_USELIB_OPENMP
  5. #include <omp.h>
  6. #endif
  7. #include "SLIC/SLIC.h"
  8. using namespace std;
  9. using namespace NICE;
  10. using namespace OBJREC;
  11. ///////////////////// ///////////////////// /////////////////////
  12. // CONSTRUCTORS / DESTRUCTORS
  13. ///////////////////// ///////////////////// /////////////////////
  14. RSSlic::RSSlic() : RegionSegmentationMethod()
  15. {
  16. this->spcount = -1;
  17. this->regionsize = 50;
  18. this->compactness = 10.0;
  19. this->b_lab = true;
  20. }
  21. RSSlic::RSSlic( const NICE::Config * _conf )
  22. {
  23. this->initFromConfig( _conf );
  24. }
  25. RSSlic::~RSSlic()
  26. {
  27. }
  28. void RSSlic::initFromConfig(const NICE::Config* _conf, const std::string & _confSection )
  29. {
  30. //first, call method for parent object
  31. OBJREC::RegionSegmentationMethod::initFromConfig( _conf );
  32. //now set own member variables
  33. this->spcount = _conf->gI( _confSection, "spcount", -1);
  34. this->regionsize = _conf->gI( _confSection, "regionsize", 50);
  35. this->compactness = _conf->gD( _confSection, "compactness", 10.0);
  36. this->b_lab = _conf->gB( _confSection, "useLAB", true);
  37. }
  38. ///////////////////// ///////////////////// /////////////////////
  39. // SEGMENTATION STUFF
  40. ///////////////////// ///////////////////// //////////////////
  41. int RSSlic::segRegions ( const NICE::Image & img, NICE::Matrix & mask) const
  42. {
  43. ColorImage cimg ( img, img, img );
  44. return segRegions ( cimg, mask );
  45. }
  46. int RSSlic::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. uint *inputImage = new uint[imageWidth*imageHeight];
  52. unsigned long int counter = 0;
  53. for ( unsigned int y = 0; y < imageHeight; y++)
  54. {
  55. for ( unsigned int x = 0; x < imageWidth; x++, counter++)
  56. {
  57. uint tmp = 255;
  58. for (int c = 0; c < 3; c++)
  59. {
  60. tmp = tmp<<8;
  61. tmp+=(unsigned char)img.getPixelQuick( x, y, c );
  62. }
  63. inputImage[counter] = tmp;
  64. }
  65. }
  66. int* labels = new int[imageWidth*imageHeight];
  67. // Eingabebild segmentieren
  68. SLIC slic;
  69. int numlabels = 0;
  70. int superpixelsize = regionsize;
  71. if(spcount > 0)
  72. {
  73. superpixelsize = 0.5+double(imageWidth*imageHeight)/double(spcount);
  74. }
  75. slic.DoSuperpixelSegmentation_ForGivenSuperpixelSize(inputImage, imageWidth, imageHeight, labels, numlabels, superpixelsize, compactness, b_lab);
  76. mask.resize(imageWidth, imageHeight);
  77. counter = 0;
  78. for ( unsigned int y = 0; y < imageHeight; y++)
  79. {
  80. for ( unsigned int x = 0; x < imageWidth; x++, counter++ )
  81. {
  82. mask(x, y) = labels[counter];
  83. }
  84. }
  85. /*
  86. counter = 0;
  87. for ( unsigned int y = 0; y < imageHeight; y++ )
  88. {
  89. for ( unsigned int x = 0; x < imageWidth; x++, counter++ )
  90. {
  91. for(int c = 0; c < 3; c++)
  92. {
  93. resultImage.setPixelQuick( x, y, c, labels[counter]);
  94. }
  95. }
  96. }
  97. transformSegmentedImg( resultImage, mask);*/
  98. // Speicher freigeben
  99. delete inputImage;
  100. inputImage = NULL;
  101. delete labels;
  102. labels = NULL;
  103. return numlabels;
  104. }
  105. ///////////////////// INTERFACE PERSISTENT /////////////////////
  106. // interface specific methods for store and restore
  107. ///////////////////// INTERFACE PERSISTENT /////////////////////
  108. void RSSlic::restore ( std::istream & is, int format )
  109. {
  110. //delete everything we knew so far...
  111. this->clear();
  112. if ( is.good() )
  113. {
  114. std::string tmp;
  115. is >> tmp; //class name
  116. if ( ! this->isStartTag( tmp, "RSSlic" ) )
  117. {
  118. std::cerr << " WARNING - attempt to restore RSSlic, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  119. throw;
  120. }
  121. bool b_endOfBlock ( false ) ;
  122. while ( !b_endOfBlock )
  123. {
  124. is >> tmp; // start of block
  125. if ( this->isEndTag( tmp, "RSSlic" ) )
  126. {
  127. b_endOfBlock = true;
  128. continue;
  129. }
  130. tmp = this->removeStartTag ( tmp );
  131. if ( tmp.compare("spcount") == 0 )
  132. {
  133. is >> this->spcount;
  134. is >> tmp; // end of block
  135. tmp = this->removeEndTag ( tmp );
  136. }
  137. else if ( tmp.compare("regionsize") == 0 )
  138. {
  139. is >> this->regionsize;
  140. is >> tmp; // end of block
  141. tmp = this->removeEndTag ( tmp );
  142. }
  143. else if ( tmp.compare("compactness") == 0 )
  144. {
  145. is >> this->compactness;
  146. is >> tmp; // end of block
  147. tmp = this->removeEndTag ( tmp );
  148. }
  149. else if ( tmp.compare("b_lab") == 0 )
  150. {
  151. is >> this->b_lab;
  152. is >> tmp; // end of block
  153. tmp = this->removeEndTag ( tmp );
  154. }
  155. else
  156. {
  157. std::cerr << "WARNING -- unexpected RSSlic object -- " << tmp << " -- for restoration... aborting" << std::endl;
  158. throw;
  159. }
  160. }
  161. }
  162. else
  163. {
  164. std::cerr << "RSSlic::restore -- InStream not initialized - restoring not possible!" << std::endl;
  165. throw;
  166. }
  167. }
  168. void RSSlic::store ( std::ostream & os, int format ) const
  169. {
  170. if (os.good())
  171. {
  172. // show starting point
  173. os << this->createStartTag( "RSSlic" ) << std::endl;
  174. os << this->createStartTag( "spcount" ) << std::endl;
  175. os << this->spcount << std::endl;
  176. os << this->createEndTag( "spcount" ) << std::endl;
  177. os << this->createStartTag( "regionsize" ) << std::endl;
  178. os << this->regionsize << std::endl;
  179. os << this->createEndTag( "regionsize" ) << std::endl;
  180. os << this->createStartTag( "compactness" ) << std::endl;
  181. os << this->compactness << std::endl;
  182. os << this->createEndTag( "compactness" ) << std::endl;
  183. os << this->createStartTag( "b_lab" ) << std::endl;
  184. os << this->b_lab << std::endl;
  185. os << this->createEndTag( "b_lab" ) << std::endl;
  186. // done
  187. os << this->createEndTag( "RSSlic" ) << std::endl;
  188. }
  189. else
  190. {
  191. std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
  192. }
  193. }
  194. void RSSlic::clear ()
  195. {
  196. }