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 itkHistogramToImageFilter_h
19 #define itkHistogramToImageFilter_h
20 
21 #include "itkImageSource.h"
22 #include "itkConceptChecking.h"
23 #include "itkHistogram.h"
24 #include "itkImageRegionIteratorWithIndex.h"
25 #include "itkSimpleDataObjectDecorator.h"
26 
27 namespace itk
28 {
29 /** \class HistogramToImageFilter
30  *  \brief This class takes a histogram as an input and returns an image of
31  *  type specified by the functor.
32  *
33  *  The dimension of the image is equal to the size of each measurement
34  *  vector of the histogram. The size in the image along each dimension will be
35  *  equal to the number of bins along each dimension of the histogram.
36  *
37  *  The filter may be used in registration methods to plot the joint histogram
38  *  after every iteration. A functor is used since it is customary to plot
39  *  p log p    where p is the probability of each measurement vector
40  *  p is given by Number of occurrences of the measurement vector / total number
41  *  of occurrences of all measurement vectors.
42  *
43  *  \sa HistogramToProbabilityImageFilter, HistogramToLogProbabilityImageFilter,
44  *  HistogramToIntensityImageFilter, HistogramToEntropyImageFilter
45  *
46  * \ingroup ITKStatistics
47  */
48 
49 template< typename THistogram, typename TImage, typename TFunction >
50 class ITK_TEMPLATE_EXPORT HistogramToImageFilter:
51   public ImageSource< TImage >
52 {
53 public:
54   ITK_DISALLOW_COPY_AND_ASSIGN(HistogramToImageFilter);
55 
56   /** Standard class type aliases. */
57   using FunctorType = TFunction;
58   using Self = HistogramToImageFilter;
59   using Superclass = ImageSource< TImage >;
60   using Pointer = SmartPointer< Self >;
61   using ConstPointer = SmartPointer< const Self >;
62 
63   using OutputImageType = TImage;
64   using OutputImagePointer = typename Superclass::Pointer;
65   using SpacingType = typename OutputImageType::SpacingType;
66   using PointType = typename OutputImageType::PointType;
67   using OutputPixelType = typename OutputImageType::PixelType;
68 
69   // Define an iterator to iterate through the image
70   using ImageIteratorType = itk::ImageRegionIteratorWithIndex< OutputImageType >;
71 
72   /** Method for creation through the object factory. */
73   itkNewMacro(Self);
74 
75   /** Run-time type information (and related methods). */
76   itkTypeMacro(HistogramToImageFilter, ImageSource);
77 
78   /** Superclass type alias. */
79   using OutputImageRegionType = typename Superclass::OutputImageRegionType;
80 
81   /** Some convenient type alias. */
82   using HistogramType = THistogram;
83   using MeasurementVectorType = typename HistogramType::MeasurementVectorType;
84   using HistogramSizeType = typename HistogramType::SizeType;
85   using SizeType = typename OutputImageType::SizeType;
86 
87   /** Determine the image dimension. */
88   static constexpr unsigned int ImageDimension = OutputImageType::ImageDimension;
89 
90   /** Set/Get the input of this process object.  */
91   using Superclass::SetInput;
92   virtual void SetInput(const HistogramType *histogram);
93 
94   const HistogramType * GetInput();
95 
96   /** Set the functor object.  This replaces the current Functor with a
97    * copy of the specified Functor. This allows the user to specify a
98    * functor that has ivars set differently than the default functor.
99    * This method requires an operator!=() be defined on the functor
100    * (or the compiler's default implementation of operator!=() being
101    * appropriate). */
SetFunctor(const FunctorType & functor)102   void SetFunctor(const FunctorType & functor)
103   {
104     m_Functor = functor;
105     this->Modified();
106   }
107 
108   /** Get the functor object.  The functor is returned by reference.
109    * (Functors do not have to derive from itk::LightObject, so they do
110    * not necessarily have a reference count. So we cannot return a
111    * SmartPointer.) */
GetFunctor()112   FunctorType & GetFunctor() { return m_Functor; }
GetFunctor()113   const FunctorType & GetFunctor() const { return m_Functor; }
114 
115   void SetTotalFrequency(SizeValueType n);
116 
117 protected:
118   HistogramToImageFilter();
119   ~HistogramToImageFilter() override = default;
120 
121   void GenerateOutputInformation() override;
122 
123   void GenerateData() override;
124 
125   FunctorType m_Functor;
126 
127   void PrintSelf(std::ostream & os, Indent indent) const override;
128 };
129 } // end namespace itk
130 
131 #ifndef ITK_MANUAL_INSTANTIATION
132 #include "itkHistogramToImageFilter.hxx"
133 #endif
134 
135 #endif
136