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 itkClosingByReconstructionImageFilter_h
19 #define itkClosingByReconstructionImageFilter_h
20 
21 #include "itkImageToImageFilter.h"
22 
23 namespace itk
24 {
25 /** \class ClosingByReconstructionImageFilter
26  * \brief Closing by reconstruction of an image
27  *
28  * This filter is similar to the morphological closing, but contrary
29  * to the mophological closing, the closing by reconstruction
30  * preserves the shape of the components.  The closing by
31  * reconstruction of an image "f" is defined as:
32  *
33  * ClosingByReconstruction(f) = ErosionByReconstruction(f, Dilation(f)).
34  *
35  * Closing by reconstruction not only preserves structures preserved by
36  * the dilation, but also levels raises the contrast of the darkest
37  * regions. If PreserveIntensities is on, a subsequent reconstruction
38  * by dilation using a marker image that is the original image for all
39  * unaffected pixels.
40  *
41  * Closing by reconstruction is described in Chapter 6.3.9 of Pierre
42  * Soille's book "Morphological Image Analysis: Principles and
43  * Applications", Second Edition, Springer, 2003.
44  *
45  * \author Gaetan Lehmann. Biologie du Developpement et de la Reproduction, INRA de Jouy-en-Josas, France.
46  *
47  * \sa GrayscaleMorphologicalClosingImageFilter
48  * \ingroup ImageEnhancement  MathematicalMorphologyImageFilters
49  * \ingroup ITKMathematicalMorphology
50  */
51 template< typename TInputImage, typename TOutputImage, typename TKernel >
52 class ITK_TEMPLATE_EXPORT ClosingByReconstructionImageFilter:
53   public ImageToImageFilter< TInputImage, TOutputImage >
54 {
55 public:
56   ITK_DISALLOW_COPY_AND_ASSIGN(ClosingByReconstructionImageFilter);
57 
58   /** Standard class type aliases. */
59   using Self = ClosingByReconstructionImageFilter;
60   using Superclass = ImageToImageFilter< TInputImage, TOutputImage >;
61   using Pointer = SmartPointer< Self >;
62   using ConstPointer = SmartPointer< const Self >;
63 
64   /** Some convenient type alias. */
65   using InputImageType = TInputImage;
66   using InputImagePointer = typename InputImageType::Pointer;
67   using InputImageConstPointer = typename InputImageType::ConstPointer;
68   using InputImageRegionType = typename InputImageType::RegionType;
69   using InputImagePixelType = typename InputImageType::PixelType;
70 
71   using OutputImageType = TOutputImage;
72   using OutputImagePointer = typename OutputImageType::Pointer;
73   using OutputImageConstPointer = typename OutputImageType::ConstPointer;
74   using OutputImageRegionType = typename OutputImageType::RegionType;
75   using OutputImagePixelType = typename OutputImageType::PixelType;
76 
77   /** Kernel type alias. */
78   using KernelType = TKernel;
79 
80   /** ImageDimension constants */
81   static constexpr unsigned int InputImageDimension = TInputImage::ImageDimension;
82   static constexpr unsigned int OutputImageDimension = TOutputImage::ImageDimension;
83 
84   /** Standard New method. */
85   itkNewMacro(Self);
86 
87   /** Runtime information support. */
88   itkTypeMacro(ClosingByReconstructionImageFilter,
89                ImageToImageFilter);
90 
91   /** Set kernel (structuring element). */
92   itkSetMacro(Kernel, KernelType);
93 
94   /** Get the kernel (structuring element). */
95   itkGetConstReferenceMacro(Kernel, KernelType);
96 
97   /**
98    * Set/Get whether the connected components are defined strictly by
99    * face connectivity or by face+edge+vertex connectivity.  Default is
100    * FullyConnectedOff.  For objects that are 1 pixel wide, use
101    * FullyConnectedOn.
102    */
103   itkSetMacro(FullyConnected, bool);
104   itkGetConstReferenceMacro(FullyConnected, bool);
105   itkBooleanMacro(FullyConnected);
106 
107   /**
108    * Set/Get whether the original intensities of the image retained for
109    * those pixels unaffected by the opening by reconstrcution. If Off,
110    * the output pixel contrast will be reduced. */
111   itkSetMacro(PreserveIntensities, bool);
112   itkGetConstReferenceMacro(PreserveIntensities, bool);
113   itkBooleanMacro(PreserveIntensities);
114 
115 #ifdef ITK_USE_CONCEPT_CHECKING
116   // Begin concept checking
117   itkConceptMacro( InputConvertibleToOutputCheck,
118                    ( Concept::Convertible< InputImagePixelType, OutputImagePixelType > ) );
119   // End concept checking
120 #endif
121 
122 protected:
123   ClosingByReconstructionImageFilter();
124   ~ClosingByReconstructionImageFilter() override = default;
125   void PrintSelf(std::ostream & os, Indent indent) const override;
126 
127   /** ClosingByReconstructionImageFilter needs the entire input be
128    * available. Thus, it needs to provide an implementation of
129    * GenerateInputRequestedRegion(). */
130   void GenerateInputRequestedRegion() override;
131 
132   /** ClosingByReconstructionImageFilter will produce the entire output. */
133   void EnlargeOutputRequestedRegion( DataObject *itkNotUsed(output) ) override;
134 
135   void GenerateData() override;
136 
137 private:
138   /** kernel or structuring element to use. */
139   KernelType m_Kernel;
140   bool       m_FullyConnected;
141   bool       m_PreserveIntensities;
142 }; // end of class
143 } // end namespace itk
144 
145 #ifndef ITK_MANUAL_INSTANTIATION
146 #include "itkClosingByReconstructionImageFilter.hxx"
147 #endif
148 
149 #endif
150