File: histogram.h

package info (click to toggle)
mrtrix3 3.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,712 kB
  • sloc: cpp: 129,776; python: 9,494; sh: 593; makefile: 234; xml: 47
file content (240 lines) | stat: -rw-r--r-- 7,538 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* Copyright (c) 2008-2022 the MRtrix3 contributors.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Covered Software is provided under this License on an "as is"
 * basis, without warranty of any kind, either expressed, implied, or
 * statutory, including, without limitation, warranties that the
 * Covered Software is free of defects, merchantable, fit for a
 * particular purpose or non-infringing.
 * See the Mozilla Public License v. 2.0 for more details.
 *
 * For more details, see http://www.mrtrix.org/.
 */

#ifndef __algo_histogram_h__
#define __algo_histogram_h__

#include <cmath>

#include "image_helpers.h"
#include "types.h"
#include "adapter/replicate.h"
#include "algo/loop.h"

namespace MR
{
  namespace Algo
  {
    namespace Histogram
    {


      extern const App::OptionGroup Options;


      class Calibrator
      { MEMALIGN (Calibrator)

        public:
          Calibrator (const size_t number_of_bins = 0, const bool ignorezero = false) :
              min (std::numeric_limits<default_type>::infinity()),
              max (-std::numeric_limits<default_type>::infinity()),
              bin_width (NaN),
              num_bins (number_of_bins),
              ignore_zero (ignorezero) { }

          template <typename value_type>
          typename std::enable_if<std::is_arithmetic<value_type>::value, bool>::type operator() (const value_type val) {
            if (std::isfinite(val) && !(ignore_zero && val == 0.0)) {
              min = std::min (min, default_type(val));
              max = std::max (max, default_type(val));
              if (!num_bins)
                data.push_back (default_type(val));
            }
            return true;
          }

          template <class T>
          FORCE_INLINE typename std::enable_if<!std::is_arithmetic<T>::value, bool>::type operator() (const T& val) {
            return (*this) (typename T::value_type (val));
          }

          void from_file (const std::string&);

          void finalize (const size_t num_volumes, const bool is_integer);

          default_type get_bin_centre (const size_t i) const {
            assert (i < num_bins);
            return get_min() + (get_bin_width() * (i + 0.5));
          }

          default_type get_bin_width() const { return bin_width; }
          size_t get_num_bins() const { return num_bins; }
          default_type get_min() const { return min; }
          default_type get_max() const { return max; }
          bool get_ignore_zero() const { return ignore_zero; }


        private:
          default_type min, max, bin_width;
          size_t num_bins;
          const bool ignore_zero;
          vector<default_type> data;

          default_type get_iqr();

      };




      class Data
      { MEMALIGN (Data)
        public:

          using vector_type = Eigen::Array<size_t, Eigen::Dynamic, 1>;
          using cdf_type = Eigen::Array<default_type, Eigen::Dynamic, 1>;

          Data (const Calibrator& calibrate) :
              info (calibrate),
              list (vector_type::Zero (info.get_num_bins())) { }

          template <typename value_type>
          bool operator() (const value_type val) {
            if (std::isfinite(val) && !(info.get_ignore_zero() && val == 0.0)) {
              const size_t pos = bin (val);
              if (pos != size_t(list.size()))
                ++list[pos];
            }
            return true;
          }

          template <typename value_type>
          size_t bin (const value_type val) const {
            size_t pos = std::floor ((val - info.get_min()) / info.get_bin_width());
            if (pos > size_t(list.size())) return size();
            return pos;
          }


          size_t operator[] (const size_t index) const {
            assert (index < size_t(list.size()));
            return list[index];
          }
          size_t size() const {
            return list.size();
          }
          const Calibrator& get_calibration() const { return info; }

          const vector_type& pdf() const { return list; }
          cdf_type cdf() const;

          default_type first_min () const;

          default_type entropy () const;

        protected:
          const Calibrator info;
          vector_type list;
          friend class Kernel;
      };


      // Convenience functions for calibrating (& histograming) basic input images
      template <class ImageType>
      void calibrate (Calibrator& result, ImageType& image)
      {
        for (auto l = Loop(image) (image); l; ++l)
          result (image.value());
        result.finalize (image.ndim() > 3 ? image.size(3) : 1, std::is_integral<typename ImageType::value_type>::value);
      }

      template <class ImageType, class MaskType>
      void calibrate (Calibrator& result, ImageType& image, MaskType& mask)
      {
        if (!mask.valid()) {
          calibrate (result, image);
          return;
        }
        if (!dimensions_match (image, mask, 0, 3))
          throw Exception ("Image and mask for histogram calibration do not match");
        Adapter::Replicate<MaskType> mask_replicate (mask, image);
        for (auto l = Loop(image) (image, mask_replicate); l; ++l) {
          if (mask_replicate.value())
            result (image.value());
        }
        result.finalize (image.ndim() > 3 ? image.size(3) : 1, std::is_integral<typename ImageType::value_type>::value);
      }

      template <class ImageType>
      Data generate (ImageType& image, const size_t num_bins, const bool ignore_zero = false)
      {
        Calibrator calibrator (num_bins, ignore_zero);
        calibrate (calibrator, image);
        return generate (calibrator, image);
      }

      template <class ImageType, class MaskType>
      Data generate (ImageType& image, MaskType& mask, const size_t num_bins, const bool ignore_zero = false)
      {
        Calibrator calibrator (num_bins, ignore_zero);
        calibrate (calibrator, image, mask);
        return generate (calibrator, image, mask);
      }

      template <class ImageType>
      Data generate (const Calibrator& calibrator, ImageType& image)
      {
        Data result (calibrator);
        for (auto l = Loop(image) (image); l; ++l)
          result (typename ImageType::value_type (image.value()));
        return result;
      }

      template <class ImageType, class MaskType>
      Data generate (const Calibrator& calibrator, ImageType& image, MaskType& mask)
      {
        if (!mask.valid())
          return generate (calibrator, image);
        if (!dimensions_match (image, mask, 0, 3))
          throw Exception ("Image and mask for histogram generation do not match");
        Data result (calibrator);
        Adapter::Replicate<MaskType> mask_replicate (mask, image);
        for (auto l = Loop(image) (image, mask_replicate); l; ++l) {
          if (mask_replicate.value())
            result (typename ImageType::value_type (image.value()));
        }
        return result;
      }




      class Matcher
      { MEMALIGN (Matcher)

          using vector_type = Eigen::Array<default_type, Eigen::Dynamic, 1>;

        public:
          Matcher (const Data& input, const Data& target);

          default_type operator() (const default_type) const;

        private:
          const Calibrator calib_input, calib_target;
          vector_type mapping;

      };





    }
  }
}

#endif