testSegmentation.txt 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * @file testSegmentation.cpp
  3. * @brief test segmentation algorithm
  4. * @author Björn Fröhlich
  5. * @date 01/20/2010
  6. */
  7. #include <core/basics/Config.h>
  8. #include <segmentation/RSMeanShift.h>
  9. #include <segmentation/RSGraphBased.h>
  10. #include <segmentation/RSSlic.h>
  11. #include <core/imagedisplay/OverlayColors.h>
  12. #include "core/basics/Timer.h"
  13. #include <core/imagedisplay/ImageDisplay.h>
  14. using namespace OBJREC;
  15. using namespace NICE;
  16. using namespace std;
  17. int main ( int argc, char **argv )
  18. {
  19. if ( argc < 2 )
  20. {
  21. cerr << "Bitte Bild angeben" << endl;
  22. return -1;
  23. }
  24. int type = 0;
  25. RegionSegmentationMethod *rg = NULL;
  26. if ( argc >= 3 )
  27. {
  28. string tmp;
  29. tmp += argv[2];
  30. if ( argc >= 4 )
  31. {
  32. string configfile;
  33. configfile += argv[3];
  34. Config conf ( configfile );
  35. if ( tmp == "f" )
  36. {
  37. rg = new RSGraphBased ( &conf );
  38. }
  39. else if ( tmp == "m" ) {
  40. rg = new RSMeanShift ( &conf );
  41. }
  42. else if ( tmp == "s" ) {
  43. rg = new RSSlic ( &conf );
  44. }
  45. else
  46. {
  47. cout << "please choose between \nm: MeanShift and \nf: Felzenswalb \nas a segmentation method" << endl;
  48. return -1;
  49. }
  50. }
  51. else {
  52. if ( tmp == "f" )
  53. {
  54. rg = new RSGraphBased();
  55. }
  56. else if ( tmp == "m" )
  57. {
  58. rg = new RSMeanShift();
  59. }
  60. else if ( tmp == "s" ) {
  61. rg = new RSSlic ();
  62. }
  63. else
  64. {
  65. cout << "please choose between \nm: MeanShift and \nf: Felzenswalb \nas a segmentation method" << endl;
  66. return -1;
  67. }
  68. }
  69. }
  70. else
  71. {
  72. rg = new RSGraphBased();
  73. }
  74. string filename;
  75. filename += argv[1];
  76. NICE::ColorImage img;
  77. img.read ( filename );
  78. Matrix result;
  79. Timer timer;
  80. timer.start();
  81. rg->segRegions ( img, result );
  82. timer.stop();
  83. cerr << "segmentation finished in: " << timer.getLastAbsolute() << " seconds" << endl;
  84. //rg->visualizeGraphRepresentation(img,result);
  85. Image overlay ( img.width(), img.height() );
  86. double maxval = 0.0;
  87. for ( int y = 0; y < img.height(); y++ )
  88. {
  89. for ( int x = 0; x < img.width(); x++ )
  90. {
  91. int val = ( ( int ) result ( x, y ) + 1 ) % 256;
  92. overlay.setPixel ( x, y, val );
  93. maxval = std::max ( result ( x, y ), maxval );
  94. }
  95. }
  96. cout << maxval << " different regions found" << endl;
  97. vector<int> color (3);
  98. color[0] = 255;
  99. ColorImage marked(img.width(), img.height());
  100. rg->markContours(img,result,color,marked);
  101. NICE::showImage ( marked, "Segmentation Result" );
  102. marked.write("segmented.ppm");
  103. return 0;
  104. }