LocalizationResult.cpp 14 KB

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