ICETools.cpp 7.2 KB

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