1 /*=========================================================================
2  *
3  *  Copyright Insight Software Consortium
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  *         http://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 itkHistogramToEntropyImageFilter_h
19 #define itkHistogramToEntropyImageFilter_h
20 
21 #include "itkHistogramToImageFilter.h"
22 
23 namespace itk
24 {
25 /** \class HistogramToEntropyImageFilter
26  * \brief The class takes a histogram as an input and gives the entropy
27  * image as the output. A pixel, at position I,  in the output image is given by
28  *
29  * \f[
30  * f(I) = -p \log_2 p
31  * \f]
32  *
33  * where
34  * \f[
35  * p = \frac{q_I}{\sum_{i \in I} q_I}
36  * \f]
37  *  where  \f$q_I\f$ is the frequency of measurement vector, I.
38  *
39  * \f$p\f$ is the frequency of a measurement vector by the sum of all frequencies =
40  * Probability of the the measurement vector
41  *
42  * The output image is of type double.
43  *
44  * This is useful in plotting the joint histograms during registration.
45  *
46  *  \sa HistogramToImageFilter, HistogramToLogProbabilityImageFilter,
47  *  HistogramToIntensityImageFilter, HistogramToProbabilityImageFilter
48  *
49  * \ingroup ITKStatistics
50  */
51 
52 namespace Function
53 {
54 template< typename TInput, typename TOutput = double >
55 class HistogramEntropyFunction
56 {
57 public:
58 
59   //Probability function = Number of occurrences in each bin /
60   //   Total Number of occurrences.
61   //
62   // Returns pixels of float..
63   using OutputPixelType = TOutput;
64 
HistogramEntropyFunction()65   HistogramEntropyFunction()
66     {}
67 
68   ~HistogramEntropyFunction() = default;
69 
operator()70   inline OutputPixelType operator()(const TInput & A) const
71   {
72     if ( A )
73       {
74       const double p = static_cast< OutputPixelType >( A )
75                        / static_cast< OutputPixelType >( m_TotalFrequency );
76       return static_cast< OutputPixelType >( ( -1 ) * p * std::log(p) / std::log(2.0) );
77       }
78     else
79       {
80       const double p = static_cast< OutputPixelType >( A + 1 )
81                        / static_cast< OutputPixelType >( m_TotalFrequency );
82       return static_cast< OutputPixelType >( ( -1 ) * p * std::log(p) / std::log(2.0) );
83       }
84   }
85 
SetTotalFrequency(const SizeValueType n)86   void SetTotalFrequency(const SizeValueType n)
87   {
88     m_TotalFrequency = n;
89   }
90 
GetTotalFrequency()91   SizeValueType GetTotalFrequency() const
92   {
93     return m_TotalFrequency;
94   }
95 
96 private:
97   SizeValueType m_TotalFrequency{1};
98 };
99 }
100 
101 template< typename THistogram, typename TImage=Image< double, 3> >
102 class HistogramToEntropyImageFilter:
103   public HistogramToImageFilter< THistogram, TImage,
104                                  Function::HistogramEntropyFunction< SizeValueType, typename TImage::PixelType > >
105 {
106 public:
107   ITK_DISALLOW_COPY_AND_ASSIGN(HistogramToEntropyImageFilter);
108 
109   /** Standard class type aliases. */
110   using Self = HistogramToEntropyImageFilter;
111 
112   /** Standard "Superclass" type alias. */
113   using Superclass = HistogramToImageFilter< THistogram, TImage,
114                                  Function::HistogramEntropyFunction< SizeValueType, typename TImage::PixelType > >;
115 
116   using Pointer = SmartPointer< Self >;
117   using ConstPointer = SmartPointer< const Self >;
118 
119   /** Run-time type information (and related methods).   */
120   itkTypeMacro(HistogramToEntropyImageFilter, HistogramToImageFilter);
121 
122   /** Method for creation through the object factory. */
123   itkNewMacro(Self);
124 
125 protected:
126   HistogramToEntropyImageFilter() = default;
127   ~HistogramToEntropyImageFilter() override = default;
128 };
129 } // end namespace itk
130 
131 #endif
132