#include "RSMeanShift.h" #ifdef NICE_USELIB_OPENMP #include #endif #include #include #include "core/basics/StringTools.h" using namespace std; using namespace NICE; using namespace OBJREC; void RSMeanShift::setSpeedUpLevel( const string& _speedUpLevelString ) { if ( speedUpLvlString == "NONE" ) this->speedUpLevel = NO_SPEEDUP; else if ( speedUpLvlString == "MEDIUM" ) this->speedUpLevel = MED_SPEEDUP; else if ( speedUpLvlString == "HIGH" ) this->speedUpLevel = HIGH_SPEEDUP; else { std::cerr << "[err] RSMeanShift::RSMeanShift: Wrong SpeedUpLevel-Value (" << speedUpLvlString << ") only NONE, MEDIUM and HIGH allowed! Take default Value (MED_SPEEDUP)" << std::endl; this->speedUpLevel = MED_SPEEDUP; } } ///////////////////// ///////////////////// ///////////////////// // CONSTRUCTORS / DESTRUCTORS ///////////////////// ///////////////////// ///////////////////// RSMeanShift::RSMeanShift() : RegionSegmentationMethod() { this->minimumRegionArea = 50; this->rangeBandwidth = 6.5; this->spatialBandwidth = 7; this->imageProc = new msImageProcessor(); this->speedUpLvlString = "MEDIUM"; this->setSpeedUpLevel( this->speedUpLvlString ); } RSMeanShift::RSMeanShift ( const Config * _conf ) { this->initFromConfig( _conf ); } RSMeanShift::~RSMeanShift() { if ( this->imageProc != NULL ) delete imageProc; this->imageProc = NULL; } void RSMeanShift::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->imageProc = new msImageProcessor(); this->minimumRegionArea = _conf->gI ( _confSection, "MinimumRegionArea", 50 ); this->rangeBandwidth = _conf->gD ( _confSection, "RangeBandwidth", 6.5 ); this->spatialBandwidth = _conf->gI ( _confSection, "SpatialBandwidth", 7 ); this->speedUpLvlString = _conf->gS ( _confSection, "SpeedUpLevel", "MEDIUM" ); this->setSpeedUpLevel( this->speedUpLvlString ); } ///////////////////// ///////////////////// ///////////////////// // SEGMENTATION STUFF ///////////////////// ///////////////////// ////////////////// int RSMeanShift::segRegions ( const NICE::Image & img, NICE::Matrix & mask ) const { ColorImage cimg ( img, img, img ); return segRegions ( cimg, mask ); } int RSMeanShift::segRegions ( const NICE::ColorImage & img, NICE::Matrix & mask ) const { clog << "[log] RSMeanShift::segRegions: EDISON-Implementation" << endl; /* >> ColorImage an EDISON uebergeben BEM: Dabei werden die Bilddaten als Pointer an die msImageProcessor::DefineImage Methode uebergeben. Der Parameter hat in der EDISON-Implementierung KEINEN 'const' Qualifier. */ unsigned int width = img.width(); unsigned int height = img.height(); const unsigned int channels = 3; // notwendiger Bildspeicher const unsigned int imageSize = width * height * channels; byte* rawImageData = NULL; #ifdef NICE_USELIB_IPP /* ipp Speichert die Bilddaten anders, so dass das Bild pixelweise kopiert werden muss. */ // Speicher fuer das Bild reservieren rawImageData = new byte[ imageSize ]; // Pixelkoordinaten unsigned int x, y; x = y = 0; // "Breite" des Bildes im Speicher // unsigned int yStepWidth = width * 3; for ( unsigned int i = 0; i < imageSize; i += 3 ) { rawImageData[ i ] = img.getPixel ( x, y, 0 ); rawImageData[ i + 1 ] = img.getPixel ( x, y, 1 ); rawImageData[ i + 2 ] = img.getPixel ( x, y, 2 ); // neue Pixelkoordinaten berechnen if ( ( ++x ) == width ) { x = 0; y++; } } #else // img in nicht 'const' ColorImage kopieren ColorImage tempImage ( img ); rawImageData = tempImage.getPixelPointer(); #endif imageProc->DefineImage ( rawImageData, COLOR, height, width ); /* >> Segementierung durchfuehren */ imageProc->Segment ( spatialBandwidth, rangeBandwidth, minimumRegionArea, speedUpLevel ); /* >> Ergebnisbild aus EDISON auslesen */ byte resultRawImageData[ width * height * 3 ]; imageProc->GetResults ( resultRawImageData ); /* >> Maske erstellen */ #ifdef NICE_USELIB_IPP delete[] rawImageData; rawImageData = 0; NICE::ColorImage ippTempImage ( width, height ); x = y = 0; for ( unsigned int i = 0; i < imageSize; i += 3 ) { ippTempImage.setPixel ( x, y, 0, resultRawImageData[ i ] ); ippTempImage.setPixel ( x, y, 1, resultRawImageData[ i + 1 ] ); ippTempImage.setPixel ( x, y, 2, resultRawImageData[ i + 2 ] ); // neue Pixelkoordinaten berechnen if ( ( ++x ) == width ) { x = 0; y++; } } return transformSegmentedImg ( ippTempImage, mask ); #else return transformSegmentedImg ( ColorImage ( resultRawImageData, width, height, GrayColorImageCommonImplementation::noAlignment ), mask ); #endif } ///////////////////// INTERFACE PERSISTENT ///////////////////// // interface specific methods for store and restore ///////////////////// INTERFACE PERSISTENT ///////////////////// void RSMeanShift::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, "RSMeanShift" ) ) { std::cerr << " WARNING - attempt to restore RSMeanShift, 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, "RSMeanShift" ) ) { b_endOfBlock = true; continue; } tmp = this->removeStartTag ( tmp ); if ( tmp.compare("minimumRegionArea") == 0 ) { is >> minimumRegionArea; is >> tmp; // end of block tmp = this->removeEndTag ( tmp ); } else if ( tmp.compare("rangeBandwidth") == 0 ) { is >> rangeBandwidth; is >> tmp; // end of block tmp = this->removeEndTag ( tmp ); } else if ( tmp.compare("spatialBandwidth") == 0 ) { is >> spatialBandwidth; is >> tmp; // end of block tmp = this->removeEndTag ( tmp ); } else if ( tmp.compare("imageProc") == 0 ) { //nothing to read actually :) imageProc = new msImageProcessor(); is >> tmp; // end of block tmp = this->removeEndTag ( tmp ); } else if ( tmp.compare("speedUpLvlString") == 0 ) { is >> speedUpLvlString; this->setSpeedUpLevel( this->speedUpLvlString ); is >> tmp; // end of block tmp = this->removeEndTag ( tmp ); } else { std::cerr << "WARNING -- unexpected RSMeanShift object -- " << tmp << " -- for restoration... aborting" << std::endl; throw; } } } else { std::cerr << "RSMeanShift::restore -- InStream not initialized - restoring not possible!" << std::endl; throw; } } void RSMeanShift::store ( std::ostream & os, int format ) const { if (os.good()) { // show starting point os << this->createStartTag( "RSMeanShift" ) << std::endl; os << this->createStartTag( "minimumRegionArea" ) << std::endl; os << this->minimumRegionArea << std::endl; os << this->createEndTag( "minimumRegionArea" ) << std::endl; os << this->createStartTag( "rangeBandwidth" ) << std::endl; os << this->rangeBandwidth << std::endl; os << this->createEndTag( "rangeBandwidth" ) << std::endl; os << this->createStartTag( "spatialBandwidth" ) << std::endl; os << this->spatialBandwidth << std::endl; os << this->createEndTag( "spatialBandwidth" ) << std::endl; os << this->createStartTag( "imageProc" ) << std::endl; //nothing to write actually :) os << this->createEndTag( "imageProc" ) << std::endl; os << this->createStartTag( "speedUpLvlString" ) << std::endl; os << this->speedUpLvlString << std::endl; os << this->createEndTag( "speedUpLvlString" ) << std::endl; // done os << this->createEndTag( "RSMeanShift" ) << std::endl; } else { std::cerr << "OutStream not initialized - storing not possible!" << std::endl; } } void RSMeanShift::clear () { if ( this->imageProc != NULL ) delete imageProc; this->imageProc = NULL; }