ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkFirstOrderTextureHistogram.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18#ifndef itkFirstOrderTextureHistogram_h
19#define itkFirstOrderTextureHistogram_h
20
21#include "itkNumericTraits.h"
22#include "itkMath.h"
23#include <map>
24
25namespace itk
26{
27namespace Function
28{
29
30/* \class FirstOrderTextureHistogram
31 *
32 * An implementation of the "MovingHistogram" interface for the
33 * MovingHistogramImageFilter class. This implementation maintains a
34 * std::map based "histogram" during iteration and computes first
35 * order statistics from the histogram.
36 *
37 * \ingroup TextureFeatures
38 */
39template <class TInputPixel, class TOutputPixel>
40class ITK_TEMPLATE_EXPORT FirstOrderTextureHistogram
41{
42public:
44
45 void
46 AddPixel(const TInputPixel & p)
47 {
48 m_Map[p]++;
49 ++m_Count;
50 }
51
52 void
53 RemovePixel(const TInputPixel & p)
54 {
55
56 // insert new item if one doesn't exist
57 auto it = m_Map.find(p);
58
59 assert(it != m_Map.end());
60
61 if (--(it->second) == 0)
62 {
63 m_Map.erase(it);
64 }
65 --m_Count;
66 }
67
68 TOutputPixel
69 GetValue(const TInputPixel &)
70 {
71 TOutputPixel out;
73
74 double sum = 0.0;
75 double sum2 = 0.0;
76 double sum3 = 0.0;
77 double sum4 = 0.0;
78 const size_t count = m_Count;
79
80 double entropy = 0.0;
81 size_t curCount = 0;
82
83 for (auto i = m_Map.begin(); i != m_Map.end(); ++i)
84 {
85 double t = double(i->first) * double(i->second);
86 sum += t;
87 sum2 += (t *= double(i->first));
88 sum3 += (t *= double(i->first));
89 sum4 += (t *= double(i->first));
90
91 curCount += i->second;
92
93 const double p_x = double(i->second) / count;
94 entropy += -p_x * std::log(p_x) / itk::Math::ln2;
95 }
96
97 const double mean = sum / count;
98
99 // unbiased estimate (requires at least 2 samples)
100 const double variance = (count > 1) ? (sum2 - (sum * sum) / count) / (count - 1) : 0.0;
101 const double sigma = std::sqrt(variance);
102 double skewness = 0.0;
103 double kurtosis = 0.0;
104 if (std::abs(variance * sigma) > itk::NumericTraits<double>::min())
105 {
106
107 skewness = ((sum3 - 3.0 * mean * sum2) / count + 2.0 * mean * mean * mean) / (variance * sigma);
108 }
109 if (std::abs(variance) > itk::NumericTraits<double>::min())
110 {
111 kurtosis = (sum4 / count + mean * (-4.0 * sum3 / count + mean * (6.0 * sum2 / count - 3.0 * mean * mean))) /
112 (variance * variance) -
113 3.0;
114 }
115
116 unsigned int i = 0;
117 out[i++] = mean;
118 out[i++] = m_Map.begin()->first;
119 out[i++] = m_Map.rbegin()->first;
120 out[i++] = variance;
121 out[i++] = sigma;
122 out[i++] = skewness;
123 out[i++] = kurtosis;
124 out[i++] = entropy;
125 return out;
126 }
127
128 void
130 {}
131
132 void
135
136private:
137 using MapType = typename std::map<TInputPixel, size_t>;
138
140 size_t m_Count;
141};
142
143} // end namespace Function
144} // end namespace itk
145#endif
typename std::map< TInputPixel, vcl_size_t > MapType
static constexpr T min(const T &)
static void SetLength(T &m, const unsigned int s)
constexpr double ln2
Definition itkMath.h:61
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....