52 template<
typename dtype>
57 if (inRate < 0. || inRate > 1.)
63 auto minValue =
static_cast<int32>(std::floor(inImageArray.
min().
item()));
64 auto maxValue =
static_cast<int32>(std::floor(inImageArray.
max().
item()));
68 return static_cast<dtype
>(maxValue);
75 return static_cast<dtype
>(minValue - 1);
81 const auto histSize =
static_cast<uint32>(maxValue - minValue + 1);
85 for (
auto intensity : inImageArray)
87 const auto bin =
static_cast<uint32>(
static_cast<int32>(std::floor(intensity)) - minValue);
92 const auto dNumPixels =
static_cast<double>(inImageArray.
size());
94 survivalFunction[-1] = 0.;
95 for (
int32 i = histSize - 1; i > -1; --i)
97 double histValue =
histogram[i] / dNumPixels;
98 survivalFunction[i] = survivalFunction[i + 1] + histValue;
103 uint32 indexHigh = histSize - 1;
104 uint32 index = indexHigh / 2;
106 constexpr bool keepGoing =
true;
109 const double value = survivalFunction[index];
114 else if (value > inRate)
120 const int32 thresh =
static_cast<int32>(index) + minValue - 1;
123 return static_cast<dtype
>(thresh);
126 return thresh < 0 ? 0 : static_cast<dtype>(thresh);
129 if (indexHigh - indexLow < 2)
131 return static_cast<dtype
>(
static_cast<int32>(indexHigh) + minValue - 1);
134 index = indexLow + (indexHigh - indexLow) / 2;
138 return static_cast<dtype
>(histSize - 1);
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition Error.hpp:37
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition StaticAsserts.hpp:39
static constexpr bool isSigned() noexcept
Definition DtypeInfo.hpp:86
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:135
self_type max(Axis inAxis=Axis::NONE) const
Definition NdArrayCore.hpp:3117
size_type size() const noexcept
Definition NdArrayCore.hpp:4600
value_type item() const
Definition NdArrayCore.hpp:3098
self_type min(Axis inAxis=Axis::NONE) const
Definition NdArrayCore.hpp:3161
Definition applyThreshold.hpp:34
dtype generateThreshold(const NdArray< dtype > &inImageArray, double inRate)
Definition generateThreshold.hpp:53
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition essentiallyEqual.hpp:47
std::int32_t int32
Definition Types.hpp:36
NdArray< uint32 > histogram(const NdArray< dtype > &inArray, const NdArray< double > &inBinEdges)
Definition histogram.hpp:56
std::uint32_t uint32
Definition Types.hpp:40