sparse_histogram.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-2017 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 #include <mia/core/filter.hh>
23 #include <vector>
24 #include <type_traits>
25 #include <limits>
26 
28 
38 class EXPORT_CORE CSparseHistogram : public TFilter<size_t> {
39 
40 public:
41  typedef std::vector<std::pair<int, unsigned long>> Compressed;
42 
44 
52  template <typename InIterator>
53  size_t operator ()(InIterator begin, InIterator end);
54 
55  template <typename Image>
56  size_t operator ()(const Image& image) {
57  return (*this)(image.begin(), image.end());
58  }
59 
63  Compressed get_compressed_histogram()const;
64  private:
65  std::vector<uint64_t> m_histogram;
66  int64_t m_shift;
67  EPixelType m_pixeltype;
68 };
69 
70 EXPORT_CORE std::ostream& operator << (std::ostream& os, const std::pair<short, uint64_t>& pair);
71 
72 // Implementation
73 
75 
76 template <typename InIterator, bool sig>
77 struct dispatch_by_pixeltype {
78  static size_t apply(InIterator MIA_PARAM_UNUSED(begin), InIterator MIA_PARAM_UNUSED(end),
79  std::vector<uint64_t>& MIA_PARAM_UNUSED(histogram)){
80  throw std::invalid_argument("Input pixel type not supported");
81  }
82 };
83 
84 template <typename InIterator>
85 struct dispatch_by_pixeltype<InIterator, false> {
86  static size_t apply(InIterator begin, InIterator end, std::vector<uint64_t>& histogram){
87  size_t n = 0;
88  while ( begin != end) {
89  ++histogram[*begin];
90  ++begin;
91  ++n;
92  }
93  return n;
94  }
95 };
96 
97 template <typename InIterator>
98 struct dispatch_by_pixeltype<InIterator, true> {
99  static size_t apply(InIterator begin, InIterator end, std::vector<uint64_t>& histogram){
100  typedef typename InIterator::value_type in_pixels;
101  int64_t shift = -std::numeric_limits<in_pixels>::min();
102  size_t n = 0;
103  while ( begin != end) {
104  ++histogram[*begin + shift];
105  ++begin;
106  ++n;
107  }
108  return n;
109  }
110 };
111 
112 
113 template <typename InIterator>
114 size_t CSparseHistogram::operator ()(InIterator begin, InIterator end)
115 {
116  typedef typename InIterator::value_type in_pixeltype;
117  if (m_pixeltype ==it_none) {
118  m_pixeltype = pixel_type<in_pixeltype>::value;
119  m_shift = -std::numeric_limits<in_pixeltype>::min();
120  switch (m_pixeltype) {
121  case it_sbyte:
122  case it_ubyte:
123  m_histogram.resize(256);
124  break;
125  case it_sshort:
126  case it_ushort:
127  m_histogram.resize(65536);
128  break;
129  default:
130  throw create_exception<std::invalid_argument>("Input pixel type '",
131  CPixelTypeDict.get_name(m_pixeltype),
132  "' not supported.");
133  }
134 
135  } else if (m_pixeltype != pixel_type<in_pixeltype>::value){
136  throw create_exception<std::invalid_argument>("Input pixels not of consisted type, started with ",
137  CPixelTypeDict.get_name(m_pixeltype), ", but got now ",
138  CPixelTypeDict.get_name(pixel_type<in_pixeltype>::value));
139  }
140 
141  const bool is_signed = std::is_signed<in_pixeltype>::value;
142 
143  size_t n = 0;
144  n += dispatch_by_pixeltype<InIterator, is_signed>::apply(begin, end, m_histogram);
145  return n;
146 }
147 
149 
EPixelType
Definition: pixeltype.hh:32
A sparse histogram.
base class for all filer type functors.
Definition: core/filter.hh:70
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
EXPORT_CORE const TDictMap< EPixelType > CPixelTypeDict
dictionary for the pixel types
std::vector< std::pair< int, unsigned long > > Compressed
size_t operator()(InIterator begin, InIterator end)
#define EXPORT_CORE
Macro to manage Visual C++ style dllimport/dllexport.
Definition: defines.hh:101
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:36