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 itkImageClassifierBase_h
19 #define itkImageClassifierBase_h
20 
21 #include "itkClassifierBase.h"
22 #include "itkMacro.h"
23 #include "itkImageRegionIterator.h"
24 
25 namespace itk
26 {
27 /** \class ImageClassifierBase
28  * \brief Base class for the ImageClassifierBase object.
29  *
30  * itkImageClassifierBase is the base class for algorithms
31  * that take input data as images and preserve the image structure
32  * while performing classification. In other words, the data is not
33  * converted into a list, hence filters that require spatial information
34  * of a pixel can use the subclasses under this tree. It provides
35  * the basic function definitions that are inherent to an image classifier
36  * objects.
37  *
38  * This is the Superclass for the image classifier tree of the classifier
39  * framework. This is the class for all the classification objects available
40  * through the classifier framework in the ITK toolkit that hold the input
41  * image and the classified image data.
42  *
43  * It is templated over the type of input image, classified image. The second
44  * template parameter allows templating over the classified image type. The
45  * name "image" indicates that the basic data structure used for storing
46  * data/results are derived from the ITK image class.
47  *
48  * This object supports data handling of multiband images. The object
49  * accepts the input image in vector format only, where each pixel is a
50  * vector and each element of the vector corresponds to an entry from
51  * 1 particular band of a multiband dataset. A single band image is treated
52  * as a vector image with a single element for every vector. The classified
53  * image is treated as a single band scalar image.
54  *
55  * This class stores the input and output data as its private members.
56  * Before you call the Classify method to start the classification process,
57  * you should plug in all necessary parts as described in the superclass
58  * documentation.
59  *
60  * The core computation is carried out here. The function requires that
61  * the number of classes be set to a non zero value and the membership
62  * functions be populated. In addition, the number of classes should be equal
63  * to the number of membership functions.
64  *
65  * \ingroup ImageClassificationFilters
66  * \ingroup ITKClassifiers
67  */
68 
69 template< typename TInputImage,
70           typename TClassifiedImage >
71 class ITK_TEMPLATE_EXPORT ImageClassifierBase:
72   public ClassifierBase< TInputImage >
73 {
74 public:
75   ITK_DISALLOW_COPY_AND_ASSIGN(ImageClassifierBase);
76 
77   /** Standard class type aliases. */
78   using Self = ImageClassifierBase;
79   using Superclass = ClassifierBase< TInputImage >;
80   using Pointer = SmartPointer< Self >;
81   using ConstPointer = SmartPointer< const Self >;
82 
83   /** Method for creation through the object factory. */
84   itkNewMacro(Self);
85 
86   /** Run-time type information (and related methods). */
87   itkTypeMacro(ImageClassifierBase, ClassifierBase);
88 
89   /** Type definition for the input image. */
90   using InputImageType = TInputImage;
91   using InputImagePointer = typename TInputImage::Pointer;
92   using InputImageConstPointer = typename TInputImage::ConstPointer;
93 
94   /** Type definitions for the classified image pixel type. */
95   using ClassifiedImagePointer = typename TClassifiedImage::Pointer;
96 
97   /** Type definitions from the Superclass */
98 
99   /**Set the decision rule */
100   using MeasurementVectorType = typename Superclass::MeasurementVectorType;
101 
102   /** Typedefs for membership funciton */
103   using MembershipFunctionType = typename Superclass::MembershipFunctionType;
104 
105   using MembershipFunctionPointer = typename Superclass::MembershipFunctionPointer;
106 
107   using MembershipFunctionPointerVector = typename Superclass::MembershipFunctionPointerVector;
108 
109   /** Type alias for decision rule */
110   using DecisionRuleType = typename Superclass::DecisionRuleType;
111 
112   /** Get/Set the input image. */
113   itkSetConstObjectMacro(InputImage, InputImageType);
114   itkGetConstObjectMacro(InputImage, InputImageType);
115 
116   /** Set the classified image. */
117   itkSetMacro(ClassifiedImage, ClassifiedImagePointer);
118 
119   /** Get the classified image. */
120   itkGetConstMacro(ClassifiedImage, ClassifiedImagePointer);
121 
122   /** Type definition for the vector associated with
123     * input image pixel type. */
124   using InputImagePixelType = typename TInputImage::PixelType;
125 
126   /** Type definitions for the vector holding
127    * training image pixel type. */
128   using ClassifiedImagePixelType = typename TClassifiedImage::PixelType;
129 
130   /** Type definition for the input image/training iterator */
131   using InputImageConstIterator = ImageRegionConstIterator<TInputImage>;
132   using ClassifiedImageIterator = ImageRegionIterator<TClassifiedImage>;
133 
134   /** Method to get the membership of a given pixel to the different classes */
135   std::vector< double >
136   GetPixelMembershipValue(const InputImagePixelType inputImagePixel);
137 
138 protected:
139   ImageClassifierBase() = default;
140   ~ImageClassifierBase() override = default;
141   void PrintSelf(std::ostream & os, Indent indent) const override;
142 
143   /** Allocate memory for the classified image. */
144   void Allocate();
145 
146   /** Starts the classification process */
147   void GenerateData() override;
148 
149 private:
150   using InputImageSizeType = typename TInputImage::SizeType;
151 
152   InputImageConstPointer m_InputImage;
153   ClassifiedImagePointer m_ClassifiedImage;
154 
155   /** Define a virtual Classifier function to classify the whole image. */
156   virtual void Classify();
157 }; // class ImageClassifierBase
158 } // namespace itk
159 
160 #ifndef ITK_MANUAL_INSTANTIATION
161 #include "itkImageClassifierBase.hxx"
162 #endif
163 
164 #endif
165