ICETools.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /**
  2. * @file ICETools.cpp
  3. * @brief simple ICE GUI tools
  4. * @author Erik Rodner
  5. * @date 03/13/2008
  6. */
  7. #include "core/image/ImageT.h"
  8. #include "core/vector/VectorT.h"
  9. #include "core/vector/MatrixT.h"
  10. #include <core/imagedisplay/ImageDisplay.h>
  11. #include <core/image/Filter.h>
  12. #include <iostream>
  13. #include <core/image/RectT.h>
  14. #include "vislearning/baselib/ICETools.h"
  15. #include "core/basics/Exception.h"
  16. using namespace OBJREC;
  17. using namespace std;
  18. using namespace NICE;
  19. ICETools::ICETools()
  20. {
  21. }
  22. ICETools::~ICETools()
  23. {
  24. }
  25. void ICETools::selectRectangles ( const NICE::Image & panel, NICE::Image & overlay, vector<Vector> & x, int color )
  26. {
  27. fthrow ( Exception, "ICETools::selectRectangles -- not yet implemented due to old ICE version." );
  28. // #ifndef NOVISUAL
  29. // // new NICE code
  30. // vector<RectT<double> > rectangles;
  31. // //TODO check this!
  32. // // NICE::selectRectangles ( panel, rectangles, "select rectangles" );
  33. // // selectRectangles ( panel, rectangles, "select rectangles" );
  34. //
  35. // for ( vector<RectT<double> >::const_iterator i = rectangles.begin();
  36. // i != rectangles.end(); i++ )
  37. // {
  38. // Vector vec (4);
  39. // vec[0] = i->left;
  40. // vec[1] = i->top;
  41. // vec[2] = i->right();
  42. // vec[3] = i->bottom();
  43. //
  44. // x.push_back ( vec );
  45. // }
  46. // #else
  47. // fprintf (stderr, "ICETools::selectRectangles: visualization disabled !\n");
  48. // #endif
  49. }
  50. void ICETools::selectPoints ( const NICE::Image & panel, NICE::Image & overlay, vector<Vector> & x, int color )
  51. {
  52. fthrow ( Exception, "ICETools::selectPoints -- not yet implemented due to old ICE version." );
  53. // #ifndef NOVISUAL
  54. // vector<CoordT<double> > points;
  55. // //TODO check this!
  56. // // NICE::selectPoints ( panel, points, "select points" );
  57. // // selectPoints ( panel, points, "select points" );
  58. //
  59. // for ( vector<CoordT<double> >::const_iterator i = points.begin();
  60. // i != points.end(); i++ )
  61. // {
  62. // Vector vec (2);
  63. // vec[0] = i->x;
  64. // vec[1] = i->y;
  65. //
  66. // x.push_back ( vec );
  67. // }
  68. //
  69. // /** OLD ICE Code
  70. // int p[2];
  71. // int rc = -1;
  72. //
  73. // while ( (rc = SelPoint ( DEFAULT, panel, p )) != -1 )
  74. // {
  75. // int x1, y1;
  76. // x1 = p[0]; y1 = p[1];
  77. // // refactor-nice.pl: check this substitution
  78. // // old: Marker ( DEFAULT, p[0], p[1], color, 5, overlay );
  79. // // REFACTOR-FIXME Unable to map this statement
  80. // // refactor-nice.pl: check this substitution
  81. // // old: x.push_back ( Vector(x1, y1) );
  82. // // REFACTOR-FIXME Unable to std::map this statement
  83. // }
  84. // */
  85. // #else
  86. // fprintf (stderr, "ICETools::selectPoints: visualization disabled !\n");
  87. // #endif
  88. }
  89. void ICETools::toColor ( double x, double & r, double & g, double & b )
  90. {
  91. size_t seg = ( size_t ) ( x * 6.0 );
  92. double y = ( 6 * x - seg );
  93. switch ( seg ) {
  94. case 0:
  95. r = 0.0;
  96. g = 0.0;
  97. b = y;
  98. break;
  99. case 1:
  100. r = 0.0;
  101. g = y;
  102. b = 1.0;
  103. break;
  104. case 2:
  105. r = 0.0;
  106. g = 1.0;
  107. b = 1.0 - y;
  108. break;
  109. case 3:
  110. r = y;
  111. g = 1.0;
  112. b = 0.0;
  113. break;
  114. case 4:
  115. r = 1.0;
  116. g = 1.0 - y;
  117. b = 0.0;
  118. break;
  119. case 5:
  120. r = 1.0;
  121. g = y;
  122. b = y;
  123. break;
  124. default:
  125. r = 1.0;
  126. g = 1.0;
  127. b = 1.0;
  128. break;
  129. }
  130. }
  131. void ICETools::convertToRGB ( const NICE::Matrix & m, NICE::ColorImage & img )
  132. {
  133. img.resize ( m.cols(), m.rows() );
  134. double max = - numeric_limits<double>::max();
  135. double min = numeric_limits<double>::max();
  136. for ( size_t x = 0 ; x < ( size_t ) m.cols(); x++ )
  137. for ( size_t y = 0 ; y < ( size_t ) m.rows() ; y++ )
  138. {
  139. if ( m ( y, x ) > max ) max = m ( y, x );
  140. if ( m ( y, x ) < min ) min = m ( y, x );
  141. }
  142. cout << "max: " << max << " min: " << min << endl;
  143. for ( size_t y = 0 ; y < ( size_t ) m.rows() ; y++ )
  144. for ( size_t x = 0 ; x < ( size_t ) m.cols(); x++ )
  145. {
  146. double val = ( m ( y, x ) - min ) / ( max - min );
  147. double r, g, b;
  148. toColor ( val, r, g, b );
  149. img.setPixel ( x, y, 0, ( int ) ( r*255 ) );
  150. img.setPixel ( x, y, 1, ( int ) ( g*255 ) );
  151. img.setPixel ( x, y, 2, ( int ) ( b*255 ) );
  152. }
  153. }
  154. void ICETools::convertToRGB ( const NICE::FloatImage & m, NICE::ColorImage & img )
  155. {
  156. img.resize ( m.width(), m.height() );
  157. double max = - numeric_limits<double>::max();
  158. double min = numeric_limits<double>::max();
  159. for ( size_t x = 0 ; x < ( size_t ) m.width(); x++ )
  160. for ( size_t y = 0 ; y < ( size_t ) m.height() ; y++ )
  161. {
  162. double v = m.getPixel ( x, y );
  163. if ( v > max ) max = v;
  164. if ( v < min ) min = v;
  165. }
  166. for ( size_t y = 0 ; y < ( size_t ) m.height() ; y++ )
  167. for ( size_t x = 0 ; x < ( size_t ) m.width(); x++ )
  168. {
  169. double val = ( m.getPixel ( x, y ) - min ) / ( max - min );
  170. double r, g, b;
  171. toColor ( val, r, g, b );
  172. img.setPixel ( x, y, 0, ( int ) ( r*255 ) );
  173. img.setPixel ( x, y, 1, ( int ) ( g*255 ) );
  174. img.setPixel ( x, y, 2, ( int ) ( b*255 ) );
  175. }
  176. }
  177. void ICETools::calcGrayImage ( const NICE::ColorImage & img, NICE::Image & imgg )
  178. {
  179. imgg.resize ( img.width(), img.height() );
  180. for ( int y = 0 ; y < img.height(); y++ )
  181. for ( int x = 0 ; x < img.width() ; x++ )
  182. {
  183. unsigned char g = ( unsigned char ) ( 0.299 * img.getPixel ( x, y, 0 ) +
  184. 0.587 * img.getPixel ( x, y, 1 ) + 0.114 * img.getPixel ( x, y, 2 ) );
  185. imgg.setPixel ( x, y, g );
  186. }
  187. }
  188. double *ICETools::convertICE2M ( const NICE::Matrix & M )
  189. {
  190. double *raw = new double [ M.rows() * M.cols() ];
  191. long index = 0;
  192. for ( uint i = 0 ; i < M.rows() ; i++ )
  193. for ( uint j = 0 ; j < M.cols() ; j++, index++ )
  194. raw[index] = M ( i, j );
  195. return raw;
  196. }
  197. void ICETools::convertM2ICE ( NICE::Matrix & M, double *raw )
  198. {
  199. long index = 0;
  200. for ( int i = 0 ; i < ( int ) M.rows() ; i++ )
  201. for ( int j = 0 ; j < ( int ) M.cols() ; j++, index++ )
  202. M ( i, j ) = raw[index];
  203. }
  204. #ifndef NOVISUAL
  205. int ICETools::markImage ( const NICE::Image & img, NICE::Image & mark, int marksize, int color )
  206. {
  207. fprintf ( stderr, "ICETools::markImage: reimplement this function for NICE\n" );
  208. exit ( -1 );
  209. /*
  210. int flags;
  211. int p[2];
  212. int xo = -1;
  213. int yo = -1;
  214. while(1) {
  215. // refactor-nice.pl: check this substitution
  216. // old: flags = Mouse(img,p[0],p[1]);
  217. // REFACTOR-FIXME Unable to map this statement
  218. if ( (flags & M_LEFT_DOWN) > 0 ) {
  219. if ( (p[0] != xo) || (p[1] != yo) ) {
  220. if ( marksize == 1 ) {
  221. // refactor-nice.pl: check this substitution
  222. // old: PutVal( mark, p[0], p[1], color );
  223. mark.setPixel(p[0],p[1],color);
  224. } else {
  225. // refactor-nice.pl: check this substitution
  226. // old: Marker(3, p[0], p[1], color, marksize, mark);
  227. // REFACTOR-FIXME Unable to map this statement
  228. }
  229. xo = p[0];
  230. yo = p[1];
  231. }
  232. }
  233. if ( (flags & M_RIGHT_DOWN) > 0 ) {
  234. while ( (flags & M_RIGHT_DOWN) > 0 ) {
  235. // refactor-nice.pl: check this substitution
  236. // old: flags = Mouse(img,p[0],p[1]);
  237. // REFACTOR-FIXME Unable to map this statement
  238. };
  239. return -1;
  240. }
  241. }
  242. */
  243. return 0;
  244. }
  245. // refactor-nice.pl: check this substitution
  246. // old: int ICETools::showImages ( vector<Image> & imagelist )
  247. int ICETools::showImages ( vector<NICE::Image> & imagelist )
  248. {
  249. #ifndef NOVISUAL
  250. for ( size_t j = 0 ; j < imagelist.size() ; j++ )
  251. {
  252. // refactor-nice.pl: check this substitution
  253. // old: Show(ON, imagelist[j]);
  254. showImage ( imagelist[j] );
  255. }
  256. #endif
  257. // assumption: same size
  258. // refactor-nice.pl: check this substitution
  259. // old: int xsize = imagelist[0]->xsize;
  260. int xsize = imagelist[0].width();
  261. // refactor-nice.pl: check this substitution
  262. // old: int ysize = imagelist[0]->ysize;
  263. int ysize = imagelist[0].height();
  264. int n = imagelist.size();
  265. int n1 = ( int ) sqrt ( n );
  266. int n2 = n / n1 + 1;
  267. NICE::Image img ( n1*xsize, n2*ysize );
  268. img.set ( 0 );
  269. int k = 0;
  270. for ( int j = 0 ; j < n2 ; j++ )
  271. for ( int i = 0 ; i < n1 ; i++, k++ )
  272. if ( k >= n ) break;
  273. else {
  274. for ( int y = 0 ; y < imagelist[k].height(); y++ )
  275. for ( int x = 0 ; x < imagelist[k].width(); x++ )
  276. {
  277. img.setPixel ( i*xsize + x, j*ysize + y,
  278. imagelist[k].getPixel ( x, y ) );
  279. }
  280. }
  281. img.write ( "display.bmp" );
  282. getchar();
  283. return 0;
  284. }
  285. #endif