ICETools.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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: r=0.0; g=0.0; b=y; break;
  95. case 1: r=0.0; g= y; b=1.0; break;
  96. case 2: r=0.0; g=1.0; b=1.0-y; break;
  97. case 3: r= y; g=1.0; b=0.0; break;
  98. case 4: r=1.0; g=1.0-y; b=0.0; break;
  99. case 5: r=1.0; g=y; b=y; break;
  100. default: r=1.0; g=1.0; b=1.0; break;
  101. }
  102. }
  103. void ICETools::convertToRGB ( const NICE::Matrix & m, NICE::ColorImage & img )
  104. {
  105. img.resize ( m.cols(), m.rows() );
  106. double max = - numeric_limits<double>::max();
  107. double min = numeric_limits<double>::max();
  108. for ( size_t x = 0 ; x < (size_t)m.cols(); x++ )
  109. for ( size_t y = 0 ; y < (size_t)m.rows() ; y++ )
  110. {
  111. if (m(y, x) > max ) max =m(y, x);
  112. if (m(y, x) < min ) min =m(y, x);
  113. }
  114. for ( size_t y = 0 ; y < (size_t)m.rows() ; y++ )
  115. for ( size_t x = 0 ; x < (size_t)m.cols(); x++ )
  116. {
  117. double val =(m(y, x) - min) / (max-min);
  118. double r,g,b;
  119. toColor(val, r, g, b);
  120. img.setPixel(x,y,0,(int)(r*255));
  121. img.setPixel(x,y,1,(int)(g*255));
  122. img.setPixel(x,y,2,(int)(b*255));
  123. }
  124. }
  125. void ICETools::convertToRGB ( const NICE::FloatImage & m, NICE::ColorImage & img )
  126. {
  127. img.resize ( m.width(), m.height() );
  128. double max = - numeric_limits<double>::max();
  129. double min = numeric_limits<double>::max();
  130. for ( size_t x = 0 ; x < (size_t)m.width(); x++ )
  131. for ( size_t y = 0 ; y < (size_t)m.height() ; y++ )
  132. {
  133. double v = m.getPixel(x,y);
  134. if ( v > max ) max = v;
  135. if ( v < min ) min = v;
  136. }
  137. for ( size_t y = 0 ; y < (size_t)m.height() ; y++ )
  138. for ( size_t x = 0 ; x < (size_t)m.width(); x++ )
  139. {
  140. double val = (m.getPixel(x,y) - min) / (max-min);
  141. double r,g,b;
  142. toColor(val, r, g, b);
  143. img.setPixel(x,y,0,(int)(r*255));
  144. img.setPixel(x,y,1,(int)(g*255));
  145. img.setPixel(x,y,2,(int)(b*255));
  146. }
  147. }
  148. void ICETools::calcGrayImage ( const NICE::ColorImage & img, NICE::Image & imgg )
  149. {
  150. imgg.resize(img.width(), img.height());
  151. for ( int y = 0 ; y < img.height(); y++ )
  152. for ( int x = 0 ; x < img.width() ; x++ )
  153. {
  154. unsigned char g = (unsigned char)(0.299*img.getPixel(x,y,0) +
  155. 0.587*img.getPixel(x,y,1) + 0.114*img.getPixel(x,y,2));
  156. imgg.setPixel(x,y,g);
  157. }
  158. }
  159. double *ICETools::convertICE2M ( const NICE::Matrix & M )
  160. {
  161. double *raw = new double [ M.rows() * M.cols() ];
  162. long index = 0;
  163. for ( uint i = 0 ; i < M.rows() ; i++ )
  164. for ( uint j = 0 ; j < M.cols() ; j++, index++ )
  165. raw[index] = M(i, j);
  166. return raw;
  167. }
  168. void ICETools::convertM2ICE ( NICE::Matrix & M, double *raw )
  169. {
  170. long index = 0;
  171. for ( int i = 0 ; i < (int)M.rows() ; i++ )
  172. for ( int j = 0 ; j < (int)M.cols() ; j++, index++ )
  173. M(i, j) = raw[index];
  174. }
  175. #ifndef NOVISUAL
  176. int ICETools::markImage (const NICE::Image & img, NICE::Image & mark, int marksize, int color)
  177. {
  178. fprintf (stderr, "ICETools::markImage: reimplement this function for NICE\n");
  179. exit(-1);
  180. /*
  181. int flags;
  182. int p[2];
  183. int xo = -1;
  184. int yo = -1;
  185. while(1) {
  186. // refactor-nice.pl: check this substitution
  187. // old: flags = Mouse(img,p[0],p[1]);
  188. // REFACTOR-FIXME Unable to map this statement
  189. if ( (flags & M_LEFT_DOWN) > 0 ) {
  190. if ( (p[0] != xo) || (p[1] != yo) ) {
  191. if ( marksize == 1 ) {
  192. // refactor-nice.pl: check this substitution
  193. // old: PutVal( mark, p[0], p[1], color );
  194. mark.setPixel(p[0],p[1],color);
  195. } else {
  196. // refactor-nice.pl: check this substitution
  197. // old: Marker(3, p[0], p[1], color, marksize, mark);
  198. // REFACTOR-FIXME Unable to map this statement
  199. }
  200. xo = p[0];
  201. yo = p[1];
  202. }
  203. }
  204. if ( (flags & M_RIGHT_DOWN) > 0 ) {
  205. while ( (flags & M_RIGHT_DOWN) > 0 ) {
  206. // refactor-nice.pl: check this substitution
  207. // old: flags = Mouse(img,p[0],p[1]);
  208. // REFACTOR-FIXME Unable to map this statement
  209. };
  210. return -1;
  211. }
  212. }
  213. */
  214. return 0;
  215. }
  216. // refactor-nice.pl: check this substitution
  217. // old: int ICETools::showImages ( vector<Image> & imagelist )
  218. int ICETools::showImages ( vector<NICE::Image> & imagelist )
  219. {
  220. #ifndef NOVISUAL
  221. for ( size_t j = 0 ; j < imagelist.size() ; j++ )
  222. {
  223. // refactor-nice.pl: check this substitution
  224. // old: Show(ON, imagelist[j]);
  225. showImage(imagelist[j]);
  226. }
  227. #endif
  228. // assumption: same size
  229. // refactor-nice.pl: check this substitution
  230. // old: int xsize = imagelist[0]->xsize;
  231. int xsize = imagelist[0].width();
  232. // refactor-nice.pl: check this substitution
  233. // old: int ysize = imagelist[0]->ysize;
  234. int ysize = imagelist[0].height();
  235. int n = imagelist.size();
  236. int n1 = (int)sqrt(n);
  237. int n2 = n/n1 + 1;
  238. NICE::Image img (n1*xsize, n2*ysize);
  239. img.set(0);
  240. int k = 0;
  241. for ( int j = 0 ; j < n2 ; j++ )
  242. for ( int i = 0 ; i < n1 ; i++,k++ )
  243. if ( k >= n ) break;
  244. else {
  245. for ( int y = 0 ; y < imagelist[k].height(); y++ )
  246. for ( int x = 0 ; x < imagelist[k].width(); x++ )
  247. {
  248. img.setPixel( i*xsize + x, j*ysize + y,
  249. imagelist[k].getPixel(x,y) );
  250. }
  251. }
  252. img.write ("display.bmp");
  253. getchar();
  254. return 0;
  255. }
  256. #endif