ICETools.cpp 7.4 KB

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