RSMeanShift.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.width(), img.height() );
  42. for(int y = 0; y < img.height(); y++)
  43. {
  44. for(int x = 0; x < img.width(); x++)
  45. {
  46. for(int c = 0; c < 3; c++)
  47. {
  48. cimg.setPixelQuick(x,y,c,img.getPixelQuick(x,y));
  49. }
  50. }
  51. }
  52. return segRegions ( cimg, mask );
  53. }
  54. int RSMeanShift::segRegions ( const NICE::ColorImage & img, NICE::Matrix & mask ) const
  55. {
  56. clog << "[log] RSMeanShift::segRegions: EDISON-Implementation" << endl;
  57. /* >> ColorImage an EDISON uebergeben
  58. BEM: Dabei werden die Bilddaten als Pointer an die msImageProcessor::DefineImage Methode uebergeben.
  59. Der Parameter hat in der EDISON-Implementierung KEINEN 'const' Qualifier.
  60. */
  61. unsigned int width = img.width();
  62. unsigned int height = img.height();
  63. const unsigned int channels = 3;
  64. // notwendiger Bildspeicher
  65. const unsigned int imageSize = width * height * channels;
  66. byte* rawImageData = NULL;
  67. #ifdef NICE_USELIB_IPP
  68. /* ipp Speichert die Bilddaten anders, so dass das Bild pixelweise kopiert werden muss. */
  69. // Speicher fuer das Bild reservieren
  70. rawImageData = new byte[ imageSize ];
  71. // Pixelkoordinaten
  72. unsigned int x, y;
  73. x = y = 0;
  74. // "Breite" des Bildes im Speicher
  75. // unsigned int yStepWidth = width * 3;
  76. for ( unsigned int i = 0; i < imageSize; i += 3 )
  77. {
  78. rawImageData[ i ] = img.getPixel ( x, y, 0 );
  79. rawImageData[ i + 1 ] = img.getPixel ( x, y, 1 );
  80. rawImageData[ i + 2 ] = img.getPixel ( x, y, 2 );
  81. // neue Pixelkoordinaten berechnen
  82. if ( ( ++x ) == width ) {
  83. x = 0;
  84. y++;
  85. }
  86. }
  87. #else
  88. // img in nicht 'const' ColorImage kopieren
  89. ColorImage tempImage ( img );
  90. rawImageData = tempImage.getPixelPointer();
  91. #endif
  92. imageProc->DefineImage ( rawImageData, COLOR, height, width );
  93. /* >> Segementierung durchfuehren */
  94. imageProc->Segment ( spatialBandwidth, rangeBandwidth, minimumRegionArea, speedUpLevel );
  95. /* >> Ergebnisbild aus EDISON auslesen */
  96. byte resultRawImageData[ width * height * 3 ];
  97. imageProc->GetResults ( resultRawImageData );
  98. /* >> Maske erstellen */
  99. #ifdef NICE_USELIB_IPP
  100. delete[] rawImageData;
  101. rawImageData = 0;
  102. ColorImage ippTempImage ( width, height );
  103. x = y = 0;
  104. for ( unsigned int i = 0; i < imageSize; i += 3 )
  105. {
  106. ippTempImage.setPixel ( x, y, 0, resultRawImageData[ i ] );
  107. ippTempImage.setPixel ( x, y, 1, resultRawImageData[ i + 1 ] );
  108. ippTempImage.setPixel ( x, y, 2, resultRawImageData[ i + 2 ] );
  109. // neue Pixelkoordinaten berechnen
  110. if ( ( ++x ) == width ) {
  111. x = 0;
  112. y++;
  113. }
  114. }
  115. return transformSegmentedImg ( ippTempImage, mask );
  116. #else
  117. return transformSegmentedImg ( ColorImage ( resultRawImageData, width, height, GrayColorImageCommonImplementation::noAlignment ), mask );
  118. #endif
  119. }