RSSlic.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. RSSlic::RSSlic()
  12. {
  13. spcount = -1;
  14. regionsize = 50;
  15. compactness = 10.0;
  16. lab = true;
  17. }
  18. RSSlic::RSSlic(const Config *conf )
  19. {
  20. spcount = conf->gI("RSSlic", "spcount", -1);
  21. regionsize = conf->gI("RSSlic", "regionsize", 50);
  22. compactness = conf->gD("RSSlic", "compactness", 10.0);
  23. lab = conf->gB("RSSlic", "useLAB", true);
  24. }
  25. RSSlic::~RSSlic()
  26. {
  27. }
  28. int RSSlic::segRegions ( const NICE::Image & img, NICE::Matrix & mask) const
  29. {
  30. cerr << "not implemented yet" << endl;
  31. return -1;
  32. }
  33. int RSSlic::segRegions ( const NICE::ColorImage & img, NICE::Matrix & mask) const
  34. {
  35. const unsigned int imageWidth = img.width();
  36. const unsigned int imageHeight = img.height();
  37. // Kopieren der Werte aus dem ColorImage -> image<rgb>
  38. uint *inputImage = new uint[imageWidth*imageHeight];
  39. unsigned long int counter = 0;
  40. for ( unsigned int y = 0; y < imageHeight; y++)
  41. {
  42. for ( unsigned int x = 0; x < imageWidth; x++, counter++)
  43. {
  44. uint tmp = 255;
  45. for (int c = 0; c < 3; c++)
  46. {
  47. tmp = tmp<<8;
  48. tmp+=(unsigned char)img.getPixelQuick( x, y, c );
  49. }
  50. inputImage[counter] = tmp;
  51. }
  52. }
  53. int* labels = new int[imageWidth*imageHeight];
  54. // Eingabebild segmentieren
  55. SLIC slic;
  56. int numlabels = 0;
  57. int superpixelsize = regionsize;
  58. if(spcount > 0)
  59. {
  60. superpixelsize = 0.5+double(imageWidth*imageHeight)/double(spcount);
  61. }
  62. slic.DoSuperpixelSegmentation_ForGivenSuperpixelSize(inputImage, imageWidth, imageHeight, labels, numlabels, superpixelsize, compactness, lab);
  63. mask.resize(imageWidth, imageHeight);
  64. counter = 0;
  65. for ( unsigned int y = 0; y < imageHeight; y++)
  66. {
  67. for ( unsigned int x = 0; x < imageWidth; x++, counter++ )
  68. {
  69. mask(x, y) = labels[counter];
  70. }
  71. }
  72. /*
  73. counter = 0;
  74. for ( unsigned int y = 0; y < imageHeight; y++ )
  75. {
  76. for ( unsigned int x = 0; x < imageWidth; x++, counter++ )
  77. {
  78. for(int c = 0; c < 3; c++)
  79. {
  80. resultImage.setPixelQuick( x, y, c, labels[counter]);
  81. }
  82. }
  83. }
  84. transformSegmentedImg( resultImage, mask);*/
  85. // Speicher freigeben
  86. delete inputImage;
  87. inputImage = NULL;
  88. delete labels;
  89. labels = NULL;
  90. return numlabels;
  91. }