LocalizationResult.cpp 13 KB

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