123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- #include "RSCache.h"
- #include <iostream>
- #include <fstream>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include "vislearning/baselib/Globals.h"
- #include "core/basics/StringTools.h"
- #include "core/basics/ossettings.h"
- #include <unistd.h>
- using namespace OBJREC;
- using namespace NICE;
- using namespace std;
- RSCache::RSCache ( const Config *conf, RegionSegmentationMethod *_rsmethod )
- {
- rsmethod = _rsmethod;
- cachedir = conf->gS ( "cache", "root", "/tmp/" );
- cachedir += "/regionsegmentation";
- forcedwrite = conf->gB ( "RSCache", "forcedwrite", false );
- // Verzeichnis anlegen
- struct stat info;
- if ( stat ( cachedir.c_str(), &info ) < 0 )
- {
- if ( mkdir ( cachedir.c_str(), 0755 ) < 0 )
- {
- cerr << "RSCache:: unable to create directory: " << cachedir << endl;
- exit ( -1 );
- }
- }
- }
- RSCache::~RSCache()
- {
- }
- int RSCache::segRegions ( const NICE::Image & img, NICE::Matrix & mask ) const
- {
- std::string filename = Globals::getCurrentImgFN();
- vector<string> mylistdir;
- // Zerlege Dateipfad (alles zwischen den /)
- StringTools::split ( filename, FILESEP, mylistdir );
- if ( mylistdir.size() < 2 )
- {
- cerr << "RSCache::getCacheFilename: unable to parse filename " << filename << endl;
- exit ( -1 );
- }
- std::string name = mylistdir[mylistdir.size()-1];
- vector<string> mylist;
- // Zerlege den Dateinamen in Namen und Endung
- StringTools::split ( name, '.', mylist );
- name = "";
- for ( uint k = 0 ; k < mylist.size()-1 ; k++ )
- if ( k == 0 )
- name = name + mylist[k];
- else
- name = name + "." + mylist[k];
- filename = cachedir + FILESEPSTRING + name;
- std::string imgname = filename+".ppm";
- filename += ".mask";
- int cn = -1;
- struct stat dummy;
- if ( stat ( filename.c_str(), &dummy ) < 0 || forcedwrite )
- {
- cn = rsmethod->segRegions ( img, mask );
- ofstream fout ( filename.c_str(), ios::out );
- fout << cn << endl;
- fout << mask;
- fout.close();
- }
- else
- {
- // read from file
- ifstream fin ( filename.c_str() );
- fin >> cn;
- fin >> mask;
- fin.close();
- }
- return cn;
- }
- int RSCache::segRegions ( const NICE::ColorImage & img, NICE::Matrix & mask ) const
- {
- std::string filename = Globals::getCurrentImgFN();
- vector<string> mylistdir;
- // Zerlege Dateipfad (alles zwischen den /)
- StringTools::split ( filename, FILESEP, mylistdir );
- if ( mylistdir.size() < 2 )
- {
- cerr << "RSCache::getCacheFilename: unable to parse filename " << filename << endl;
- exit ( -1 );
- }
- std::string name = mylistdir[mylistdir.size()-1];
- vector<string> mylist;
- // Zerlege den Dateinamen in Namen und Endung
- StringTools::split ( name, '.', mylist );
- name = "";
- for ( uint k = 0 ; k < mylist.size()-1 ; k++ )
- if ( k == 0 )
- name = name + mylist[k];
- else
- name = name + "." + mylist[k];
- filename = cachedir + FILESEPSTRING + name;
- std::string imgname = filename+".ppm";
- filename += ".mask";
- int cn = -1;
- struct stat dummy;
- if ( stat ( filename.c_str(), &dummy ) < 0 || forcedwrite )
- {
- cn = rsmethod->segRegions ( img, mask );
- ofstream fout ( filename.c_str(), ios::out );
- fout << cn << endl;
- fout << mask;
- fout.close();
- }
- else
- {
- // read from file
- ifstream fin ( filename.c_str() );
- fin >> cn;
- fin >> mask;
- fin.close();
- }
- return cn;
- }
|