123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- /**
- * @file calcNormTrainingSet.cpp
- * @brief save normalized object images
- * @author Erik Rodner
- * @date 07/21/2008
- */
- #ifdef NOVISUAL
- #include <vislearning/nice_nonvis.h>
- #else
- #include <vislearning/nice.h>
- #endif
- #include <core/image/Convert.h>
- #include <sys/errno.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <core/basics/Config.h>
- #include <vislearning/baselib/cmdline.h>
- #include <vislearning/baselib/Preprocess.h>
- #include <vislearning/cbaselib/MultiDataset.h>
- #include <vislearning/baselib/ProgressBar.h>
- #include <vislearning/baselib/Globals.h>
- using namespace OBJREC;
-
- using namespace NICE;
- using namespace std;
- /**
-
- save normalized object images
-
- */
- int main (int argc, char **argv)
- {
- std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
- char configfile [300];
- char objectclass_c [1024];
- char setname_c [1024];
- bool scaleToAVG = false;
- struct CmdLineOption options[] = {
- {"config", "use config file", NULL, "%s", configfile},
- {"ds", "use data set", "train", "%s", setname_c},
- {"class", "create pictures of", "", "%s", objectclass_c},
- {"scale", "scale pictures to average sizes", NULL, NULL, &scaleToAVG},
- {NULL, NULL, NULL, NULL, NULL}
- };
- int ret;
- char *more_options[argc];
- ret = parse_arguments( argc, (const char**)argv, options, more_options);
- fprintf (stderr, "data set name: %s\n", setname_c );
- if ( ret != 0 )
- {
- if ( ret != 1 ) fprintf (stderr, "Error parsing command line !\n");
- exit (-1);
- }
- Config conf ( configfile );
- Preprocess::Init ( &conf );
-
- MultiDataset md ( &conf );
- // refactor-nice.pl: check this substitution
- // old: string setname ( setname_c );
- std::string setname ( setname_c );
- const LabeledSet & ls = *(md[setname]);
- map<int, double> maxwidth;
- map<int, double> maxheight;
- map<int, double> minwidth;
- map<int, double> minheight;
- map<int, double> avgheight;
- map<int, double> avgwidth;
- map<int, int> count;
- const ClassNames & classNames = md.getClassNames( setname );
- int objectclassno;
-
- // refactor-nice.pl: check this substitution
- // old: string objectclass ( objectclass_c );
- std::string objectclass ( objectclass_c );
- if ( objectclass.size() > 0 )
- {
- cerr << "Object class " << objectclass << endl;
- objectclassno = classNames.classno(objectclass);
- if ( objectclassno < 0 ) {
- fprintf (stderr, "Unknown object class %s\n", objectclass_c );
- exit(-1);
- }
- } else {
- objectclassno = -1;
- }
- ProgressBar pb ("Statistics");
- pb.show();
- LOOP_ALL_S(ls)
- {
- EACH_INFO(classno,info);
- pb.update ( ls.count() );
-
- fprintf (stderr, "Filename %s\n", info.img().c_str());
- if ( ! info.hasLocalizationInfo() ) {
- fprintf (stderr, "No localization information available !!\n");
- exit(-1);
- }
- const LocalizationResult *l = info.localization();
- if ( l->size() <= 0 ) {
- fprintf (stderr, "No objects found in this image !!\n");
- exit(-1);
- }
- fprintf (stderr, "Analyzing bounding boxes\n");
- for ( LocalizationResult::const_iterator i = l->begin();
- i != l->end(); i++ )
- {
- SingleLocalizationResult *slr = *i;
- fprintf (stderr, "checking classno\n");
- assert ( slr->r != NULL );
- int c = slr->r->classno;
- if ( (objectclassno < 0 ) || (c == objectclassno) )
- {
- fprintf (stderr, "getting bounding box\n");
- int xi, xa, yi, ya;
- slr->getBoundingBox ( xi, yi, xa, ya );
- if ( !finite(xi) || !finite(yi) || !finite(xa) || !finite(ya) )
- {
- fprintf (stderr, "illegal bounding box information: %s\n", info.img().c_str() );
- exit(-1);
- }
- double width = xa - xi;
- double height = ya - yi;
- if ( width <= 0 ) {
- fprintf (stderr, "negative width: %s !\n", info.img().c_str());
- exit(-1);
- }
- if ( height <= 0 ) {
- fprintf (stderr, "negative height %s !\n", info.img().c_str());
- exit(-1);
- }
- if ( (minwidth.find(c) == minwidth.end()) || (minwidth[c] > width ) )
- minwidth[c] = width;
- if ( (maxwidth.find(c) == maxwidth.end()) || (maxwidth[c] > width ) )
- maxwidth[c] = width;
- if ( (minheight.find(c) == minheight.end()) || (minheight[c] > height ) )
- minheight[c] = height;
- if ( (maxheight.find(c) == maxheight.end()) || (maxheight[c] > height ) )
- maxheight[c] = height;
- if ( avgheight.find(c) == avgheight.end() )
- avgheight[c] = height;
- else
- avgheight[c] += height;
- if ( avgwidth.find(c) == avgwidth.end() )
- avgwidth[c] = width;
- else
- avgwidth[c] += width;
- if ( count.find(c) == count.end() )
- count[c] = 0;
- else
- count[c] ++;
- }
- fprintf (stderr, "ready for the next file\n");
- }
- }
- if ( (objectclassno >= 0) && (count.find(objectclassno) == count.end() ) )
- {
- fprintf (stderr, "NO examples of class %s found !!\n", objectclass.c_str());
- exit(-1);
- }
-
- fprintf (stderr, "-- Object Statistics --\n");
- for ( map<int, int>::iterator i = count.begin();
- i != count.end();
- i++ )
- {
- int c = i->first;
- int count = i->second;
- avgheight[c] /= count;
- avgwidth[c] /= count;
- // refactor-nice.pl: check this substitution
- // old: string dir = classNames.text(c);
- std::string dir = classNames.text(c);
- int retcode = mkdir ( dir.c_str(), 0700 );
- if ( (retcode < 0) && (retcode != EEXIST ) ) {
- fprintf (stderr, "Failed to create directory: %s\n", dir.c_str() );
- exit(-1);
- }
- fprintf (stderr, "[%s]\n", classNames.text(c).c_str() );
- fprintf (stderr, "width: min %f max %f avg %f\n", minwidth[c], maxwidth[c], avgwidth[c] );
- fprintf (stderr, "height: min %f max %f avg %f\n", minheight[c], maxheight[c], avgheight[c] );
- }
- pb.reset("Crop");
- double borderx = conf.gD("crop", "borderx", 0.2);
- double bordery = conf.gD("crop", "bordery", 0.2);
-
- int counter = 0;
-
- LOOP_ALL_S(ls)
- {
- EACH_INFO(classno,info);
- pb.update ( ls.count() );
- // refactor-nice.pl: check this substitution
- // old: string filename = info.img();
- std::string filename = info.img();
- if ( ! info.hasLocalizationInfo() ) {
- fprintf (stderr, "createNormTrainingSet: file %s has no localization information\n",
- filename.c_str() );
- exit(-1);
- }
- // refactor-nice.pl: check this substitution
- // old: ImageRGB img = Preprocess::ReadImgAdvRGB ( filename );
- NICE::ColorImage img = Preprocess::ReadImgAdvRGB ( filename );
- Globals::setCurrentImgFN ( filename );
- const LocalizationResult *l = info.localization();
-
- for ( LocalizationResult::const_iterator i = l->begin();
- i != l->end(); i++ )
- {
- SingleLocalizationResult *slr = *i;
- int c = slr->r->classno;
- if ( (objectclassno < 0) || (c == objectclassno) )
- {
- int xi, xa, yi, ya;
- slr->getBoundingBox ( xi, yi, xa, ya );
- double w = xa - xi;
- double h = ya - yi;
- if ( (w < 1) || (h < 1) ) {
- fprintf (stderr, "Illegal width or height: %s\n", filename.c_str() );
- exit(-1);
- }
- double dstwidth;
- double dstheight;
- if ( scaleToAVG )
- {
- double normwidth = avgwidth[c]*(1.0+borderx);
- double normheight = avgheight[c]*(1.0+bordery);
- dstwidth = normwidth;
- dstheight = normheight;
- } else {
- dstwidth = w;
- dstheight = h;
- }
- double bxi = xi - borderx / 2.0;
- double bxa = xa + borderx / 2.0;
- double byi = yi - bordery / 2.0;
- double bya = ya + bordery / 2.0;
- if ( bxi < 0.0 ) bxi = 0.0;
- if ( byi < 0.0 ) byi = 0.0;
- // refactor-nice.pl: check this substitution
- // old: if ( bxa > img.xsize() - 1 ) bxa = img.xsize() - 1;
- if ( bxa > img.width() - 1 ) bxa = img.width() - 1;
- // refactor-nice.pl: check this substitution
- // old: if ( bya > img.ysize() - 1 ) bya = img.ysize() - 1;
- if ( bya > img.height() - 1 ) bya = img.height() - 1;
- RectT<int> rect ( bxi, byi,
- bxa - bxi + 1, bya - byi + 1 );
- NICE::ColorImage *subImage = img.createSubImage ( rect );
- NICE::ColorImage dst ( (int)round(dstwidth), (int)round(dstheight) );
- scale ( *subImage, &dst );
- #ifndef NOVISUAL
- showImage(dst);
- #endif
- std::string dir = classNames.text(c);
- char imgfilename_s [1024];
- sprintf ( imgfilename_s, "%s/image_%06d.jpg", dir.c_str(), counter );
- // refactor-nice.pl: check this substitution
- // old: fprintf (stderr, "%s: %d x %d\n", imgfilename_s, dst.xsize(), dst.ysize() );
- fprintf (stderr, "%s: %d x %d\n", imgfilename_s, dst.width(), dst.height() );
- ImageFile imgf ( imgfilename_s );
- try {
- imgf.writer ( &dst );
- } catch ( Exception ) {
- fprintf (stderr, "Failed to write filename %s\n", imgfilename_s );
- exit(-1);
- }
- counter++;
- }
- }
- }
-
- return 0;
- }
|