123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- /**
- * @file SemanticSegmentation.cpp
- * @brief abstract interface for semantic segmentation algorithms
- * @author Erik Rodner and Sven Sickert
- * @date 03/19/2009
- */
- #include <iostream>
- #include "SemanticSegmentation.h"
- #include "vislearning/baselib/Preprocess.h"
- #include "vislearning/baselib/Globals.h"
- #include "core/image/MultiChannelImage3DT.h"
- #include "core/basics/StringTools.h"
- using namespace OBJREC;
- using namespace std;
- using namespace NICE;
- void SemanticSegmentation::convertLSetToSparseExamples ( Examples &examples, LabeledSetVector &lvec )
- {
- #ifdef DEBUG_PRINTS
- cout << "SemSegRegionBased::convertLSetToExamples starts" << endl;
- #endif
- for ( map< int, vector<NICE::Vector *> >::iterator iter = lvec.begin(); iter != lvec.end(); ++iter )
- {
- for ( int j = 0; j < ( int ) iter->second.size(); j++ )
- {
- Vector &tmp = * ( iter->second[j] );
- int dim = tmp.size();
- SparseVector *vec = new SparseVector ( dim );
- for ( int j = 0; j < dim; j++ )
- {
- if ( tmp[j] != 0.0 )
- {
- ( *vec ) [j] = tmp[j];
- }
- }
- Example ex;
- ex.svec = vec;
- examples.push_back ( pair<int, Example> ( iter->first, ex ) );
- }
- }
- lvec.clear();
- #ifdef DEBUG_PRINTS
- cout << "SemSegRegionBased::convertLSetToExamples finished" << endl;
- #endif
- }
- void SemanticSegmentation::convertLSetToExamples ( Examples &examples, LabeledSetVector &lvec )
- {
- #ifdef DEBUG_PRINTS
- cout << "SemSegRegionBased::convertLSetToExamples starts" << endl;
- #endif
- for ( map< int, vector<NICE::Vector *> >::iterator iter = lvec.begin(); iter != lvec.end(); ++iter )
- {
- for ( int j = 0; j < ( int ) iter->second.size(); j++ )
- {
- NICE::Vector *vec = new NICE::Vector ( * ( iter->second[j] ) );
- Example ex ( vec );
- examples.push_back ( pair<int, Example> ( iter->first, ex ) );
- }
- }
- lvec.clear();
- #ifdef DEBUG_PRINTS
- cout << "SemSegRegionBased::convertLSetToExamples finished" << endl;
- #endif
- }
- void SemanticSegmentation::convertExamplesToLSet ( Examples &examples, LabeledSetVector &lvec )
- {
- #ifdef DEBUG_PRINTS
- cout << "SemSegRegionBased::convertExamplesToLSet starts" << endl;
- #endif
- lvec.clear();
- for ( int i = 0; i < ( int ) examples.size(); i++ )
- {
- if ( examples[i].second.vec != NULL )
- {
- lvec.add ( examples[i].first, *examples[i].second.vec );
- delete examples[i].second.vec;
- examples[i].second.vec = NULL;
- }
- else
- {
- if ( examples[i].second.svec != NULL )
- {
- throw ( "Transform SVEC to VEC not yet implemented" );
- }
- else
- {
- throw ( "no features for LabeledSet" );
- }
- }
- }
- examples.clear();
- #ifdef DEBUG_PRINTS
- cout << "SemSegRegionBased::convertExamplesToLSet finished" << endl;
- #endif
- }
- void SemanticSegmentation::convertExamplesToVVector ( VVector &feats, Examples &examples, vector<int> &label )
- {
- #ifdef DEBUG_PRINTS
- cout << "SemSegRegionBased::convertExamplesToVVector starts" << endl;
- #endif
- feats.clear();
- label.clear();
- for ( int i = 0; i < ( int ) examples.size(); i++ )
- {
- label.push_back ( examples[i].first );
- feats.push_back ( *examples[i].second.vec );
- delete examples[i].second.vec;
- examples[i].second.vec = NULL;
- }
- examples.clear();
- #ifdef DEBUG_PRINTS
- cout << "SemSegRegionBased::convertExamplesToVVector finished" << endl;
- #endif
- }
- void SemanticSegmentation::convertVVectorToExamples ( VVector &feats, Examples &examples, vector<int> &label )
- {
- #ifdef DEBUG_PRINTS
- cout << "SemSegRegionBased::convertVVectorToExamples starts" << endl;
- #endif
- for ( int i = 0; i < ( int ) feats.size(); i++ )
- {
- NICE::Vector *v = new NICE::Vector ( feats[i] );
- Example ex ( v );
- ex.position = 0; //TODO: hier mal was besseres überlegen, damit Klassifikator wieder Bildspezifisch lernt
- examples.push_back ( pair<int, Example> ( label[i], ex ) );
- feats[i].clear();
- }
- feats.clear();
- label.clear();
- #ifdef DEBUG_PRINTS
- cout << "SemSegRegionBased::convertVVectorToExamples finished" << endl;
- #endif
- }
- SemanticSegmentation::SemanticSegmentation ( const Config *conf,
- const ClassNames *classNames )
- {
- this->classNames = classNames;
- Preprocess::Init ( conf );
- std::string imagetype_s = conf->gS ( "main", "imagetype", "gray" );
- if ( imagetype_s == "rgb" )
- imagetype = IMAGETYPE_RGB;
- else if ( imagetype_s == "gray" )
- imagetype = IMAGETYPE_GRAY;
- else
- {
- fprintf ( stderr, "SemanticSegmentation:: unknown image type option\n" );
- exit ( -1 );
- }
- }
- SemanticSegmentation::~SemanticSegmentation()
- {
- }
- void SemanticSegmentation::getDepthVector ( const LabeledSet *Files, vector<int> & depthVec )
- {
- std::string oldName;
- int zsize = 0;
- bool isInit = false;
- for (LabeledSet::const_iterator it = Files->begin(); it != Files->end(); it++)
- {
- for (std::vector<ImageInfo *>::const_iterator jt = it->second.begin();
- jt != it->second.end(); jt++)
- {
- ImageInfo & info = *(*jt);
- std::string file = info.img();
- std::vector< std::string > list;
- StringTools::split ( file, '/', list );
- std::string filename = list.back();
- uint found = filename.find_last_of ( "_" );
- if (found < filename.size() )
- {
- std::string curName = filename.substr ( found-3,3 );
- if ( !isInit )
- {
- oldName = curName;
- isInit = true;
- }
- if ( curName.compare ( oldName ) == 0 )
- {
- zsize++;
- }
- else
- {
- depthVec.push_back ( zsize );
- zsize = 1;
- oldName = curName;
- }
- }
- else
- {
- zsize = 1;
- depthVec.push_back ( zsize );
- }
- }
- }
- depthVec.push_back ( zsize );
- }
- void SemanticSegmentation::make3DImage ( const std::vector<std::string> & filelist,
- NICE::MultiChannelImage3DT<double> & imgData )
- {
- bool isInit = false;
- for ( int it = 0; it < ( int ) filelist.size(); it++ )
- {
- if ( imagetype == IMAGETYPE_RGB )
- {
- NICE::ColorImage img;
- try
- {
- img.read ( filelist[it] );
- }
- catch ( ImageException iE )
- {
- fprintf ( stderr, "Failed to open color image file: %s\n", filelist[it].c_str() );
- fprintf ( stderr, "%s\n", iE.what() );
- exit ( -1 );
- }
- if ( !isInit )
- {
- imgData.reInit ( img.width(),img.height(),filelist.size(),3 );
- isInit = true;
- }
- for ( int y = 0; y < img.height(); y++ )
- {
- for ( int x = 0; x < img.width(); x++ )
- {
- for ( int r = 0; r < 3; r++ )
- {
- imgData.set ( x, y, it, img.getPixel ( x,y,r ), r );
- }
- }
- }
- }
- else
- {
- NICE::Image img;
- try
- {
- img.read ( filelist[it] );
- }
- catch ( ImageException iE )
- {
- fprintf ( stderr, "Failed to open image file: %s\n", filelist[it].c_str() );
- fprintf ( stderr, "%s\n", iE.what() );
- exit ( -1 );
- }
- if ( !isInit )
- {
- imgData.reInit ( img.width(),img.height(),filelist.size(),1 );
- isInit = true;
- }
- for ( int y = 0; y < img.height(); y++ )
- {
- for ( int x = 0; x < img.width(); x++ )
- {
- imgData.set ( x, y, it, img.getPixel ( x,y ), 0 );
- }
- }
- }
- }
- if ( imagetype == IMAGETYPE_GRAY )
- {
- imgData.correctShading( 0 );
- }
- }
|