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 itkLabelMapToBinaryImageFilter_h
19 #define itkLabelMapToBinaryImageFilter_h
20 
21 #include "itkLabelMapFilter.h"
22 
23 namespace itk
24 {
25 /** \class LabelMapToBinaryImageFilter
26  * \brief Convert a LabelMap to a binary image.
27  *
28  * LabelMapToBinaryImageFilter to a binary image. All the objects in the image
29  * are used as foreground.  The background values of the original binary image
30  * can be restored by passing this image to the filter with the
31  * SetBackgroundImage() method.
32  *
33  * This implementation was taken from the Insight Journal paper:
34  * https://hdl.handle.net/1926/584  or
35  * http://www.insight-journal.org/browse/publication/176
36  *
37  * \author Gaetan Lehmann. Biologie du Developpement et de la Reproduction,
38  * INRA de Jouy-en-Josas, France.
39  *
40  * \sa LabelMapToLabelImageFilter, LabelMapMaskImageFilter
41  * \ingroup ImageEnhancement  MathematicalMorphologyImageFilters
42  * \ingroup ITKLabelMap
43  */
44 template< typename TInputImage, typename TOutputImage >
45 class ITK_TEMPLATE_EXPORT LabelMapToBinaryImageFilter:
46   public LabelMapFilter< TInputImage, TOutputImage >
47 {
48 public:
49   ITK_DISALLOW_COPY_AND_ASSIGN(LabelMapToBinaryImageFilter);
50 
51   /** Standard class type aliases. */
52   using Self = LabelMapToBinaryImageFilter;
53   using Superclass = LabelMapFilter< TInputImage, TOutputImage >;
54   using Pointer = SmartPointer< Self >;
55   using ConstPointer = SmartPointer< const Self >;
56 
57   /** Some convenient type alias. */
58   using InputImageType = TInputImage;
59   using OutputImageType = TOutputImage;
60   using InputImagePointer = typename InputImageType::Pointer;
61   using InputImageConstPointer = typename InputImageType::ConstPointer;
62   using InputImageRegionType = typename InputImageType::RegionType;
63   using InputImagePixelType = typename InputImageType::PixelType;
64   using LabelObjectType = typename InputImageType::LabelObjectType;
65 
66   using OutputImagePointer = typename OutputImageType::Pointer;
67   using OutputImageConstPointer = typename OutputImageType::ConstPointer;
68   using OutputImageRegionType = typename OutputImageType::RegionType;
69   using OutputImagePixelType = typename OutputImageType::PixelType;
70   using IndexType = typename OutputImageType::IndexType;
71 
72   /** ImageDimension constants */
73   static constexpr unsigned int InputImageDimension = TInputImage::ImageDimension;
74   static constexpr unsigned int OutputImageDimension = TOutputImage::ImageDimension;
75 
76   /** Standard New method. */
77   itkNewMacro(Self);
78 
79   /** Runtime information support. */
80   itkTypeMacro(LabelMapToBinaryImageFilter, ImageToImageFilter);
81 
82   /**
83    * Set/Get the value used as "background" in the output image.
84    * Defaults to NumericTraits<PixelType>::NonpositiveMin().
85    */
86   itkSetMacro(BackgroundValue, OutputImagePixelType);
87   itkGetConstMacro(BackgroundValue, OutputImagePixelType);
88 
89   /**
90    * Set/Get the value used as "foreground" in the output image.
91    * Defaults to NumericTraits<PixelType>::max().
92    */
93   itkSetMacro(ForegroundValue, OutputImagePixelType);
94   itkGetConstMacro(ForegroundValue, OutputImagePixelType);
95 
96   /** Set/Get the background image top be used to restore the background values
97     */
SetBackgroundImage(const OutputImageType * input)98   void SetBackgroundImage(const OutputImageType *input)
99   {
100     // Process object is not const-correct so the const casting is required.
101     this->SetNthInput( 1, const_cast< OutputImageType * >( input ) );
102   }
103 
GetBackgroundImage()104   OutputImageType * GetBackgroundImage()
105   {
106     return static_cast< OutputImageType * >( const_cast< DataObject * >( this->ProcessObject::GetInput(1) ) );
107   }
108 
109   /** Set the input image */
SetInput1(const InputImageType * input)110   void SetInput1(const InputImageType *input)
111   {
112     this->SetInput(input);
113   }
114 
115   /** Set the marker image */
SetInput2(const OutputImageType * input)116   void SetInput2(const OutputImageType *input)
117   {
118     this->SetBackgroundImage(input);
119   }
120 
121 protected:
122   LabelMapToBinaryImageFilter();
123   ~LabelMapToBinaryImageFilter() override = default;
124 
125   /** LabelMapToBinaryImageFilter needs the entire input be
126    * available. Thus, it needs to provide an implementation of
127    * GenerateInputRequestedRegion(). */
128   void GenerateInputRequestedRegion() override;
129 
130   /** LabelMapToBinaryImageFilter will produce the entire output. */
131   void EnlargeOutputRequestedRegion( DataObject *itkNotUsed(output) ) override;
132 
133   void GenerateData() override;
134 
135   void DynamicThreadedGenerateData(const OutputImageRegionType & outputRegionForThread) override;
136 
137   //part of a compile error workaround for GCC 4.8.5-28 (Red Hat) from 20150623
SuperclassDynamicTGD(const OutputImageRegionType & outputRegion)138   void SuperclassDynamicTGD(const OutputImageRegionType & outputRegion)
139   {
140     Superclass::DynamicThreadedGenerateData(outputRegion);
141   }
142 
143   void ThreadedProcessLabelObject(LabelObjectType *labelObject) override;
144 
145   void PrintSelf(std::ostream & os, Indent indent) const override;
146 
147 private:
148   OutputImagePixelType m_BackgroundValue;
149   OutputImagePixelType m_ForegroundValue;
150 }; // end of class
151 } // end namespace itk
152 
153 #ifndef ITK_MANUAL_INSTANTIATION
154 #include "itkLabelMapToBinaryImageFilter.hxx"
155 #endif
156 
157 #endif
158