2d/image.hh
Go to the documentation of this file.
1 /* -*- mia-c++ -*-
2  *
3  * This file is part of MIA - a toolbox for medical image analysis
4  * Copyright (c) Leipzig, Madrid 1999-2013 Gert Wollny
5  *
6  * MIA is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with MIA; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 #ifndef mia_2d_2dimage_hh
23 #define mia_2d_2dimage_hh
24 
25 #include <mia/core/attributes.hh>
26 #include <mia/core/pixeltype.hh>
27 #include <mia/core/filter.hh>
28 #include <mia/core/msgstream.hh>
29 #include <mia/core/tools.hh>
30 #include <mia/core/type_traits.hh>
31 #include <mia/2d/vectorfield.hh>
32 
34 
35 #define ATTR_IMAGE_KMEANS_CLASSES "kmeans"
36 
47 public:
50  static const char *data_descr;
51 
54 
56  typedef std::shared_ptr<C2DImage > Pointer;
57 
58  virtual ~C2DImage();
59 
61  EPixelType get_pixel_type() const;
62 
64  const C2DBounds& get_size() const;
65 
67  C2DFVector get_pixel_size() const;
68 
70  void set_pixel_size(const C2DFVector& pixel);
71 
76  virtual C2DImage* clone() const __attribute__((warn_unused_result)) = 0;
77 
78  protected:
81  C2DImage(const C2DBounds& size, EPixelType type);
82 
85  C2DImage(const CAttributedData& attributes, const C2DBounds& size, EPixelType type);
86 
90  C2DImage();
91 
92  private:
93  C2DBounds m_size;
94  EPixelType m_pixel_type;
95 };
96 
98 typedef C2DImage::Pointer P2DImage;
99 
101 typedef std::vector<P2DImage> C2DImageSeries;
102 
103 typedef std::shared_ptr<C2DImageSeries> P2DImageSeries;
104 
105 typedef std::vector<C2DImageSeries> C2DImageSeriesGroup;
106 typedef std::map<std::string, C2DImageSeriesGroup> C2DImageGroupedSeries;
107 
108 
116 template <typename T>
117 class EXPORT_2D T2DImage : public C2DImage {
118 public:
120  typedef C2DImage Super;
121 
123  typedef typename T2DDatafield<T>::iterator iterator;
124  typedef typename T2DDatafield<T>::const_iterator const_iterator;
125  typedef typename T2DDatafield<T>::const_reference const_reference;
126  typedef typename T2DDatafield<T>::reference reference;
127  typedef typename T2DDatafield<T>::const_pointer const_pointer;
128  typedef typename T2DDatafield<T>::pointer pointer;
129  typedef typename T2DDatafield<T>::value_type value_type;
130  typedef typename T2DDatafield<T>::difference_type difference_type;
131  typedef typename T2DDatafield<T>::size_type size_type;
132  typedef typename T2DDatafield<T>::range_iterator range_iterator;
133  typedef typename T2DDatafield<T>::const_range_iterator const_range_iterator;
134 
136 
142  T2DImage(const C2DBounds& size, const T* init_data);
143 
149  T2DImage(const C2DBounds& size, const typename T2DDatafield<T>::data_array& init_data);
155  T2DImage(const C2DBounds& size, const CAttributedData& attr);
156 
161  T2DImage(const C2DBounds& size);
162 
166  T2DImage(const T2DImage& orig);
167 
172  T2DImage(const T2DDatafield<T>& orig);
173 
179  T2DImage(const T2DDatafield<T>& orig, const CAttributedData& attr);
180 
181  T2DImage();
182 
186  virtual C2DImage* clone() const __attribute__((warn_unused_result));
187 
189  const_reference operator()(size_t x, size_t y) const {
190  return m_image(x,y);
191  }
192 
194  reference operator()(size_t x, size_t y){
195  return m_image(x,y);
196  }
197 
199  const_reference operator[](size_t idx) const {
200  return m_image[idx];
201  }
202 
204  reference operator[](size_t idx){
205  return m_image[idx];
206  }
207 
209  const_reference operator()(const C2DBounds& l) const{
210  return m_image(l.x,l.y);
211  }
212 
214  reference operator()(const C2DBounds& l){
215  return m_image(l.x,l.y);
216  }
217 
219  const_iterator begin()const {
220  return m_image.begin();
221  }
222 
224  const_iterator end()const {
225  return m_image.end();
226  }
227 
229  iterator begin() {
230  return m_image.begin();
231  }
232 
234  iterator end() {
235  return m_image.end();
236  }
237 
239  const_iterator begin_at(size_t x, size_t y)const {
240  return m_image.begin_at(x, y);
241  }
242 
244  iterator begin_at(size_t x, size_t y) {
245  return m_image.begin_at(x, y);
246  }
247 
249  range_iterator begin_range(const C2DBounds& begin, const C2DBounds& end) {
250  return m_image.begin_range(begin, end);
251  }
252 
254  range_iterator end_range(const C2DBounds& begin, const C2DBounds& end){
255  return m_image.end_range(begin, end);
256  }
257 
259  const_range_iterator begin_range(const C2DBounds& begin, const C2DBounds& end)const {
260  return m_image.begin_range(begin, end);
261  }
262 
264  const_range_iterator end_range(const C2DBounds& begin, const C2DBounds& end) const{
265  return m_image.end_range(begin, end);
266  }
267 
269  size_t size() const;
270 
272  const T2DDatafield<T>& data() const;
273 
275  void get_data_line_x(size_t y, std::vector<T>& buffer) const;
276 
278  void get_data_line_y(size_t x, std::vector<T>& buffer) const;
279 
281  void put_data_line_x(size_t y, const std::vector<T>& buffer);
282 
284  void put_data_line_y(size_t x, const std::vector<T>& buffer);
285 
291  C2DFVector get_gradient(size_t idx) const;
292 
298  C2DFVector get_gradient(const C2DFVector& p) const;
299 private:
300  T2DDatafield<T> m_image;
301 };
302 
307 class CImageComparePrinter: public TFilter<int> {
308 public:
309  template <typename T, typename S>
310  int operator () (const T2DImage<T>& src, const T2DImage<S>& ref) const {
311  typename T2DImage<T>::const_iterator si = src.begin();
312  typename T2DImage<T>::const_iterator se = src.end();
313  typename T2DImage<S>::const_iterator ri = ref.begin();
314 
315  while (si != se)
316  cvdebug() << *si++ << " expect " << *ri++ << "\n";
317  return 0;
318  }
319 };
320 
324 template <typename S>
325 struct plugin_data_type<T2DImage<S> > {
326  typedef C2DImage type;
327 };
329 
339 EXPORT_2D bool operator == (const C2DImage& a, const C2DImage& b);
340 
345 inline bool operator != (const C2DImage& a, const C2DImage& b)
346 {
347  return ! (a == b );
348 }
349 
350 
356 
364 public:
365  template <typename T>
366  P2DImage operator () (const T2DImage<T>& image) const {
367  return P2DImage(new T2DImage<T>(image));
368  }
369 };
370 
373 
376 
379 
382 
385 
388 
391 
392 #ifdef LONG_64BIT
395 
398 #endif
399 
402 
405 
414 template <>
415 struct Binder<C2DImage> {
417  typedef __bind_all<T2DImage> Derived;
418 };
419 
423 template <>
424 struct dispatch_attr_string<C2DFVector> {
430  static std::string val2string(const C2DFVector& value) {
431  std::stringstream sval;
432  sval << value.x << " " << value.y;
433  return sval.str();
434  }
440  static C2DFVector string2val(const std::string& str) {
441  std::istringstream sval(str);
442  C2DFVector value;
443  sval >> value.x >> value.y;
444  return value;
445  }
446 };
448 
460 template <typename O>
461 struct FConvert2DImageToPixeltypeO: public TFilter<T2DImage<O> > {
462 
468  template <typename T>
469  T2DImage<O> operator () (const T2DImage<T> &image) const {
470  T2DImage<O> result(image.get_size(), image);
471  copy(image.begin(), image.end(), result.begin());
472  return result;
473  }
474 
481  T2DImage<O> operator () (const C2DImage &image) const {
482  return filter(*this, image);
483  }
484 
491  T2DImage<O> operator () (P2DImage image) const {
492  return filter(*this, *image);
493  }
494 };
495 
496 
504 
507 
508 
510 
511 #endif