ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkMaskImageFilter.h
Go to the documentation of this file.
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 itkMaskImageFilter_h
19 #define itkMaskImageFilter_h
20 
22 #include "itkNumericTraits.h"
24 #include "itkMath.h"
25 
26 namespace itk
27 {
28 namespace Functor
29 {
35 template< typename TInput, typename TMask, typename TOutput = TInput >
36 class MaskInput
37 {
38 public:
40 
42  {
44  InitializeOutsideValue( static_cast<TOutput*>( nullptr ) );
45  }
46  ~MaskInput() = default;
47  bool operator!=(const MaskInput &) const
48  {
49  return false;
50  }
51 
52  bool operator==(const MaskInput & other) const
53  {
54  return !( *this != other );
55  }
56 
57  inline TOutput operator()(const TInput & A, const TMask & B) const
58  {
59  if ( B != m_MaskingValue )
60  {
61  return static_cast< TOutput >( A );
62  }
63  else
64  {
65  return m_OutsideValue;
66  }
67  }
68 
70  void SetOutsideValue(const TOutput & outsideValue)
71  {
72  m_OutsideValue = outsideValue;
73  }
74 
76  const TOutput & GetOutsideValue() const
77  {
78  return m_OutsideValue;
79  }
80 
82  void SetMaskingValue(const TMask & maskingValue)
83  {
84  m_MaskingValue = maskingValue;
85  }
86 
88  const TMask & GetMaskingValue() const
89  {
90  return m_MaskingValue;
91  }
92 
93 private:
94 
95  template < typename TPixelType >
96  void InitializeOutsideValue( TPixelType * )
97  {
99  }
100 
101  template < typename TValue >
103  {
104  // set the outside value to be of zero length
106  }
107 
108  TOutput m_OutsideValue;
110 };
111 }
112 
146 template< typename TInputImage, typename TMaskImage, typename TOutputImage = TInputImage >
148  public
149  BinaryGeneratorImageFilter< TInputImage, TMaskImage, TOutputImage >
150 
151 {
152 public:
153  ITK_DISALLOW_COPY_AND_ASSIGN(MaskImageFilter);
154 
158 
159  using FunctorType = Functor::MaskInput< typename TInputImage::PixelType,
160  typename TMaskImage::PixelType,
161  typename TOutputImage::PixelType >;
162 
165 
167  itkNewMacro(Self);
168 
170  itkTypeMacro(MaskImageFilter,
172 
174  using MaskImageType = TMaskImage;
175 
180  void SetMaskImage(const MaskImageType *maskImage)
181  {
182  // Process object is not const-correct so the const casting is required.
183  this->SetNthInput( 1, const_cast< MaskImageType * >( maskImage ) );
184  }
186  {
187  return static_cast<const MaskImageType*>(this->ProcessObject::GetInput(1));
188  }
190 
192  void SetOutsideValue(const typename TOutputImage::PixelType & outsideValue)
193  {
194  if ( Math::NotExactlyEquals(this->GetOutsideValue(), outsideValue) )
195  {
196  this->Modified();
197  this->GetFunctor().SetOutsideValue(outsideValue);
198  }
199  }
201 
202  const typename TOutputImage::PixelType & GetOutsideValue() const
203  {
204  return this->GetFunctor().GetOutsideValue();
205  }
206 
208  void SetMaskingValue(const typename TMaskImage::PixelType & maskingValue)
209  {
210  if ( this->GetMaskingValue() != maskingValue )
211  {
212  this->Modified();
213  this->GetFunctor().SetMaskingValue(maskingValue);
214  }
215  }
217 
219  const typename TMaskImage::PixelType & GetMaskingValue() const
220  {
221  return this->GetFunctor().GetMaskingValue();
222  }
223 
224 #ifdef ITK_USE_CONCEPT_CHECKING
225  // Begin concept checking
226  itkConceptMacro( MaskEqualityComparableCheck,
228  itkConceptMacro( InputConvertibleToOutputCheck,
229  ( Concept::Convertible< typename TInputImage::PixelType,
230  typename TOutputImage::PixelType > ) );
231  // End concept checking
232 #endif
233 
234 protected:
235  MaskImageFilter() = default;
236  ~MaskImageFilter() override = default;
237 
238  void PrintSelf(std::ostream & os, Indent indent) const override
239  {
240  Superclass::PrintSelf(os, indent);
241  os << indent << "OutsideValue: " << this->GetOutsideValue() << std::endl;
242  }
243 
245  {
246  using PixelType = typename TOutputImage::PixelType;
247  this->CheckOutsideValue( static_cast<PixelType*>(nullptr) );
248 
249  this->SetFunctor(this->GetFunctor());
250  }
251 
252 private:
253  itkGetConstReferenceMacro(Functor, FunctorType);
255 
257 
258  template < typename TPixelType >
259  void CheckOutsideValue( const TPixelType * ) {}
260 
261  template < typename TValue >
263  {
264  // Check to see if the outside value contains only zeros. If so,
265  // resize it to have the same number of zeros as the output
266  // image. Otherwise, check that the number of components in the
267  // outside value is the same as the number of components in the
268  // output image. If not, throw an exception.
269  VariableLengthVector< TValue > currentValue =
270  this->GetFunctor().GetOutsideValue();
271  VariableLengthVector< TValue > zeroVector( currentValue.GetSize() );
273 
274  if ( currentValue == zeroVector )
275  {
276  zeroVector.SetSize( this->GetOutput()->GetVectorLength() );
277  zeroVector.Fill( NumericTraits< TValue >::ZeroValue() );
278  this->GetFunctor().SetOutsideValue( zeroVector );
279  }
280  else if ( this->GetFunctor().GetOutsideValue().GetSize() !=
281  this->GetOutput()->GetVectorLength() )
282  {
283  itkExceptionMacro(
284  << "Number of components in OutsideValue: "
285  << this->GetFunctor().GetOutsideValue().GetSize()
286  << " is not the same as the "
287  << "number of components in the image: "
288  << this->GetOutput()->GetVectorLength());
289  }
290  }
291 
292 };
293 } // end namespace itk
294 
295 #endif
void PrintSelf(std::ostream &os, Indent indent) const override
void InitializeOutsideValue(TPixelType *)
void PrintSelf(std::ostream &os, Indent indent) const override
const MaskImageType * GetMaskImage()
FunctorType & GetFunctor()
Define numeric traits for std::vector.
TOutput operator()(const TInput &A, const TMask &B) const
bool operator!=(const MaskInput &) const
void SetMaskingValue(const TMask &maskingValue)
The base class for all process objects (source, filters, mappers) in the Insight data processing pipe...
const TOutputImage::PixelType & GetOutsideValue() const
Functor::MaskInput< typename TInputImage::PixelType, typename TMaskImage::PixelType, typename TOutputImage::PixelType > FunctorType
Base class for all process objects that output image data.
void CheckOutsideValue(const VariableLengthVector< TValue > *)
~MaskImageFilter() override=default
Represents an array whose length can be defined at run-time.
const TMaskImage::PixelType & GetMaskingValue() const
virtual const FunctorType & GetFunctor() const
Implements pixel-wise generic operation of two images, or of an image and a constant.
void BeforeThreadedGenerateData() override
const TMask & GetMaskingValue() const
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition: itkMath.h:718
virtual void Modified() const
void InitializeOutsideValue(VariableLengthVector< TValue > *)
MaskImageFilter()=default
DataObject * GetInput(const DataObjectIdentifierType &key)
Return an input.
const TOutput & GetOutsideValue() const
void SetOutsideValue(const TOutput &outsideValue)
Mask an image with a mask.
OutputImageType * GetOutput()
Control indentation during Print() invocation.
Definition: itkIndent.h:49
void CheckOutsideValue(const TPixelType *)
virtual void SetNthInput(DataObjectPointerArraySizeType num, DataObject *input)
void SetMaskImage(const MaskImageType *maskImage)
void Fill(TValue const &v)
#define itkConceptMacro(name, concept)
bool operator==(const MaskInput &other) const
void SetMaskingValue(const typename TMaskImage::PixelType &maskingValue)
void SetOutsideValue(const typename TOutputImage::PixelType &outsideValue)