17#ifndef __algo_histogram_h__
18#define __algo_histogram_h__
42 Calibrator (
const size_t number_of_bins = 0,
const bool ignorezero =
false) :
43 min (std::numeric_limits<default_type>::infinity()),
44 max (-std::numeric_limits<default_type>::infinity()),
46 num_bins (number_of_bins),
47 ignore_zero (ignorezero) { }
49 template <
typename value_type>
51 if (std::isfinite(val) && !(ignore_zero && val == 0.0)) {
65 void from_file (
const std::string&);
67 void finalize (
const size_t num_volumes,
const bool is_integer);
70 assert (i < num_bins);
71 return get_min() + (get_bin_width() * (i + 0.5));
75 size_t get_num_bins()
const {
return num_bins; }
78 bool get_ignore_zero()
const {
return ignore_zero; }
84 const bool ignore_zero;
98 using vector_type = Eigen::Array<size_t, Eigen::Dynamic, 1>;
99 using cdf_type = Eigen::Array<default_type, Eigen::Dynamic, 1>;
103 list (vector_type::Zero (
info.get_num_bins())) { }
105 template <
typename value_type>
107 if (std::isfinite(val) && !(
info.get_ignore_zero() && val == 0.0)) {
108 const size_t pos = bin (val);
109 if (pos !=
size_t(
list.size()))
115 template <
typename value_type>
118 if (pos >
size_t(
list.size()))
return size();
123 size_t operator[] (
const size_t index)
const {
127 size_t size()
const {
133 cdf_type cdf()
const;
147 template <
class ImageType>
150 for (
auto l =
Loop(image) (image); l; ++l)
151 result (image.value());
155 template <
class ImageType,
class MaskType>
162 if (!dimensions_match (image, mask, 0, 3))
163 throw Exception (
"Image and mask for histogram calibration do not match");
165 for (
auto l =
Loop(image) (image, mask_replicate); l; ++l) {
166 if (mask_replicate.value())
167 result (image.value());
172 template <
class ImageType>
173 Data generate (ImageType& image,
const size_t num_bins,
const bool ignore_zero =
false)
175 Calibrator calibrator (num_bins, ignore_zero);
177 return generate (calibrator, image);
180 template <
class ImageType,
class MaskType>
181 Data generate (ImageType& image, MaskType& mask,
const size_t num_bins,
const bool ignore_zero =
false)
183 Calibrator calibrator (num_bins, ignore_zero);
185 return generate (calibrator, image, mask);
188 template <
class ImageType>
191 Data result (calibrator);
192 for (
auto l =
Loop(image) (image); l; ++l)
197 template <
class ImageType,
class MaskType>
201 return generate (calibrator, image);
202 if (!dimensions_match (image, mask, 0, 3))
203 throw Exception (
"Image and mask for histogram generation do not match");
204 Data result (calibrator);
206 for (
auto l =
Loop(image) (image, mask_replicate); l; ++l) {
207 if (mask_replicate.value())
219 using vector_type = Eigen::Array<default_type, Eigen::Dynamic, 1>;
Matcher(const Data &input, const Data &target)
default_type operator()(const default_type) const
a class to hold a named list of Option's
constexpr I floor(const T x)
template function with cast to different type
FORCE_INLINE LoopAlongAxes Loop()
VectorType::Scalar value(const VectorType &coefs, typename VectorType::Scalar cos_elevation, typename VectorType::Scalar cos_azimuth, typename VectorType::Scalar sin_azimuth, int lmax)
Data generate(ImageType &image, const size_t num_bins, const bool ignore_zero=false)
const App::OptionGroup Options
void calibrate(Calibrator &result, ImageType &image)
MR::default_type value_type
Eigen::Array< value_type, Eigen::Dynamic, 1 > vector_type
double default_type
the default type used throughout MRtrix
constexpr default_type NaN