LocalizationResult.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /**
  2. * @file LocalizationResult.cpp
  3. * @brief Localization result, what else?
  4. * @author Erik Rodner
  5. * @date 02/13/2008
  6. */
  7. #include "core/image/ImageT.h"
  8. #include "core/vector/VectorT.h"
  9. #include "core/vector/MatrixT.h"
  10. #include <iostream>
  11. #include <core/image/LineT.h>
  12. #include "vislearning/cbaselib/LocalizationResult.h"
  13. #include "vislearning/cbaselib/ImageInfo.h"
  14. #include "core/basics/StringTools.h"
  15. // use this macro to show labeled images
  16. #undef DEBUG_LOCALIZATIONREAD
  17. #ifdef DEBUG_LOCALIZATIONREAD
  18. #include <core/imagedisplay/ImageDisplay.h>
  19. #endif
  20. using namespace OBJREC;
  21. using namespace std;
  22. using namespace NICE;
  23. /******** SingleLocalizationResult ********/
  24. SingleLocalizationResult::SingleLocalizationResult ( ClassificationResult *_r, const NICE::Region & _reg, int _controlPoints )
  25. : controlPoints(_controlPoints), hasRegionInformation_bool(true), reg(_reg), r(_r)
  26. {
  27. reg.getRect(xi,yi,xa,ya);
  28. }
  29. SingleLocalizationResult::SingleLocalizationResult ( ClassificationResult *_r, int _xi, int _yi, int _xa, int _ya )
  30. : controlPoints(4), xi(_xi), yi(_yi), xa(_xa), ya(_ya), hasRegionInformation_bool(true), r(_r)
  31. {
  32. // reg.add (xi,yi,xa,ya);
  33. // this might lead to problems...in general the current Region representation is awful !
  34. }
  35. double SingleLocalizationResult::getBBOverlapMeasureMin ( const SingleLocalizationResult & y ) const
  36. {
  37. double measure = 0.0;
  38. int xxi, xyi, xxa, xya;
  39. int yxi, yyi, yxa, yya;
  40. getBoundingBox ( xxi, xyi, xxa, xya );
  41. y.getBoundingBox ( yxi, yyi, yxa, yya );
  42. int mxi = ( xxi > yxi ) ? xxi : yxi;
  43. int myi = ( xyi > yyi ) ? xyi : yyi;
  44. int mxa = ( xxa < yxa ) ? xxa : yxa;
  45. int mya = ( xya < yya ) ? xya : yya;
  46. int iw = mxa - mxi + 1;
  47. int ih = mya - myi + 1;
  48. if ( (iw > 0) && (ih > 0) )
  49. {
  50. // if iw>0 & ih>0
  51. double A = (xxa - xxi + 1)*(xya - xyi + 1);
  52. double B = (yxa - yxi + 1)*(yya - yyi + 1);
  53. double overlap = A < B ? A : B;
  54. measure = iw*ih / overlap;
  55. }
  56. return measure;
  57. }
  58. double SingleLocalizationResult::getBBOverlapMeasure ( const SingleLocalizationResult & y ) const
  59. {
  60. double measure = 0.0;
  61. int xxi, xyi, xxa, xya;
  62. int yxi, yyi, yxa, yya;
  63. getBoundingBox ( xxi, xyi, xxa, xya );
  64. y.getBoundingBox ( yxi, yyi, yxa, yya );
  65. int mxi = ( xxi > yxi ) ? xxi : yxi;
  66. int myi = ( xyi > yyi ) ? xyi : yyi;
  67. int mxa = ( xxa < yxa ) ? xxa : yxa;
  68. int mya = ( xya < yya ) ? xya : yya;
  69. int iw = mxa - mxi + 1;
  70. int ih = mya - myi + 1;
  71. if ( (iw > 0) && (ih > 0) )
  72. {
  73. // if iw>0 & ih>0
  74. double overlap = (xxa - xxi + 1)*(xya - xyi + 1) +
  75. (yxa - yxi + 1)*(yya - yyi + 1) -
  76. iw*ih;
  77. measure = iw*ih / overlap;
  78. }
  79. return measure;
  80. }
  81. void SingleLocalizationResult::getBoundingBox ( int & _xi, int & _yi, int & _xa, int & _ya ) const
  82. {
  83. _xi = xi;
  84. _yi = yi;
  85. _xa = xa;
  86. _ya = ya;
  87. }
  88. void SingleLocalizationResult::getBoundingBox ( RectT<int> & rectangle ) const
  89. {
  90. rectangle = RectT<int> ( CoordT<int> ( xi, yi ), CoordT<int> ( xa, ya ) );
  91. }
  92. void SingleLocalizationResult::getCentroid ( double & x, double & y ) const
  93. {
  94. reg.getCentroid ( x, y );
  95. }
  96. SingleLocalizationResult::~SingleLocalizationResult ()
  97. {
  98. if ( r != NULL )
  99. delete r;
  100. }
  101. /******** LocalizationResult *********/
  102. LocalizationResult::LocalizationResult ( int xsize, int ysize ) : cn(NULL)
  103. {
  104. hasLabeledImage = false;
  105. this->xsize = xsize;
  106. this->ysize = ysize;
  107. }
  108. LocalizationResult::LocalizationResult ( const ClassNames *_cn, int xsize, int ysize ) : cn(_cn)
  109. {
  110. hasLabeledImage = false;
  111. this->xsize = xsize;
  112. this->ysize = ysize;
  113. }
  114. LocalizationResult::~LocalizationResult ()
  115. {
  116. for ( iterator k = begin(); k != end() ; k++ )
  117. {
  118. SingleLocalizationResult *slr = *k;
  119. delete slr;
  120. }
  121. }
  122. LocalizationResult::LocalizationResult ( const ClassNames *_cn, const NICE::Image & img, int classno ) : cn(_cn)
  123. {
  124. // FIXME: just a bad predefined threshold !
  125. const int t = 200;
  126. NICE::Region reg;
  127. #ifdef DEBUG_LOCALIZATIONREAD
  128. NICE::Image imgo (img);
  129. imgo.set(0);
  130. #endif
  131. this->xsize = img.width();
  132. this->ysize = img.height();
  133. for ( int y = 0 ; y < img.height(); y++ )
  134. for ( int x = 0 ; x < img.width(); x++ )
  135. {
  136. if ( img.getPixel(x,y) < t )
  137. {
  138. #ifdef DEBUG_LOCALIZATIONREAD
  139. imgo.setPixel(x,y,1);
  140. #endif
  141. reg.add ( x, y );
  142. }
  143. }
  144. #ifdef DEBUG_LOCALIZATIONREAD
  145. NICE::showImageOverlay ( imgo, imgo );
  146. #endif
  147. ClassificationResult *r = new ClassificationResult (classno, 1.0, _cn->getMaxClassno());
  148. push_back ( new SingleLocalizationResult ( r, reg ) );
  149. hasLabeledImage = false;
  150. }
  151. LocalizationResult::LocalizationResult ( const ClassNames *_cn, const NICE::ColorImage & img) : cn(_cn)
  152. {
  153. map<int, NICE::Region> regions;
  154. xsize = img.width();
  155. ysize = img.height();
  156. #ifdef DEBUG_LOCALIZATIONREAD
  157. NICE::showImage ( img );
  158. NICE::Image imgo (xsize,ysize);
  159. imgo.set(0);
  160. #endif
  161. for ( int y = 0 ; y < ysize ; y++ )
  162. {
  163. int xstart = 0;
  164. // RGB values of the current pixel
  165. int r = img.getPixel(0,y,0);
  166. int g = img.getPixel(0,y,1);
  167. int b = img.getPixel(0,y,2);
  168. for ( int x = 0 ; x < xsize ; x++ )
  169. {
  170. int r_next, g_next, b_next;
  171. if ( x != xsize - 1 ) {
  172. r_next = img.getPixel(x,y,0);
  173. g_next = img.getPixel(x,y,1);
  174. b_next = img.getPixel(x,y,2);
  175. } else {
  176. // at the border of the image, we should
  177. // always have a color change to add the last
  178. // line segment
  179. r_next = -1;
  180. g_next = -1;
  181. b_next = -1;
  182. }
  183. // now the RGB color changes and we have an object boundary
  184. // therefore we have to add a line segment
  185. if ( r != r_next || g != g_next || b != b_next )
  186. {
  187. int classno;
  188. // look up class number for the label color
  189. _cn->getClassnoFromColor ( classno, r, g, b );
  190. if ( classno >= 0 ) {
  191. // add line segment as an rectangular region
  192. regions[classno].add( xstart, y, x, y );
  193. #ifdef DEBUG_LOCALIZATIONREAD
  194. for ( int z = xstart ; z <= x ; z++ )
  195. imgo.setPixel(z,y,classno);
  196. #endif
  197. xstart = x+1;
  198. }
  199. }
  200. r = r_next;
  201. g = g_next;
  202. b = b_next;
  203. }
  204. }
  205. #ifdef DEBUG_LOCALIZATIONREAD
  206. showImageOverlay(imgo, imgo);
  207. #endif
  208. for ( map<int, NICE::Region>::const_iterator j = regions.begin();
  209. j != regions.end();
  210. j++ )
  211. {
  212. int classno = j->first;
  213. ClassificationResult *r = new ClassificationResult (classno, 1.0, _cn->getMaxClassno());
  214. push_back ( new SingleLocalizationResult ( r, j->second ) );
  215. }
  216. hasLabeledImage = false;
  217. }
  218. void LocalizationResult::restore (istream & is, int format)
  219. {
  220. if ( format == FILEFORMAT_PASCAL2006_RESULT )
  221. {
  222. while ( ! is.eof() )
  223. {
  224. double score;
  225. int xi, yi, xa, ya;
  226. // refactor-nice.pl: check this substitution
  227. // old: string classname;
  228. std::string classname;
  229. if ( ! (is >> classname) ) break;
  230. if ( ! (is >> score) ) break;
  231. if ( ! (is >> xi) ) break;
  232. if ( ! (is >> yi) ) break;
  233. if ( ! (is >> xa) ) break;
  234. if ( ! (is >> ya) ) break;
  235. ClassificationResult *r = new ClassificationResult ( cn->classno(classname), score, cn->getMaxClassno() );
  236. SingleLocalizationResult *sr = new SingleLocalizationResult ( r, xi, yi, xa, ya );
  237. push_back ( sr );
  238. }
  239. } else if ( format == FILEFORMAT_PASCAL2006_GROUNDTRUTH ) {
  240. #if 0
  241. /* # Details for object 1 ("PAScat")
  242. Original label for object 1 "PAScat" : "PAScat"
  243. Bounding box for object 1 "PAScat" (Xmin, Ymin) - (Xmax, Ymax) : (11, 135) - (333, 410) */
  244. // refactor-nice.pl: check this substitution
  245. // old: string word;
  246. std::string word;
  247. while ( ! is.eof() )
  248. {
  249. if ( ! (is >> word) ) break;
  250. if ( word != "Bounding" ) continue;
  251. char line[1024];
  252. is.getline (line, 1024);
  253. vector<string> submatches;
  254. bool result = StringTools::regexMatch ( line, "box for object ([:digit]+) \"([:alpha:]+)\" (Xmin, Ymin) - (Xmax, Ymax) : (([:digit:]+) *, *([:digit:]+)) *: *(([:digit:]+) *, *([:digit:]+))", submatches );
  255. cerr << "string: " << line << endl;
  256. for ( vector<string>::const_iterator i = submatches.begin(); i != submatches.end(); i++ )
  257. cerr << "submatch " << *i << endl;
  258. exit(-1);
  259. }
  260. #endif
  261. } else if ( format == FILEFORMAT_POLYGON ) {
  262. // This is limited to bounding boxes ...sorry
  263. while (! is.eof()) {
  264. #define USE_CALTECH101_POLYGON_FORMAT
  265. #ifdef USE_CALTECH101_POLYGON_FORMAT
  266. std::string filename;
  267. if ( !(is >> filename) ) break;
  268. #endif
  269. std::string classname;
  270. if ( !(is >> classname) ) break;
  271. const double score = 1.0;
  272. int classno = cn->classnoFromText(classname);
  273. uint polygon_points;
  274. if ( !(is >> polygon_points) ) break;
  275. int xi = std::numeric_limits<int>::max();
  276. int xa = - std::numeric_limits<int>::max();
  277. int yi = std::numeric_limits<int>::max();
  278. int ya = - std::numeric_limits<int>::max();
  279. for ( uint i = 0 ; i < polygon_points ; i++ )
  280. {
  281. double x,y;
  282. if ( !(is >> x) ) break;
  283. if ( !(is >> y) ) break;
  284. if ( x < xi ) xi = x;
  285. if ( x > xa ) xa = x;
  286. if ( y < yi ) yi = y;
  287. if ( y > ya ) ya = y;
  288. }
  289. if ( classno >= 0 ) {
  290. ClassificationResult *r = new ClassificationResult ( classno, score, cn->getMaxClassno() );
  291. SingleLocalizationResult *sr = new SingleLocalizationResult ( r, xi, yi, xa, ya );
  292. push_back ( sr );
  293. }
  294. }
  295. //sortEmpricalDepth();
  296. }
  297. else {
  298. fthrow(IOException, "LocalizationResult::restore: file format not yet supported !");
  299. }
  300. }
  301. void LocalizationResult::loadImageInfo(std::string sFilename)
  302. {
  303. try
  304. {
  305. ImageInfo info;
  306. info.loadImageInfo(sFilename);
  307. const std::list< OBJREC::BoundingBox > *listBBoxes = info.bboxes();
  308. const double score = 1.0;
  309. OBJREC::BoundingBox box;
  310. std::list< OBJREC::BoundingBox >::const_iterator itBBoxes = listBBoxes->begin();
  311. for(;itBBoxes != listBBoxes->end(); itBBoxes++)
  312. {
  313. box = *itBBoxes;
  314. int id = box.id();
  315. std::stringstream ss;
  316. ss << id;
  317. std::string classname = ss.str();
  318. int classno = cn->classno(classname);
  319. if(classno == -1)
  320. {
  321. fprintf (stderr, "LocalizationResult::loadImageInfo: no classno found for classname %s (using classno=-1)\n", classname.c_str());
  322. }
  323. ClassificationResult *r = new ClassificationResult ( classno, score, cn->getMaxClassno() );
  324. SingleLocalizationResult *sr = new SingleLocalizationResult ( r, box.topLeft().x,
  325. box.topLeft().y,
  326. box.width(),
  327. box.height() );
  328. this->push_back ( sr );
  329. }
  330. }
  331. catch(Exception e)
  332. {
  333. fthrow( Exception, "LocalizationResult::loadImageInfo: error loading image info (ImageLabeler xml format)");
  334. }
  335. }
  336. void LocalizationResult::store (ostream & os, int format) const
  337. {
  338. if ( format == FILEFORMAT_PASCAL2006_RESULT )
  339. {
  340. for ( const_iterator i = begin(); i != end(); i++ )
  341. {
  342. const SingleLocalizationResult *sr = *i;
  343. const ClassificationResult *r = sr->r;
  344. int classno = r->classno;
  345. double score = r->scores.get(classno);
  346. int xi, yi, xa, ya;
  347. sr->getBoundingBox ( xi, yi, xa, ya );
  348. os << cn->text(r->classno) << " " << score << " "
  349. << xi << " "
  350. << yi << " "
  351. << xa << " "
  352. << ya << " " << endl;
  353. }
  354. } else {
  355. fprintf (stderr, "LocalizationResult::store: file format not yet supported !\n");
  356. exit(-1);
  357. }
  358. }
  359. void LocalizationResult::clear ()
  360. {
  361. for ( iterator k = begin(); k != end() ; k++ )
  362. {
  363. SingleLocalizationResult *slr = *k;
  364. delete slr;
  365. }
  366. vector<SingleLocalizationResult *>::clear();
  367. hasLabeledImage = false;
  368. }
  369. /** returns whether the depth of x is smaller than that of y
  370. !!! no transitivity !!! */
  371. bool depthCompare ( const SingleLocalizationResult *x, const SingleLocalizationResult *y )
  372. {
  373. /** According to the LabelMe paper of Torralba et al., Murphy */
  374. const NICE::Region & rx = x->getRegion();
  375. const NICE::Region & ry = y->getRegion();
  376. NICE::Region intersect;
  377. intersect.setIntersection ( rx, ry );
  378. int ax = rx.size();
  379. int ay = ry.size();
  380. int is = intersect.size();
  381. if ( is == 0 )
  382. {
  383. int nx = x->getControlPoints();
  384. int ny = y->getControlPoints();
  385. return ( nx > ny );
  386. } else {
  387. double ratx = (double)is / ax;
  388. double raty = (double)is / ay;
  389. return ( ratx > raty );
  390. }
  391. }
  392. bool confidenceCompare ( const SingleLocalizationResult *x, const SingleLocalizationResult *y )
  393. {
  394. return ( x->r->confidence() > y->r->confidence() );
  395. }
  396. void LocalizationResult::sortDescendingConfidence()
  397. {
  398. sort ( begin(), end(), confidenceCompare );
  399. }
  400. void LocalizationResult::sortEmpricalDepth()
  401. {
  402. sort ( begin(), end(), depthCompare );
  403. }
  404. void LocalizationResult::calcLabeledImage ( NICE::Image & mark, int backgroundClassNo ) const
  405. {
  406. mark.set(backgroundClassNo);
  407. fprintf (stderr, "LocalizationResult: calcLabeledImage %zd\n", size() );
  408. for ( int y = 0 ; y < mark.height(); y++ )
  409. for ( int x = 0 ; x < mark.width(); x++ )
  410. {
  411. for ( LocalizationResult::const_iterator k = begin(); k != end() ; k++ )
  412. {
  413. SingleLocalizationResult *slr = *k;
  414. const NICE::Region & r = slr->getRegion();
  415. if ( r.inside(x,y) ) {
  416. mark.setPixel(x,y,slr->r->classno);
  417. break;
  418. }
  419. }
  420. }
  421. }
  422. void LocalizationResult::getLabeledImageCache ( NICE::Image & mark ) const
  423. {
  424. assert ( hasLabeledImage );
  425. labeledImage->copyFrom ( mark );
  426. }
  427. void LocalizationResult::setMap ( const NICE::Image & _labeledImage )
  428. {
  429. labeledImage = new Image( _labeledImage );
  430. hasLabeledImage = true;
  431. }
  432. void drawOrthoLine ( NICE::ColorImage & img,
  433. int x1, int y1, int x2, int y2,
  434. int width, int sign,
  435. int r,
  436. int g,
  437. int b )
  438. {
  439. int xi = x1; int yi = y1;
  440. int xa = x2; int ya = y2;
  441. for ( int i = 0 ; i < width; i++ )
  442. {
  443. if ( yi == ya ) {
  444. yi = yi + sign;
  445. ya = ya + sign;
  446. } else if ( xi == xa ) {
  447. xi = xi + sign;
  448. xa = xa + sign;
  449. } else {
  450. assert ( 0 == 1 );
  451. }
  452. if ( (xi>=0) && (yi>=0) && (xa<=img.width())
  453. && (ya<=img.height()) )
  454. {
  455. NICE::Line l ( Coord(xi, yi), Coord(xa, ya) );
  456. img.draw( l, NICE::Color(r, g, b) );
  457. }
  458. }
  459. }
  460. void LocalizationResult::displayBoxes ( NICE::ColorImage & img, const ClassNames *cn,
  461. bool display_confidence, bool invert, int width ) const
  462. {
  463. for ( LocalizationResult::const_iterator k = begin(); k != end() ; k++ )
  464. {
  465. SingleLocalizationResult *slr = *k;
  466. int xi, yi, xa, ya;
  467. slr->getBoundingBox ( xi, yi, xa, ya );
  468. int classno = (slr->r == NULL ) ? 0 : slr->r->classno;
  469. int r,g,b;
  470. if ( cn != NULL ) {
  471. cn->getRGBColor ( classno, r, g, b );
  472. } else {
  473. r = 255;
  474. g = 0;
  475. b = 0;
  476. }
  477. if ( invert ) {
  478. r = 255 - r;
  479. g = 255 - g;
  480. b = 255 - b;
  481. }
  482. if ( display_confidence && (cn != NULL)) {
  483. std::string name = cn->text(classno);
  484. char caption[1024];
  485. sprintf ( caption, "%3.2lf %s", slr->r->confidence(), name.c_str() );
  486. // refactor-nice.pl: check this substitution
  487. // old: Text(caption, xi, yi, r, 0, img.RedImage());
  488. // REFACTOR-FIXME Unable to map this statement
  489. // refactor-nice.pl: check this substitution
  490. // old: Text(caption, xi, yi, g, 0, img.GreenImage());
  491. // REFACTOR-FIXME Unable to map this statement
  492. // refactor-nice.pl: check this substitution
  493. // old: Text(caption, xi, yi, b, 0, img.BlueImage());
  494. // REFACTOR-FIXME Unable to map this statement
  495. }
  496. drawOrthoLine ( img, xi-width, yi, xa+width, yi, width, -1, r, g, b );
  497. drawOrthoLine ( img, xa, yi, xa, ya, width, +1, r, g, b );
  498. drawOrthoLine ( img, xa+width, ya, xi-width, ya, width, +1, r, g, b );
  499. drawOrthoLine ( img, xi, ya, xi, yi, width, -1, r, g, b );
  500. }
  501. }