NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
generateThreshold.hpp
Go to the documentation of this file.
1
28
29#pragma once
30
31#include <cmath>
32
36#include "NumCpp/Core/Types.hpp"
37#include "NumCpp/NdArray.hpp"
39
40namespace nc::imageProcessing
41{
42 //============================================================================
43 // Method Description:
52 template<typename dtype>
53 dtype generateThreshold(const NdArray<dtype>& inImageArray, double inRate)
54 {
56
57 if (inRate < 0. || inRate > 1.)
58 {
59 THROW_INVALID_ARGUMENT_ERROR("input rate must be of the range [0, 1]");
60 }
61
62 // first build a histogram
63 auto minValue = static_cast<int32>(std::floor(inImageArray.min().item()));
64 auto maxValue = static_cast<int32>(std::floor(inImageArray.max().item()));
65
66 if (utils::essentiallyEqual(inRate, 0.))
67 {
68 return static_cast<dtype>(maxValue);
69 }
70
71 if (utils::essentiallyEqual(inRate, 1.))
72 {
74 {
75 return static_cast<dtype>(minValue - 1);
76 }
77
78 return dtype{ 0 };
79 }
80
81 const auto histSize = static_cast<uint32>(maxValue - minValue + 1);
82
83 NdArray<double> histogram(1, histSize);
84 histogram.zeros();
85 for (auto intensity : inImageArray)
86 {
87 const auto bin = static_cast<uint32>(static_cast<int32>(std::floor(intensity)) - minValue);
88 ++histogram[bin];
89 }
90
91 // integrate the normalized histogram from right to left to make a survival function (1 - CDF)
92 const auto dNumPixels = static_cast<double>(inImageArray.size());
93 NdArray<double> survivalFunction(1, histSize + 1);
94 survivalFunction[-1] = 0.;
95 for (int32 i = histSize - 1; i > -1; --i)
96 {
97 double histValue = histogram[i] / dNumPixels;
98 survivalFunction[i] = survivalFunction[i + 1] + histValue;
99 }
100
101 // binary search through the survival function to find the rate
102 uint32 indexLow = 0;
103 uint32 indexHigh = histSize - 1;
104 uint32 index = indexHigh / 2; // integer division
105
106 constexpr bool keepGoing = true;
107 while (keepGoing)
108 {
109 const double value = survivalFunction[index];
110 if (value < inRate)
111 {
112 indexHigh = index;
113 }
114 else if (value > inRate)
115 {
116 indexLow = index;
117 }
118 else
119 {
120 const int32 thresh = static_cast<int32>(index) + minValue - 1;
122 {
123 return static_cast<dtype>(thresh);
124 }
125
126 return thresh < 0 ? 0 : static_cast<dtype>(thresh);
127 }
128
129 if (indexHigh - indexLow < 2)
130 {
131 return static_cast<dtype>(static_cast<int32>(indexHigh) + minValue - 1);
132 }
133
134 index = indexLow + (indexHigh - indexLow) / 2;
135 }
136
137 // shouldn't ever get here but stop the compiler from throwing a warning
138 return static_cast<dtype>(histSize - 1);
139 }
140
141} // namespace nc::imageProcessing
#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