36 #ifndef THRESHOLD_HXX_
37 #define THRESHOLD_HXX_
40 #include <Core/ITKCommon/common.hxx>
60 template <
class image_t>
62 threshold_by_intensity_inplace(
typename image_t::Pointer & image,
63 const double clip_param,
64 const bool keep_pixels_above_threshold =
true,
65 const unsigned int bins = 256,
66 const double bg_offset = 1e-3)
68 typedef itk::ImageRegionConstIterator<image_t> iter_t;
69 typedef typename image_t::IndexType index_t;
72 double v_min = std::numeric_limits<double>::max();
73 double v_max = -v_min;
75 iter_t iter(image, image->GetLargestPossibleRegion());
76 for (iter.GoToBegin(); !iter.IsAtEnd(); ++iter)
78 const double & v = iter.Get();
79 v_min = std::min(v_min, v);
80 v_max = std::max(v_max, v);
84 const double v_rng = v_max - v_min;
92 double clip = v_min + clip_param * v_rng;
96 keep_pixels_above_threshold
97 ? clip - bg_offset * v_rng
98 : clip + bg_offset * v_rng;
100 if (keep_pixels_above_threshold)
102 image = threshold<image_t>(image, clip, v_max, background, v_max);
106 image = threshold<image_t>(image, v_min, clip, v_min, background);
115 template <
class image_t>
116 typename image_t::Pointer
117 threshold_by_intensity(
const image_t * image,
118 const double clip_param,
119 const bool keep_pixels_above_threshold =
true,
120 const unsigned int bins = 256,
121 const double bg_offset = 1e-3)
123 typename image_t::Pointer peaks = cast<image_t, image_t>(image);
124 threshold_by_intensity_inplace(peaks,
126 keep_pixels_above_threshold,
156 template <
class image_t>
158 threshold_by_area_inplace(
typename image_t::Pointer & image,
159 const double clip_param,
160 const bool keep_pixels_above_threshold =
true,
161 const unsigned int bins = 4096,
162 const double bg_offset = 1e-3)
164 typedef itk::ImageRegionConstIterator<image_t> iter_t;
165 typedef typename image_t::IndexType index_t;
168 double v_min = std::numeric_limits<double>::max();
169 double v_max = -v_min;
171 iter_t iter(image, image->GetLargestPossibleRegion());
172 for (iter.GoToBegin(); !iter.IsAtEnd(); ++iter)
174 const double v = iter.Get();
175 v_min = std::min(v_min, v);
176 v_max = std::max(v_max, v);
180 const double v_rng = v_max - v_min;
189 std::vector<unsigned int> pdf;
192 for (iter.GoToBegin(); !iter.IsAtEnd(); ++iter)
194 const double v = iter.Get();
195 const unsigned int bin =
196 (
unsigned int)(
double((v - v_min) / v_rng) * double(bins - 1));
201 std::vector<unsigned int> cdf(bins);
203 for (
unsigned int i = 1; i < bins; i++)
205 cdf[i] = cdf[i - 1] + pdf[i];
209 typename image_t::SizeType size =
210 image->GetLargestPossibleRegion().GetSize();
212 double total_number_of_pixels = 1.0;
213 for (
unsigned int i = 0; i < size.GetSizeDimension(); i++)
215 total_number_of_pixels *= size[i];
220 for (
unsigned int i = 1; i < bins; i++)
222 clip = v_min + (double(i) / double(bins - 1)) * v_rng;
223 if (
double(cdf[i]) >= clip_param * total_number_of_pixels)
231 keep_pixels_above_threshold
232 ? clip - bg_offset * v_rng
233 : clip + bg_offset * v_rng;
235 if (keep_pixels_above_threshold)
237 image = threshold<image_t>(image, clip, v_max, background, v_max);
241 image = threshold<image_t>(image, v_min, clip, v_min, background);
250 template <
class image_t>
251 typename image_t::Pointer
252 threshold_by_area(
const image_t * image,
253 const double clip_param,
254 const bool keep_pixels_above_threshold =
true,
255 const unsigned int bins = 256,
256 const double bg_offset = 1e-3)
258 typename image_t::Pointer peaks = cast<image_t, image_t>(image);
259 threshold_by_area_inplace(peaks,
261 keep_pixels_above_threshold,
268 #endif // THRESHOLD_HXX_