RSCache.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "RSCache.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <sys/stat.h>
  5. #include <sys/types.h>
  6. #include "vislearning/baselib/Globals.h"
  7. #include "core/basics/StringTools.h"
  8. #include "core/basics/ossettings.h"
  9. #include <unistd.h>
  10. using namespace OBJREC;
  11. using namespace NICE;
  12. using namespace std;
  13. RSCache::RSCache ( const Config *conf, RegionSegmentationMethod *_rsmethod )
  14. {
  15. rsmethod = _rsmethod;
  16. cachedir = conf->gS ( "cache", "root", "/tmp/" );
  17. cachedir += "/regionsegmentation";
  18. forcedwrite = conf->gB ( "RSCache", "forcedwrite", false );
  19. // Verzeichnis anlegen
  20. struct stat info;
  21. if ( stat ( cachedir.c_str(), &info ) < 0 )
  22. {
  23. if ( mkdir ( cachedir.c_str(), 0755 ) < 0 )
  24. {
  25. cerr << "RSCache:: unable to create directory: " << cachedir << endl;
  26. exit ( -1 );
  27. }
  28. }
  29. }
  30. RSCache::~RSCache()
  31. {
  32. }
  33. int RSCache::segRegions ( const NICE::Image & img, NICE::Matrix & mask ) const
  34. {
  35. std::string filename = Globals::getCurrentImgFN();
  36. vector<string> mylistdir;
  37. // Zerlege Dateipfad (alles zwischen den /)
  38. StringTools::split ( filename, FILESEP, mylistdir );
  39. if ( mylistdir.size() < 2 )
  40. {
  41. cerr << "RSCache::getCacheFilename: unable to parse filename " << filename << endl;
  42. exit ( -1 );
  43. }
  44. std::string name = mylistdir[mylistdir.size()-1];
  45. vector<string> mylist;
  46. // Zerlege den Dateinamen in Namen und Endung
  47. StringTools::split ( name, '.', mylist );
  48. name = "";
  49. for ( uint k = 0 ; k < mylist.size()-1 ; k++ )
  50. if ( k == 0 )
  51. name = name + mylist[k];
  52. else
  53. name = name + "." + mylist[k];
  54. filename = cachedir + FILESEPSTRING + name;
  55. std::string imgname = filename+".ppm";
  56. filename += ".mask";
  57. int cn = -1;
  58. struct stat dummy;
  59. if ( stat ( filename.c_str(), &dummy ) < 0 || forcedwrite )
  60. {
  61. cn = rsmethod->segRegions ( img, mask );
  62. ofstream fout ( filename.c_str(), ios::out );
  63. fout << cn << endl;
  64. fout << mask;
  65. fout.close();
  66. }
  67. else
  68. {
  69. // read from file
  70. ifstream fin ( filename.c_str() );
  71. fin >> cn;
  72. fin >> mask;
  73. fin.close();
  74. }
  75. return cn;
  76. }
  77. int RSCache::segRegions ( const NICE::ColorImage & img, NICE::Matrix & mask ) const
  78. {
  79. std::string filename = Globals::getCurrentImgFN();
  80. vector<string> mylistdir;
  81. // Zerlege Dateipfad (alles zwischen den /)
  82. StringTools::split ( filename, FILESEP, mylistdir );
  83. if ( mylistdir.size() < 2 )
  84. {
  85. cerr << "RSCache::getCacheFilename: unable to parse filename " << filename << endl;
  86. exit ( -1 );
  87. }
  88. std::string name = mylistdir[mylistdir.size()-1];
  89. vector<string> mylist;
  90. // Zerlege den Dateinamen in Namen und Endung
  91. StringTools::split ( name, '.', mylist );
  92. name = "";
  93. for ( uint k = 0 ; k < mylist.size()-1 ; k++ )
  94. if ( k == 0 )
  95. name = name + mylist[k];
  96. else
  97. name = name + "." + mylist[k];
  98. filename = cachedir + FILESEPSTRING + name;
  99. std::string imgname = filename+".ppm";
  100. filename += ".mask";
  101. int cn = -1;
  102. struct stat dummy;
  103. if ( stat ( filename.c_str(), &dummy ) < 0 || forcedwrite )
  104. {
  105. cn = rsmethod->segRegions ( img, mask );
  106. ofstream fout ( filename.c_str(), ios::out );
  107. fout << cn << endl;
  108. fout << mask;
  109. fout.close();
  110. }
  111. else
  112. {
  113. // read from file
  114. ifstream fin ( filename.c_str() );
  115. fin >> cn;
  116. fin >> mask;
  117. fin.close();
  118. }
  119. return cn;
  120. }