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