iRoCS Toolbox  1.1.0
lblitzDataExplorer.hh
Go to the documentation of this file.
1 /*
2  * lblitzDataExplorer.hh
3  *
4  * Created on: Nov 7, 2010
5  * Author: liu
6  */
7 
8 #ifndef LBLITZDATAEXPLORER_HH_
9 #define LBLITZDATAEXPLORER_HH_
10 
11 #ifdef HAVE_CONFIG_H
12 #include <config.hh>
13 #endif
14 
15 #include <QtGui/QImage>
16 
17 #include <libArrayToolbox/Array.hh>
18 
20 
21 template<typename DataT>
22  QImage
23  arrayToColorQImage(const blitz::Array<DataT, 3> &data);
24 
25 template<typename DataT>
26  QImage
27  arrayToGrayQImage(const blitz::Array<DataT, 2> &data);
28 
30 {
31 public:
32 
33 template<typename DataT>
34  BlitzDataExplorer(blitz::Array<DataT, 4>& data)
35  {
36  blitz::Range all = blitz::Range::all();
37  m_color = true;
38  m_dataColor.resize(data.shape());
39  m_dataColor_channel0.reference(m_dataColor(all, all, all, 0));
40  m_dataColor_channel1.reference(m_dataColor(all, all, all, 1));
41  m_dataColor_channel2.reference(m_dataColor(all, all, all, 2));
42 
43  dmin = 0;
44  dmax = 255;
45  m_dataColor = data;
46  m_currentPos(0) = data.extent(0) / 2;
47  m_currentPos(1) = data.extent(1) / 2;
48  m_currentPos(2) = data.extent(2) / 2;
49  m_currentRatio = 1;
50  m_currentZoom = 0;
51  m_size[0] = data.extent(0);
52  m_size[1] = data.extent(1);
53  m_size[2] = data.extent(2);
54 
55  // m_originalDataColor.resize(data.shape());
56  // m_originalDataColor = blitz::cast<float>(data);
57  ST = 1;
58  }
59 
60 template<typename DataT>
61  BlitzDataExplorer(blitz::Array<DataT, 3>& data)
62  {
63 
64  m_color = false;
65  // std::cerr << "resize" << std::endl;
66  m_dataGray.resize(data.shape());
67  // std::cerr << "normalize" << std::endl;
68  normalize(data, m_dataGray);
69  // std::cerr << "After normalize" << std::endl;
70  m_currentPos = data.shape() / 2;
71  m_currentRatio = 1;
72  m_currentZoom = 0;
73  m_size = data.shape();
74  ST = 1;
75  // m_originalDataGray.resize(data.shape());
76  // m_originalDataGray = blitz::cast<float>(data);
77  }
78 
79 template<typename DataT>
80  void
81  updateData(blitz::Array<bool, 3>& mask, blitz::TinyVector<DataT, 3>& color, blitz::RectDomain<3> & domain)
82  {
83  std::cerr << "update data" << std::endl;
84  m_dataColor_channel0(domain) = where(mask, (color(0) - dmin) / double((dmax - dmin)) * 255, m_dataColor_channel0(
85  domain));
86  m_dataColor_channel1(domain) = where(mask, (color(1) - dmin) / double((dmax - dmin)) * 255, m_dataColor_channel1(
87  domain));
88  m_dataColor_channel2(domain) = where(mask, (color(2) - dmin) / double((dmax - dmin)) * 255, m_dataColor_channel2(
89  domain));
90  }
91 
92 template<typename DataT>
93  void
94  updateData(blitz::Array<bool, 3>& mask, DataT & grayValue, blitz::RectDomain<3> & domain)
95  {
96  m_dataGray(domain) = where(mask, (grayValue - dmin) / double((dmax - dmin)) * 255, m_dataGray(domain));
97  }
98 
99 template<typename DataT>
100  void
101  updateData(blitz::Array<DataT, 3>& image, blitz::RectDomain<3> & domain)
102  {
103  m_dataGray(domain) = (image - dmin) / double((dmax - dmin)) * 255;
104  }
105 
106 template<typename DataT>
107  void
108  updateData(blitz::Array<DataT, 4>& image, blitz::RectDomain<4> & domain)
109  {
110  m_dataColor(domain) = image(domain);
111  }
112 
113 template<typename DataT>
114  void
115  updateData(blitz::Array<DataT, 4>& data)
116  {
117  blitz::Range all = blitz::Range::all();
118  m_color = true;
119  m_dataColor.resize(data.shape());
120  m_dataColor_channel0.reference(m_dataColor(all, all, all, 0));
121  m_dataColor_channel1.reference(m_dataColor(all, all, all, 1));
122  m_dataColor_channel2.reference(m_dataColor(all, all, all, 2));
123  normalize(data, m_dataColor);
124  m_size[0] = data.extent(0);
125  m_size[1] = data.extent(1);
126  m_size[2] = data.extent(2);
127  // m_originalDataColor.resize(data.shape());
128  // m_originalDataColor = blitz::cast<float>(data);
129  }
130 
131 template<typename DataT>
132  void
133  updateData(blitz::Array<DataT, 3>& data)
134  {
135 
136  m_color = false;
137  std::cerr << "resize" << std::endl;
138  m_dataGray.resize(data.shape());
139  std::cerr << "normalize" << std::endl;
140  normalize(data, m_dataGray);
141  std::cerr << "After normalize" << std::endl;
142  m_size = data.shape();
143  // m_originalDataGray.resize(data.shape());
144  // m_originalDataGray = blitz::cast<float>(data);
145  }
146 
147 template<typename DataT, typename DataT2, int Dim, int Dim2>
148  void
149  normalize(blitz::Array<DataT, Dim> &data, blitz::Array<DataT2, Dim2> &normdata)
150  {
151  dmax = blitz::max(data);
152  dmin = blitz::min(data);
153  normdata = (data - dmin) / double((dmax - dmin)) * 255;
154  }
155 
156 public:
157 void
158 move(int x, int y, int z)
159 {
160  m_currentPos = m_currentPos + blitz::TinyVector<int, 3>(z, y, x);
161  keepInRange3D(m_currentPos, blitz::TinyVector<int, 3>(0, 0, 0), m_size - 1);
162 }
163 
164 void
165 zoom(double r)
166 {
167  m_currentZoom = m_currentZoom + r;
168  m_currentZoom = std::min(std::max(m_currentZoom, -4.0), 4.0);
169  m_currentRatio = std::pow(2, m_currentZoom);
170 }
171 
172 void
173 addCursor(QImage& qview, int x, int y)
174 {
175  for (int i = 0; i < qview.width(); i++)
176  {
177  qview.setPixel(i, y, 0xff0000ff);
178  }
179  for (int i = 0; i < qview.height(); i++)
180  {
181  qview.setPixel(x, i, 0xff0000ff);//(qview->pixel(x,i) & 0x0fffffff));
182  }
183 }
184 
185 QImage
187 {
188  int z = std::floor(m_currentPos(0) + 0.5);
189 
190  if (!m_color)
191  {
192  blitz::Range all = blitz::Range::all();
193  QImage qview;
194  if (ST > 1)
195  {
196  LOG << ST << std::endl;
197  int lb = lmax(z + 1 - ST, 0);
198  int ub = lmin(z - 1 + ST, m_size(0) - 1);
199  blitz::Range sliceRange(lb, ub);
200  blitz::Array<unsigned char, 3> subview(m_dataGray(sliceRange, all, all));
201  blitz::firstIndex i;
202  blitz::secondIndex j;
203  blitz::thirdIndex k;
204  blitz::Array<unsigned char, 2> view(mean(subview(k, i, j), k));
205  qview = arrayToGrayQImage(view);
206  }
207  else
208  {
209  blitz::Array<unsigned char, 2> view(m_dataGray(z, all, all));
210  qview = arrayToGrayQImage(view);
211  }
212  QSize imageSize = QSize(qview.width() * m_currentRatio, qview.height() * m_currentRatio);
213  qview = qview.scaled(imageSize, Qt::KeepAspectRatio);
214  addCursor(qview, m_currentPos(2) * m_currentRatio + m_currentRatio / 2, m_currentPos(1) * m_currentRatio
215  + m_currentRatio / 2);
216  return qview;
217  }
218  else
219  {
220  blitz::Range all = blitz::Range::all();
221  QImage qview;
222  if (ST > 1)
223  {
224  // LOG << ST << std::endl;
225  int lb = lmax(z + 1 - ST, 0);
226  int ub = lmin(z - 1 + ST, m_size(0) - 1);
227  blitz::Range sliceRange(lb, ub);
228  blitz::Array<unsigned char, 4> subview(m_dataColor(sliceRange, all, all, all));
229  blitz::firstIndex i;
230  blitz::secondIndex j;
231  blitz::thirdIndex k;
232  blitz::fourthIndex l;
233  blitz::Array<unsigned char, 3> view(blitz::mean(subview(l, i, j, k), l));
234  qview = arrayToColorQImage(view);
235  }
236  else
237  {
238  blitz::Array<unsigned char, 3> view(m_dataColor(z, all, all, all));
239  qview = arrayToColorQImage(view);
240  }
241  QSize imageSize = QSize(qview.width() * m_currentRatio, qview.height() * m_currentRatio);
242  qview = qview.scaled(imageSize, Qt::KeepAspectRatio);
243  addCursor(qview, m_currentPos(2) * m_currentRatio + m_currentRatio / 2, m_currentPos(1) * m_currentRatio
244  + m_currentRatio / 2);
245  return qview;
246  }
247 
248 }
249 
250 QImage
252 {
253  int y = std::floor(m_currentPos(1) + 0.5);
254  if (!m_color)
255  {
256  blitz::Range all = blitz::Range::all();
257  blitz::Array<unsigned char, 2> view = m_dataGray(all, y, all);
258  QImage qview = arrayToGrayQImage(view);
259  QSize imageSize = QSize(qview.width() * m_currentRatio, qview.height() * m_currentRatio);
260  qview = qview.scaled(imageSize, Qt::KeepAspectRatio);
261  addCursor(qview, m_currentPos(2) * m_currentRatio + m_currentRatio / 2, m_currentPos(0) * m_currentRatio
262  + m_currentRatio / 2);
263  return qview;
264  }
265  else
266  {
267  blitz::Range all = blitz::Range::all();
268  QImage qview;
269  if (ST > 1)
270  {
271  // LOG << ST << std::endl;
272  int lb = lmax(y + 1 - ST, 0);
273  int ub = lmin(y - 1 + ST, m_size(1) - 1);
274  blitz::Range sliceRange(lb, ub);
275  blitz::Array<unsigned char, 4> subview(m_dataColor(all, sliceRange, all, all));
276  blitz::firstIndex i;
277  blitz::secondIndex j;
278  blitz::thirdIndex k;
279  blitz::fourthIndex l;
280  blitz::Array<unsigned char, 3> view(blitz::mean(subview(i, l, j, k), l));
281  qview = arrayToColorQImage(view);
282  }
283  else
284  {
285  blitz::Array<unsigned char, 3> view(m_dataColor(all, y, all, all));
286  qview = arrayToColorQImage(view);
287  }
288  QSize imageSize = QSize(qview.width() * m_currentRatio, qview.height() * m_currentRatio);
289  qview = qview.scaled(imageSize, Qt::KeepAspectRatio);
290  addCursor(qview, m_currentPos(2) * m_currentRatio + m_currentRatio / 2, m_currentPos(0) * m_currentRatio
291  + m_currentRatio / 2);
292  return qview;
293  }
294 }
295 
296 QImage
298 {
299  int x = std::floor(m_currentPos(2) + 0.5);
300  blitz::Range all = blitz::Range::all();
301  if (!m_color)
302  {
303  blitz::Array<unsigned char, 2> view = m_dataGray(all, all, x);
304  view.transposeSelf(1, 0);
305  QImage qview = arrayToGrayQImage(view);
306  QSize imageSize = QSize(qview.width() * m_currentRatio, qview.height() * m_currentRatio);
307  qview = qview.scaled(imageSize, Qt::KeepAspectRatio);
308  addCursor(qview, m_currentPos(0) * m_currentRatio + m_currentRatio / 2, m_currentPos(1) * m_currentRatio
309  + m_currentRatio / 2);
310  return qview;
311  }
312  else
313  {
314  QImage qview;
315  if (ST > 1)
316  {
317  // LOG << ST << std::endl;
318  int lb = lmax(x + 1 - ST, 0);
319  int ub = lmin(x - 1 + ST, m_size(2) - 1);
320  blitz::Range sliceRange(lb, ub);
321  blitz::Array<unsigned char, 4> subview(m_dataColor(all, all, sliceRange, all));
322  blitz::firstIndex i;
323  blitz::secondIndex j;
324  blitz::thirdIndex k;
325  blitz::fourthIndex l;
326  blitz::Array<unsigned char, 3> view(blitz::mean(subview(i, j, l, k), l));
327  view.transposeSelf(1, 0, 2);
328  qview = arrayToColorQImage(view);
329  }
330  else
331  {
332  blitz::Array<unsigned char, 3> view(m_dataColor(all, all, x, all));
333  view.transposeSelf(1, 0, 2);
334  qview = arrayToColorQImage(view);
335  }
336  QSize imageSize = QSize(qview.width() * m_currentRatio, qview.height() * m_currentRatio);
337  qview = qview.scaled(imageSize, Qt::KeepAspectRatio);
338  addCursor(qview, m_currentPos(0) * m_currentRatio + m_currentRatio / 2, m_currentPos(1) * m_currentRatio
339  + m_currentRatio / 2);
340  return qview;
341  }
342 }
343 
344 void
345 setST(int st)
346 {
347  ST = st;
348 }
349 // blitz::TinyVector<DataT, Dim-3> getPointData();
350 
351 // void
352 // QImageToArray(QImage *image, Array<DataT, Dim> &newArray);
353 
354 public:
355 blitz::TinyVector<double, 3>
357 {
358  return blitz::TinyVector<int, 3>(m_currentPos(2) * m_currentRatio + m_currentRatio / 2, m_currentPos(1)
359  * m_currentRatio + m_currentRatio / 2, m_currentPos(0) * m_currentRatio + m_currentRatio / 2);
360 }
361 int
363 {
364  return m_currentPos(2) * m_currentRatio + m_currentRatio / 2;
365 }
366 int
368 {
369  return m_currentPos(1) * m_currentRatio + m_currentRatio / 2;
370 }
371 int
373 {
374  return m_currentPos(0) * m_currentRatio + m_currentRatio / 2;
375 }
376 
377 void
378 setPos(int x, int y, int z)
379 {
380  m_currentPos(2) = x > 0 ? std::floor(x / m_currentRatio) : m_currentPos(2);
381  m_currentPos(1) = y > 0 ? std::floor(y / m_currentRatio) : m_currentPos(1);
382  m_currentPos(0) = z > 0 ? std::floor(z / m_currentRatio) : m_currentPos(0);
383  keepInRange3D(m_currentPos, blitz::TinyVector<int, 3>(0, 0, 0), m_size - 1);
384 }
385 
386 void
387 setDataPos(int x, int y, int z)
388 {
389  m_currentPos(2) = x;
390  m_currentPos(1) = y;
391  m_currentPos(0) = z;
392  keepInRange3D(m_currentPos, blitz::TinyVector<int, 3>(0, 0, 0), m_size - 1);
393 }
394 
395 blitz::TinyVector<double, 3>
397 {
398  return m_currentPos;
399 }
400 
401 blitz::TinyVector<double, 6>
402 getDataOnPoint(int x, int y, int z)
403 {
404  blitz::TinyVector<int, 3> point;
405  point(2) = x > 0 ? std::floor(x / m_currentRatio) : m_currentPos(2);
406  point(1) = y > 0 ? std::floor(y / m_currentRatio) : m_currentPos(1);
407  point(0) = z > 0 ? std::floor(z / m_currentRatio) : m_currentPos(0);
408  keepInRange3D(point, blitz::TinyVector<int, 3>(0, 0, 0), m_size - 1);
409  int xx = point(2), yy = point(1), zz = point(0);
410  if (m_color)
411  {
412  return blitz::TinyVector<double, 6>(point(2), point(1), point(0), m_dataColor(zz, yy, xx, 0), m_dataColor(zz, yy,
413  xx, 1), m_dataColor(zz, yy, xx, 2));
414  }
415  else
416  {
417  // return blitz::TinyVector<double, 6>(m_currentZoom, m_currentRatio, m_size(0),
418  // m_size(1), m_size(2),
419  // m_currentPos(0));
420  return blitz::TinyVector<double, 6>(point(2), point(1), point(0), m_dataGray(zz, yy, xx), m_dataGray(zz, yy, xx),
421  m_dataGray(zz, yy, xx));
422  }
423 }
424 
425 public:
426 bool m_color;
427 blitz::TinyVector<int, 3> m_size;
428 blitz::Array<float, 3> m_originalDataGray;
429 blitz::Array<float, 4> m_originalDataColor;
430 private:
431 blitz::Array<unsigned char, 4> m_dataColor;
432 blitz::Array<unsigned char, 3> m_dataColor_channel0;
433 blitz::Array<unsigned char, 3> m_dataColor_channel1;
434 blitz::Array<unsigned char, 3> m_dataColor_channel2;
435 int ST;
436 
437 blitz::Array<unsigned char, 3> m_dataGray;
438 blitz::TinyVector<double, 3> m_currentPos;
439 double m_currentZoom;
440 double m_currentRatio;
441 double dmax, dmin;
442 };
443 
444 template<typename DataT>
445  QImage
446  arrayToGrayQImage(const blitz::Array<DataT, 2> &data)
447  {
448  blitz::Range all = blitz::Range::all();
449  blitz::TinyVector<double, 2> size = data.shape();
450  blitz::Array<unsigned char, 3> imageBuffer(size(0), size(1), 4);
451 
452  imageBuffer(all, all, 0) = blitz::cast<unsigned char>(data);
453  imageBuffer(all, all, 1) = blitz::cast<unsigned char>(data);
454  imageBuffer(all, all, 2) = blitz::cast<unsigned char>(data);
455  imageBuffer(all, all, 3) = 0xff;
456  QImage image(size(1), size(0), QImage::Format_ARGB32);
457  memcpy(image.bits(), imageBuffer.data(), size(0) * size(1) * 4);
458  return image;
459  }
460 
461 template<typename DataT>
462  QImage
463  arrayToColorQImage(const blitz::Array<DataT, 3> &data)
464  {
465  blitz::Range all = blitz::Range::all();
466  blitz::TinyVector<double, 3> size = data.shape();
467  blitz::Array<unsigned char, 3> imageBuffer(size(0), size(1), 4);
468  imageBuffer(all, all, blitz::Range(0, 2)) = blitz::cast<unsigned char>(data);
469  imageBuffer(all, all, 3) = 0xff;
470  QImage image(size(1), size(0), QImage::Format_ARGB32);
471  memcpy(image.bits(), imageBuffer.data(), size(0) * size(1) * 4);
472  return image;
473  }
474 
475 #endif /* LBLITZDATAEXPLORER_HH_ */
blitz::TinyVector< double, 6 > getDataOnPoint(int x, int y, int z)
#define lmax(a, b)
Definition: helper.hh:46
Array class derived from blitz++ Arrays for handling microscopic datasets with associated element siz...
blitz::Array< float, 3 > m_originalDataGray
#define lmin(a, b)
Definition: helper.hh:47
QImage arrayToGrayQImage(const blitz::Array< DataT, 2 > &data)
void setDataPos(int x, int y, int z)
void updateData(blitz::Array< DataT, 4 > &data)
void keepInRange3D(Type &input, Type2 lb, Type3 ub)
void updateData(blitz::Array< bool, 3 > &mask, blitz::TinyVector< DataT, 3 > &color, blitz::RectDomain< 3 > &domain)
#define LOG
Definition: helper.hh:44
BlitzDataExplorer(blitz::Array< DataT, 3 > &data)
void move(int x, int y, int z)
void addCursor(QImage &qview, int x, int y)
blitz::TinyVector< double, 3 > getCurrentPos()
QImage arrayToColorQImage(const blitz::Array< DataT, 3 > &data)
void updateData(blitz::Array< DataT, 3 > &data)
void normalize(blitz::Array< DataT, Dim > &data, blitz::Array< DataT2, Dim2 > &normdata)
BlitzDataExplorer(blitz::Array< DataT, 4 > &data)
void updateData(blitz::Array< bool, 3 > &mask, DataT &grayValue, blitz::RectDomain< 3 > &domain)
void updateData(blitz::Array< DataT, 4 > &image, blitz::RectDomain< 4 > &domain)
bool all(blitz::TinyMatrix< bool, NRows, NColumns > const &matrix)
all() reduction for boolean blitz::TinyMatrix.
void setPos(int x, int y, int z)
blitz::TinyVector< int, 3 > m_size
blitz::Array< float, 4 > m_originalDataColor
void updateData(blitz::Array< DataT, 3 > &image, blitz::RectDomain< 3 > &domain)
Polynomial< CoeffT > pow(const Polynomial< CoeffT > &p, int exponential)
Power operator.
blitz::TinyVector< double, 3 > getCurrentCenter()