/** * @file toyExample.cpp * @brief just a toy tool * @author Erik Rodner * @date 04/07/2009 */ #include #include #include #include #include #include "core/basics/Config.h" #include "vislearning/baselib/ICETools.h" #include "vislearning/classifier/genericClassifierSelection.h" #ifdef NOVISUAL #include #else #include #endif using namespace OBJREC; using namespace NICE; using namespace std; #ifndef NOVISUAL void selectTrainingSet ( LabeledSetVector & train, NICE::Image & img, int numClasses, bool addBias = false ) { vector colors; vector > points; NICE::selectColoredPoints ( img, points, colors, "Select some points!", numClasses ); int k = 0; for ( vector >::const_iterator i = points.begin(); i != points.end(); i++,k++ ) { NICE::Vector feature ( addBias ? 3 : 2 ); feature[0] = i->x; feature[1] = i->y; if ( addBias ) feature[2] = 1.0; train.add ( colors[k]-1, feature ); } } #endif void markBoundary ( const NICE::Image & imgclassno, NICE::Image & mark ) { for ( int y = 0 ; y < imgclassno.height(); y++ ) for ( int x = 0 ; x < imgclassno.width(); x++ ) { int val = imgclassno.getPixel(x,y); bool boundary = false; for ( int i = -1 ; (i <= 1) && (!boundary) ; i++ ) for ( int j = -1 ; (j <= 1) && (!boundary) ; j++ ) { int xn = x + i; int yn = y + j; if ( (xn<0) || (yn<0) || (xn>=imgclassno.width()) || (yn>=imgclassno.height()) ) continue; int valn = imgclassno.getPixel(xn,yn); if ( valn != val ) boundary = true; } if ( boundary ) mark.setPixel(x,y,1); } } /** just a toy tool */ int main (int argc, char **argv) { std::set_terminate(__gnu_cxx::__verbose_terminate_handler); Config conf ( argc, argv ); conf.store(cout); std::string classifier_type = conf.gS("main", "classifier", "sparse_logistic_regression"); fprintf (stderr, "Classifier type: %s\n", classifier_type.c_str() ); VecClassifier *vec_classifier = GenericClassifierSelection::selectVecClassifier ( &conf, classifier_type ); if ( vec_classifier == NULL ) { fprintf (stderr, "This classifier type is unknown !\n"); exit(-1); } conf.sS("VCSVMLight", "normalization_type", "none" ); int xsize = conf.gI("main", "xsize", 300 ); int ysize = conf.gI("main", "ysize", 300 ); int numClasses = conf.gI("main", "numClasses", 2 ); vec_classifier->setMaxClassNo(numClasses); bool addBias = conf.gB("main", "addbias", "false" ); NICE::Image img (xsize, ysize); NICE::Image mark (img); mark.set(0); img.set(255); LabeledSetVector train; std::string trainsetcache = conf.gS("main", "trainset", ""); bool readtrainset = conf.gB("main", "readtrainset", false); bool selectManually = conf.gB("main", "select", true); if ( selectManually ) { #ifdef NOVISUAL fprintf (stderr, "toyExample: visual manual selection needs ICE visualization\n"); #else selectTrainingSet ( train, img, numClasses, addBias ); #endif } if ( readtrainset && (trainsetcache.size() > 0 ) ) { train.read ( trainsetcache, LabeledSetVector::FILEFORMAT_NOINDEX ); } LOOP_ALL(train) { EACH(classno,x); if ( classno == 0 ) { Cross cross ( Coord( (int)(x[0]), (int)(x[1]) ), 10 ); mark.draw ( cross, classno+2 ); } else { Circle circle ( Coord( (int)(x[0]), (int)(x[1]) ), 10 ); mark.draw ( circle, classno+2 ); } } bool writetrainset = conf.gB("main", "writetrainset", false); if ( writetrainset && (trainsetcache.size() > 0) ) train.save ( trainsetcache, LabeledSetVector::FILEFORMAT_NOINDEX); if ( train.count() <= 0 ) { fprintf (stderr, "toyExample: size of the training set is zero!\n"); exit(-1); } fprintf (stderr, "Dimension of the training set: %d\n", train.dimension() ); vec_classifier->teach ( train ); vec_classifier->finishTeaching(); NICE::FloatImage imgd (img.width(), img.height()); NICE::Image imgclassno (img); for ( int y = 0 ; y < img.height(); y++ ) for ( int x = 0 ; x < img.width(); x++ ) { NICE::Vector example ( addBias ? 3 : 2 ); example[0] = x; example[1] = y; if ( addBias ) example[2] = 1.0; ClassificationResult r = vec_classifier->classify(example); if ( numClasses == 2 ) { imgd.setPixel(x,y,(r.scores.get(1))); } else { imgd.setPixel(x, y, r.classno / (double)(numClasses-1) ); } imgclassno.setPixel(x,y,r.classno); } markBoundary ( imgclassno, mark ); floatToGrayScaled ( imgd, &img ); showImageOverlay ( img, mark ); string resultimg = conf.gS("main", "resultimg", ""); if ( resultimg.size() > 0 ) { ColorImage result; grayToRGB( img, &result ); for ( uint y = 0 ; y < result.height() ; y++ ) for ( uint x = 0 ; x < result.width() ; x++ ) if ( mark.getPixel(x,y) > 0 ) { result.setPixel(x,y,0,overlayColorTable[mark.getPixel(x,y)][0]); result.setPixel(x,y,1,overlayColorTable[mark.getPixel(x,y)][1]); result.setPixel(x,y,2,overlayColorTable[mark.getPixel(x,y)][2]); } ImageFile imgf ( resultimg ); imgf.writer(&result); } showImageOverlay ( imgclassno, imgclassno ); return 0; }