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 itkShapePriorSegmentationLevelSetImageFilter_h
19 #define itkShapePriorSegmentationLevelSetImageFilter_h
20 
21 #include "itkSegmentationLevelSetImageFilter.h"
22 #include "itkShapePriorSegmentationLevelSetFunction.h"
23 #include "itkSingleValuedNonLinearOptimizer.h"
24 #include "itkShapePriorMAPCostFunctionBase.h"
25 #include "itkMath.h"
26 
27 namespace itk
28 {
29 /**
30  *
31  * \class ShapePriorSegmentationLevelSetImageFilter
32  *
33  * \brief A base class which defines the API for implementing a level set
34  * segmentation filter with statistical shape influence.
35  *
36  * \par OVERVIEW
37  * This class extends the functionality of SegmentationLevelSetImageFilter
38  * with an additional statistical shape influence term in the level set evolution as
39  * developed in [1].
40  *
41  * \par TEMPLATE PARAMETERS
42  * There are two required and one optional template parameter for these
43  * filters.
44  *
45  * TInputImage is the image type of the initial model you will input to the
46  * filter using SetInput() or SetInitialImage().
47  *
48  * TFeatureImage is the image type of the image from which the filter will
49  * calculate the speed term for segmentation (see INPUTS).
50  *
51  * TOutputPixelType is the data type used for the output image phi, the implicit
52  * level set image.  This should really only ever be set as float (default) or
53  * double.
54  *
55  *
56  * \par PARAMETERS
57  *
58  * \par
59  * From a level set evolution point of view, the shape is represented by a
60  * signed distance function from the shape encapsulated in a ShapeSignedDistanceFunction
61  * object.
62  *
63  * \sa ShapeSignedDistanceFunction
64  * \sa ShapePriorSegmentationLevelSetFunction
65  *
66  * \par REFERENCES
67  * \par
68  * [1] Leventon, M.E. et al. "Statistical Shape Influence in Geodesic Active Contours", CVPR 2000.
69  *
70  * \ingroup ITKLevelSets
71  */
72 template< typename TInputImage,
73           typename TFeatureImage,
74           typename TOutputPixelType = float >
75 class ITK_TEMPLATE_EXPORT ShapePriorSegmentationLevelSetImageFilter:
76   public SegmentationLevelSetImageFilter< TInputImage, TFeatureImage,
77                                           TOutputPixelType >
78 {
79 public:
80   ITK_DISALLOW_COPY_AND_ASSIGN(ShapePriorSegmentationLevelSetImageFilter);
81 
82   /** Dimension of the input/level set image. */
83   static constexpr unsigned int ImageDimension = TInputImage::ImageDimension;
84 
85   /** Standard class type aliases */
86   using Self = ShapePriorSegmentationLevelSetImageFilter;
87   using Superclass =
88       SegmentationLevelSetImageFilter< TInputImage, TFeatureImage, TOutputPixelType >;
89   using Pointer = SmartPointer< Self >;
90   using ConstPointer = SmartPointer< const Self >;
91 
92   /** Run-time type information (and related methods). */
93   itkTypeMacro(ShapePriorSegmentationLevelSetImageFilter, SegmentationLevelSetImageFilter);
94 
95   /** Inherited type alias from the superclass. */
96   using ValueType = typename Superclass::ValueType;
97   using OutputImageType = typename Superclass::OutputImageType;
98   using FeatureImageType = typename Superclass::FeatureImageType;
99 
100   /** Type of the output pixel. */
101   using OutputPixelType = TOutputPixelType;
102 
103   /** The level set function with shape prior type */
104   using ShapePriorSegmentationFunctionType = ShapePriorSegmentationLevelSetFunction< OutputImageType,
105                                                   FeatureImageType >;
106 
107   /** The shape signed distance function type. */
108   using ShapeFunctionType = typename ShapePriorSegmentationFunctionType::ShapeFunctionType;
109   using ShapeFunctionPointer = typename ShapeFunctionType::Pointer;
110 
111   /** The type of the MAP estimate cost function. */
112   using CostFunctionType = ShapePriorMAPCostFunctionBase< TFeatureImage, TOutputPixelType >;
113   using CostFunctionPointer = typename CostFunctionType::Pointer;
114   using ParametersType = typename CostFunctionType::ParametersType;
115 
116   /** Type of node used to represent the active region around the zero set. */
117   using NodeType = typename CostFunctionType::NodeType;
118   using NodeContainerType = typename CostFunctionType::NodeContainerType;
119   using NodeContainerPointer = typename NodeContainerType::Pointer;
120 
121   /** The type of optimizer used to compute the MAP estimate of the shape and
122     pose parameters. */
123   using OptimizerType = SingleValuedNonLinearOptimizer;
124   using OptimizerPointer = typename OptimizerType::Pointer;
125 
126   /** Set/Get the shape signed distance function. */
127   virtual void SetShapeFunction(ShapeFunctionType *s);
128   itkGetModifiableObjectMacro(ShapeFunction, ShapeFunctionType);
129 
130   /** Set/Get the shape prior MAP cost function. */
131   itkSetObjectMacro(CostFunction, CostFunctionType);
132   itkGetModifiableObjectMacro(CostFunction, CostFunctionType);
133 
134   /** Set/Get the optimizer. */
135   itkSetObjectMacro(Optimizer, OptimizerType);
136   itkGetModifiableObjectMacro(Optimizer, OptimizerType);
137 
138   /** Set/Get the initial parameters. These are the initial parameters applied
139    * to the ShapeFunction. The user should refer to the documentation of
140    * the particular type of ShapeSignedDistanceFunction used to determine
141    * the meaning of the parameters. */
142   itkSetMacro(InitialParameters, ParametersType);
143   itkGetConstMacro(InitialParameters, ParametersType);
144 
145   /** Set/Get the scaling of the shape prior term. */
SetShapePriorScaling(ValueType v)146   void SetShapePriorScaling(ValueType v)
147   {
148     if ( Math::NotExactlyEquals(v, m_ShapePriorSegmentationFunction->GetShapePriorWeight()) )
149       {
150       m_ShapePriorSegmentationFunction->SetShapePriorWeight(v);
151       this->Modified();
152       }
153   }
154 
GetShapePriorScaling()155   ValueType GetShapePriorScaling() const
156   {
157     return m_ShapePriorSegmentationFunction->GetShapePriorWeight();
158   }
159 
160   /** Set the shape prior segmentation function. In general, this should only be called
161    * by a subclass of this object. It is made public to allow itk::Command objects access. */
162   virtual void SetShapePriorSegmentationFunction(ShapePriorSegmentationFunctionType *s);
163 
GetShapePriorSegmentationFunction()164   virtual ShapePriorSegmentationFunctionType * GetShapePriorSegmentationFunction()
165   { return m_ShapePriorSegmentationFunction; }
166 
167   /** Get the current parameters. */
168   itkGetConstReferenceMacro(CurrentParameters, ParametersType);
169 
170 protected:
171   ~ShapePriorSegmentationLevelSetImageFilter() override = default;
172   ShapePriorSegmentationLevelSetImageFilter();
173 
174   void PrintSelf(std::ostream & os, Indent indent) const override;
175 
176   /** Overrides parent implementation. MAP estimates of the shape and pose parameters
177    is computed in this method. */
178   void InitializeIteration() override;
179 
180   /** Overridden from ProcessObject to set certain values before starting the
181    * finite difference solver and then create an appropriate output */
182   void GenerateData() override;
183 
184   /** Extract node of active region into a NodeContainer */
185   void ExtractActiveRegion(NodeContainerType *ptr);
186 
187 private:
188   ShapeFunctionPointer m_ShapeFunction;
189   CostFunctionPointer  m_CostFunction;
190   OptimizerPointer     m_Optimizer;
191   ParametersType       m_InitialParameters;
192   ParametersType       m_CurrentParameters;
193 
194   ShapePriorSegmentationFunctionType *m_ShapePriorSegmentationFunction;
195 };
196 } // end namespace itk
197 
198 #ifndef ITK_MANUAL_INSTANTIATION
199 #include "itkShapePriorSegmentationLevelSetImageFilter.hxx"
200 #endif
201 
202 #endif
203