RSMeanShift.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #include "RSMeanShift.h"
  2. #ifdef NICE_USELIB_OPENMP
  3. #include <omp.h>
  4. #endif
  5. #include <iostream>
  6. #include <fstream>
  7. #include "core/basics/StringTools.h"
  8. using namespace std;
  9. using namespace NICE;
  10. using namespace OBJREC;
  11. void RSMeanShift::setSpeedUpLevel( const string& _speedUpLevelString )
  12. {
  13. if ( speedUpLvlString == "NONE" )
  14. this->speedUpLevel = NO_SPEEDUP;
  15. else if ( speedUpLvlString == "MEDIUM" )
  16. this->speedUpLevel = MED_SPEEDUP;
  17. else if ( speedUpLvlString == "HIGH" )
  18. this->speedUpLevel = HIGH_SPEEDUP;
  19. else
  20. {
  21. std::cerr << "[err] RSMeanShift::RSMeanShift: Wrong SpeedUpLevel-Value (" << speedUpLvlString << ") only NONE, MEDIUM and HIGH allowed! Take default Value (MED_SPEEDUP)" << std::endl;
  22. this->speedUpLevel = MED_SPEEDUP;
  23. }
  24. }
  25. ///////////////////// ///////////////////// /////////////////////
  26. // CONSTRUCTORS / DESTRUCTORS
  27. ///////////////////// ///////////////////// /////////////////////
  28. RSMeanShift::RSMeanShift() : RegionSegmentationMethod()
  29. {
  30. this->minimumRegionArea = 50;
  31. this->rangeBandwidth = 6.5;
  32. this->spatialBandwidth = 7;
  33. this->imageProc = new msImageProcessor();
  34. this->speedUpLvlString = "MEDIUM";
  35. this->setSpeedUpLevel( this->speedUpLvlString );
  36. }
  37. RSMeanShift::RSMeanShift ( const Config * _conf )
  38. {
  39. this->initFromConfig( _conf );
  40. }
  41. RSMeanShift::~RSMeanShift()
  42. {
  43. if ( this->imageProc != NULL )
  44. delete imageProc;
  45. this->imageProc = NULL;
  46. }
  47. void RSMeanShift::initFromConfig( const NICE::Config* _conf, const std::string & _confSection )
  48. {
  49. //first, call method for parent object
  50. OBJREC::RegionSegmentationMethod::initFromConfig( _conf );
  51. //now set own member variables
  52. this->imageProc = new msImageProcessor();
  53. this->minimumRegionArea = _conf->gI ( _confSection, "MinimumRegionArea", 50 );
  54. this->rangeBandwidth = _conf->gD ( _confSection, "RangeBandwidth", 6.5 );
  55. this->spatialBandwidth = _conf->gI ( _confSection, "SpatialBandwidth", 7 );
  56. this->speedUpLvlString = _conf->gS ( _confSection, "SpeedUpLevel", "MEDIUM" );
  57. this->setSpeedUpLevel( this->speedUpLvlString );
  58. }
  59. ///////////////////// ///////////////////// /////////////////////
  60. // SEGMENTATION STUFF
  61. ///////////////////// ///////////////////// //////////////////
  62. int RSMeanShift::segRegions ( const NICE::Image & img, NICE::Matrix & mask ) const
  63. {
  64. ColorImage cimg ( img, img, img );
  65. return segRegions ( cimg, mask );
  66. }
  67. int RSMeanShift::segRegions ( const NICE::ColorImage & img, NICE::Matrix & mask ) const
  68. {
  69. clog << "[log] RSMeanShift::segRegions: EDISON-Implementation" << endl;
  70. /* >> ColorImage an EDISON uebergeben
  71. BEM: Dabei werden die Bilddaten als Pointer an die msImageProcessor::DefineImage Methode uebergeben.
  72. Der Parameter hat in der EDISON-Implementierung KEINEN 'const' Qualifier.
  73. */
  74. unsigned int width = img.width();
  75. unsigned int height = img.height();
  76. const unsigned int channels = 3;
  77. // notwendiger Bildspeicher
  78. const unsigned int imageSize = width * height * channels;
  79. byte* rawImageData = NULL;
  80. #ifdef NICE_USELIB_IPP
  81. /* ipp Speichert die Bilddaten anders, so dass das Bild pixelweise kopiert werden muss. */
  82. // Speicher fuer das Bild reservieren
  83. rawImageData = new byte[ imageSize ];
  84. // Pixelkoordinaten
  85. unsigned int x, y;
  86. x = y = 0;
  87. // "Breite" des Bildes im Speicher
  88. // unsigned int yStepWidth = width * 3;
  89. for ( unsigned int i = 0; i < imageSize; i += 3 )
  90. {
  91. rawImageData[ i ] = img.getPixel ( x, y, 0 );
  92. rawImageData[ i + 1 ] = img.getPixel ( x, y, 1 );
  93. rawImageData[ i + 2 ] = img.getPixel ( x, y, 2 );
  94. // neue Pixelkoordinaten berechnen
  95. if ( ( ++x ) == width ) {
  96. x = 0;
  97. y++;
  98. }
  99. }
  100. #else
  101. // img in nicht 'const' ColorImage kopieren
  102. ColorImage tempImage ( img );
  103. rawImageData = tempImage.getPixelPointer();
  104. #endif
  105. imageProc->DefineImage ( rawImageData, COLOR, height, width );
  106. /* >> Segementierung durchfuehren */
  107. imageProc->Segment ( spatialBandwidth, rangeBandwidth, minimumRegionArea, speedUpLevel );
  108. /* >> Ergebnisbild aus EDISON auslesen */
  109. byte resultRawImageData[ width * height * 3 ];
  110. imageProc->GetResults ( resultRawImageData );
  111. /* >> Maske erstellen */
  112. #ifdef NICE_USELIB_IPP
  113. delete[] rawImageData;
  114. rawImageData = 0;
  115. NICE::ColorImage ippTempImage ( width, height );
  116. x = y = 0;
  117. for ( unsigned int i = 0; i < imageSize; i += 3 )
  118. {
  119. ippTempImage.setPixel ( x, y, 0, resultRawImageData[ i ] );
  120. ippTempImage.setPixel ( x, y, 1, resultRawImageData[ i + 1 ] );
  121. ippTempImage.setPixel ( x, y, 2, resultRawImageData[ i + 2 ] );
  122. // neue Pixelkoordinaten berechnen
  123. if ( ( ++x ) == width ) {
  124. x = 0;
  125. y++;
  126. }
  127. }
  128. return transformSegmentedImg ( ippTempImage, mask );
  129. #else
  130. return transformSegmentedImg ( ColorImage ( resultRawImageData, width, height, GrayColorImageCommonImplementation::noAlignment ), mask );
  131. #endif
  132. }
  133. ///////////////////// INTERFACE PERSISTENT /////////////////////
  134. // interface specific methods for store and restore
  135. ///////////////////// INTERFACE PERSISTENT /////////////////////
  136. void RSMeanShift::restore ( std::istream & is, int format )
  137. {
  138. //delete everything we knew so far...
  139. this->clear();
  140. if ( is.good() )
  141. {
  142. std::string tmp;
  143. is >> tmp; //class name
  144. if ( ! this->isStartTag( tmp, "RSMeanShift" ) )
  145. {
  146. std::cerr << " WARNING - attempt to restore RSMeanShift, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  147. throw;
  148. }
  149. bool b_endOfBlock ( false ) ;
  150. while ( !b_endOfBlock )
  151. {
  152. is >> tmp; // start of block
  153. if ( this->isEndTag( tmp, "RSMeanShift" ) )
  154. {
  155. b_endOfBlock = true;
  156. continue;
  157. }
  158. tmp = this->removeStartTag ( tmp );
  159. if ( tmp.compare("minimumRegionArea") == 0 )
  160. {
  161. is >> minimumRegionArea;
  162. is >> tmp; // end of block
  163. tmp = this->removeEndTag ( tmp );
  164. }
  165. else if ( tmp.compare("rangeBandwidth") == 0 )
  166. {
  167. is >> rangeBandwidth;
  168. is >> tmp; // end of block
  169. tmp = this->removeEndTag ( tmp );
  170. }
  171. else if ( tmp.compare("spatialBandwidth") == 0 )
  172. {
  173. is >> spatialBandwidth;
  174. is >> tmp; // end of block
  175. tmp = this->removeEndTag ( tmp );
  176. }
  177. else if ( tmp.compare("imageProc") == 0 )
  178. {
  179. //nothing to read actually :)
  180. imageProc = new msImageProcessor();
  181. is >> tmp; // end of block
  182. tmp = this->removeEndTag ( tmp );
  183. }
  184. else if ( tmp.compare("speedUpLvlString") == 0 )
  185. {
  186. is >> speedUpLvlString;
  187. this->setSpeedUpLevel( this->speedUpLvlString );
  188. is >> tmp; // end of block
  189. tmp = this->removeEndTag ( tmp );
  190. }
  191. else
  192. {
  193. std::cerr << "WARNING -- unexpected RSMeanShift object -- " << tmp << " -- for restoration... aborting" << std::endl;
  194. throw;
  195. }
  196. }
  197. }
  198. else
  199. {
  200. std::cerr << "RSMeanShift::restore -- InStream not initialized - restoring not possible!" << std::endl;
  201. throw;
  202. }
  203. }
  204. void RSMeanShift::store ( std::ostream & os, int format ) const
  205. {
  206. if (os.good())
  207. {
  208. // show starting point
  209. os << this->createStartTag( "RSMeanShift" ) << std::endl;
  210. os << this->createStartTag( "minimumRegionArea" ) << std::endl;
  211. os << this->minimumRegionArea << std::endl;
  212. os << this->createEndTag( "minimumRegionArea" ) << std::endl;
  213. os << this->createStartTag( "rangeBandwidth" ) << std::endl;
  214. os << this->rangeBandwidth << std::endl;
  215. os << this->createEndTag( "rangeBandwidth" ) << std::endl;
  216. os << this->createStartTag( "spatialBandwidth" ) << std::endl;
  217. os << this->spatialBandwidth << std::endl;
  218. os << this->createEndTag( "spatialBandwidth" ) << std::endl;
  219. os << this->createStartTag( "imageProc" ) << std::endl;
  220. //nothing to write actually :)
  221. os << this->createEndTag( "imageProc" ) << std::endl;
  222. os << this->createStartTag( "speedUpLvlString" ) << std::endl;
  223. os << this->speedUpLvlString << std::endl;
  224. os << this->createEndTag( "speedUpLvlString" ) << std::endl;
  225. // done
  226. os << this->createEndTag( "RSMeanShift" ) << std::endl;
  227. }
  228. else
  229. {
  230. std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
  231. }
  232. }
  233. void RSMeanShift::clear ()
  234. {
  235. if ( this->imageProc != NULL )
  236. delete imageProc;
  237. this->imageProc = NULL;
  238. }