#include "RSSlic.h" #include #include #ifdef NICE_USELIB_OPENMP #include #endif #include "SLIC/SLIC.h" using namespace std; using namespace NICE; using namespace OBJREC; ///////////////////// ///////////////////// ///////////////////// // CONSTRUCTORS / DESTRUCTORS ///////////////////// ///////////////////// ///////////////////// RSSlic::RSSlic() : RegionSegmentationMethod() { this->spcount = -1; this->regionsize = 50; this->compactness = 10.0; this->b_lab = true; } RSSlic::RSSlic( const NICE::Config * _conf ) { this->initFromConfig( _conf ); } RSSlic::~RSSlic() { } void RSSlic::initFromConfig(const NICE::Config* _conf, const std::string & _confSection ) { //first, call method for parent object OBJREC::RegionSegmentationMethod::initFromConfig( _conf ); //now set own member variables this->spcount = _conf->gI( _confSection, "spcount", -1); this->regionsize = _conf->gI( _confSection, "regionsize", 50); this->compactness = _conf->gD( _confSection, "compactness", 10.0); this->b_lab = _conf->gB( _confSection, "useLAB", true); } ///////////////////// ///////////////////// ///////////////////// // SEGMENTATION STUFF ///////////////////// ///////////////////// ////////////////// int RSSlic::segRegions ( const NICE::Image & img, NICE::Matrix & mask) const { cerr << "not implemented yet" << endl; return -1; } int RSSlic::segRegions ( const NICE::ColorImage & img, NICE::Matrix & mask) const { const unsigned int imageWidth = img.width(); const unsigned int imageHeight = img.height(); // Kopieren der Werte aus dem ColorImage -> image uint *inputImage = new uint[imageWidth*imageHeight]; unsigned long int counter = 0; for ( unsigned int y = 0; y < imageHeight; y++) { for ( unsigned int x = 0; x < imageWidth; x++, counter++) { uint tmp = 255; for (int c = 0; c < 3; c++) { tmp = tmp<<8; tmp+=(unsigned char)img.getPixelQuick( x, y, c ); } inputImage[counter] = tmp; } } int* labels = new int[imageWidth*imageHeight]; // Eingabebild segmentieren SLIC slic; int numlabels = 0; int superpixelsize = regionsize; if(spcount > 0) { superpixelsize = 0.5+double(imageWidth*imageHeight)/double(spcount); } slic.DoSuperpixelSegmentation_ForGivenSuperpixelSize(inputImage, imageWidth, imageHeight, labels, numlabels, superpixelsize, compactness, b_lab); mask.resize(imageWidth, imageHeight); counter = 0; for ( unsigned int y = 0; y < imageHeight; y++) { for ( unsigned int x = 0; x < imageWidth; x++, counter++ ) { mask(x, y) = labels[counter]; } } /* counter = 0; for ( unsigned int y = 0; y < imageHeight; y++ ) { for ( unsigned int x = 0; x < imageWidth; x++, counter++ ) { for(int c = 0; c < 3; c++) { resultImage.setPixelQuick( x, y, c, labels[counter]); } } } transformSegmentedImg( resultImage, mask);*/ // Speicher freigeben delete inputImage; inputImage = NULL; delete labels; labels = NULL; return numlabels; } ///////////////////// INTERFACE PERSISTENT ///////////////////// // interface specific methods for store and restore ///////////////////// INTERFACE PERSISTENT ///////////////////// void RSSlic::restore ( std::istream & is, int format ) { //delete everything we knew so far... this->clear(); if ( is.good() ) { std::string tmp; is >> tmp; //class name if ( ! this->isStartTag( tmp, "RSSlic" ) ) { std::cerr << " WARNING - attempt to restore RSSlic, but start flag " << tmp << " does not match! Aborting... " << std::endl; throw; } bool b_endOfBlock ( false ) ; while ( !b_endOfBlock ) { is >> tmp; // start of block if ( this->isEndTag( tmp, "RSSlic" ) ) { b_endOfBlock = true; continue; } tmp = this->removeStartTag ( tmp ); if ( tmp.compare("spcount") == 0 ) { is >> this->spcount; is >> tmp; // end of block tmp = this->removeEndTag ( tmp ); } else if ( tmp.compare("regionsize") == 0 ) { is >> this->regionsize; is >> tmp; // end of block tmp = this->removeEndTag ( tmp ); } else if ( tmp.compare("compactness") == 0 ) { is >> this->compactness; is >> tmp; // end of block tmp = this->removeEndTag ( tmp ); } else if ( tmp.compare("b_lab") == 0 ) { is >> this->b_lab; is >> tmp; // end of block tmp = this->removeEndTag ( tmp ); } else { std::cerr << "WARNING -- unexpected RSSlic object -- " << tmp << " -- for restoration... aborting" << std::endl; throw; } } } else { std::cerr << "RSSlic::restore -- InStream not initialized - restoring not possible!" << std::endl; throw; } } void RSSlic::store ( std::ostream & os, int format ) const { if (os.good()) { // show starting point os << this->createStartTag( "RSSlic" ) << std::endl; os << this->createStartTag( "spcount" ) << std::endl; os << this->spcount << std::endl; os << this->createEndTag( "spcount" ) << std::endl; os << this->createStartTag( "regionsize" ) << std::endl; os << this->regionsize << std::endl; os << this->createEndTag( "regionsize" ) << std::endl; os << this->createStartTag( "compactness" ) << std::endl; os << this->compactness << std::endl; os << this->createEndTag( "compactness" ) << std::endl; os << this->createStartTag( "b_lab" ) << std::endl; os << this->b_lab << std::endl; os << this->createEndTag( "b_lab" ) << std::endl; // done os << this->createEndTag( "RSSlic" ) << std::endl; } else { std::cerr << "OutStream not initialized - storing not possible!" << std::endl; } } void RSSlic::clear () { }