ICETools.cpp 7.7 KB

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