RSMeanShift.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. RSMeanShift::RSMeanShift()
  12. {
  13. minimumRegionArea = 50;
  14. rangeBandwidth = 6.5;
  15. spatialBandwidth = 7;
  16. imageProc = new msImageProcessor();
  17. speedUpLevel = MED_SPEEDUP;
  18. }
  19. RSMeanShift::RSMeanShift ( const Config *conf )
  20. {
  21. imageProc = new msImageProcessor();
  22. minimumRegionArea = conf->gI ( "RSMeanShift", "MinimumRegionArea", 50 );
  23. rangeBandwidth = conf->gD ( "RSMeanShift", "RangeBandwidth", 6.5 );
  24. spatialBandwidth = conf->gI ( "RSMeanShift", "SpatialBandwidth", 7 );
  25. speedUpLvlStr = conf->gS ( "RSMeanShift", "SpeedUpLevel", "MEDIUM" );
  26. if ( speedUpLvlStr == "NONE" ) speedUpLevel = NO_SPEEDUP;
  27. else if ( speedUpLvlStr == "MEDIUM" ) speedUpLevel = MED_SPEEDUP;
  28. else if ( speedUpLvlStr == "HIGH" ) speedUpLevel = HIGH_SPEEDUP;
  29. else
  30. {
  31. cerr << "[err] RSMeanShift::RSMeanShift: Wrong SpeedUpLevel-Value (" << speedUpLvlStr << ") only NONE, MEDIUM and HIGH allowed! Take default Value (MED_SPEEDUP)" << endl;
  32. speedUpLevel = MED_SPEEDUP;
  33. }
  34. }
  35. RSMeanShift::~RSMeanShift()
  36. {
  37. delete imageProc;
  38. }
  39. int RSMeanShift::segRegions ( const NICE::Image & img, NICE::Matrix & mask ) const
  40. {
  41. ColorImage cimg ( img, img, img );
  42. return segRegions ( cimg, mask );
  43. }
  44. int RSMeanShift::segRegions ( const NICE::ColorImage & img, NICE::Matrix & mask ) const
  45. {
  46. clog << "[log] RSMeanShift::segRegions: EDISON-Implementation" << endl;
  47. /* >> ColorImage an EDISON uebergeben
  48. BEM: Dabei werden die Bilddaten als Pointer an die msImageProcessor::DefineImage Methode uebergeben.
  49. Der Parameter hat in der EDISON-Implementierung KEINEN 'const' Qualifier.
  50. */
  51. unsigned int width = img.width();
  52. unsigned int height = img.height();
  53. const unsigned int channels = 3;
  54. // notwendiger Bildspeicher
  55. const unsigned int imageSize = width * height * channels;
  56. byte* rawImageData = NULL;
  57. #ifdef NICE_USELIB_IPP
  58. /* ipp Speichert die Bilddaten anders, so dass das Bild pixelweise kopiert werden muss. */
  59. // Speicher fuer das Bild reservieren
  60. rawImageData = new byte[ imageSize ];
  61. // Pixelkoordinaten
  62. unsigned int x, y;
  63. x = y = 0;
  64. // "Breite" des Bildes im Speicher
  65. // unsigned int yStepWidth = width * 3;
  66. for ( unsigned int i = 0; i < imageSize; i += 3 )
  67. {
  68. rawImageData[ i ] = img.getPixel ( x, y, 0 );
  69. rawImageData[ i + 1 ] = img.getPixel ( x, y, 1 );
  70. rawImageData[ i + 2 ] = img.getPixel ( x, y, 2 );
  71. // neue Pixelkoordinaten berechnen
  72. if ( ( ++x ) == width ) {
  73. x = 0;
  74. y++;
  75. }
  76. }
  77. #else
  78. // img in nicht 'const' ColorImage kopieren
  79. ColorImage tempImage ( img );
  80. rawImageData = tempImage.getPixelPointer();
  81. #endif
  82. imageProc->DefineImage ( rawImageData, COLOR, height, width );
  83. /* >> Segementierung durchfuehren */
  84. imageProc->Segment ( spatialBandwidth, rangeBandwidth, minimumRegionArea, speedUpLevel );
  85. /* >> Ergebnisbild aus EDISON auslesen */
  86. byte resultRawImageData[ width * height * 3 ];
  87. imageProc->GetResults ( resultRawImageData );
  88. /* >> Maske erstellen */
  89. #ifdef NICE_USELIB_IPP
  90. delete[] rawImageData;
  91. rawImageData = 0;
  92. ColorImage ippTempImage ( width, height );
  93. x = y = 0;
  94. for ( unsigned int i = 0; i < imageSize; i += 3 )
  95. {
  96. ippTempImage.setPixel ( x, y, 0, resultRawImageData[ i ] );
  97. ippTempImage.setPixel ( x, y, 1, resultRawImageData[ i + 1 ] );
  98. ippTempImage.setPixel ( x, y, 2, resultRawImageData[ i + 2 ] );
  99. // neue Pixelkoordinaten berechnen
  100. if ( ( ++x ) == width ) {
  101. x = 0;
  102. y++;
  103. }
  104. }
  105. return transformSegmentedImg ( ippTempImage, mask );
  106. #else
  107. return transformSegmentedImg ( ColorImage ( resultRawImageData, width, height, GrayColorImageCommonImplementation::noAlignment ), mask );
  108. #endif
  109. }