123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- #include "RSSlic.h"
- #include <iostream>
- #include <fstream>
- #ifdef NICE_USELIB_OPENMP
- #include <omp.h>
- #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
- {
- ColorImage cimg ( img, img, img );
- return segRegions ( cimg, mask );
- }
- 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<rgb>
- 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 ()
- {
- }
|