ITK  4.13.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*>( ITK_NULLPTR ) );
45  }
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 }
145 template< typename TInputImage, typename TMaskImage, typename TOutputImage = TInputImage >
147  public
148  BinaryFunctorImageFilter< TInputImage, TMaskImage, TOutputImage,
149  Functor::MaskInput<
150  typename TInputImage::PixelType,
151  typename TMaskImage::PixelType,
152  typename TOutputImage::PixelType > >
153 
154 {
155 public:
158  typedef BinaryFunctorImageFilter< TInputImage, TMaskImage, TOutputImage,
160  typename TInputImage::PixelType,
161  typename TMaskImage::PixelType,
162  typename TOutputImage::PixelType >
164 
167 
169  itkNewMacro(Self);
170 
172  itkTypeMacro(MaskImageFilter,
174 
176  typedef TMaskImage MaskImageType;
177 
182  void SetMaskImage(const MaskImageType *maskImage)
183  {
184  // Process object is not const-correct so the const casting is required.
185  this->SetNthInput( 1, const_cast< MaskImageType * >( maskImage ) );
186  }
188  {
189  return static_cast<const MaskImageType*>(this->ProcessObject::GetInput(1));
190  }
192 
194  void SetOutsideValue(const typename TOutputImage::PixelType & outsideValue)
195  {
196  if ( Math::NotExactlyEquals(this->GetOutsideValue(), outsideValue) )
197  {
198  this->Modified();
199  this->GetFunctor().SetOutsideValue(outsideValue);
200  }
201  }
203 
204  const typename TOutputImage::PixelType & GetOutsideValue() const
205  {
206  return this->GetFunctor().GetOutsideValue();
207  }
208 
210  void SetMaskingValue(const typename TMaskImage::PixelType & maskingValue)
211  {
212  if ( this->GetMaskingValue() != maskingValue )
213  {
214  this->Modified();
215  this->GetFunctor().SetMaskingValue(maskingValue);
216  }
217  }
219 
221  const typename TMaskImage::PixelType & GetMaskingValue() const
222  {
223  return this->GetFunctor().GetMaskingValue();
224  }
225 
226  void BeforeThreadedGenerateData() ITK_OVERRIDE
227  {
228  typedef typename TOutputImage::PixelType PixelType;
229  this->CheckOutsideValue( static_cast<PixelType*>(ITK_NULLPTR) );
230  }
231 
232 #ifdef ITK_USE_CONCEPT_CHECKING
233  // Begin concept checking
234  itkConceptMacro( MaskEqualityComparableCheck,
236  itkConceptMacro( InputConvertibleToOutputCheck,
237  ( Concept::Convertible< typename TInputImage::PixelType,
238  typename TOutputImage::PixelType > ) );
239  // End concept checking
240 #endif
241 
242 protected:
244  virtual ~MaskImageFilter() ITK_OVERRIDE {}
245 
246  void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE
247  {
248  Superclass::PrintSelf(os, indent);
249  os << indent << "OutsideValue: " << this->GetOutsideValue() << std::endl;
250  }
251 
252 private:
253  ITK_DISALLOW_COPY_AND_ASSIGN(MaskImageFilter);
254 
255  template < typename TPixelType >
256  void CheckOutsideValue( const TPixelType * ) {}
257 
258  template < typename TValue >
260  {
261  // Check to see if the outside value contains only zeros. If so,
262  // resize it to have the same number of zeros as the output
263  // image. Otherwise, check that the number of components in the
264  // outside value is the same as the number of components in the
265  // output image. If not, throw an exception.
266  VariableLengthVector< TValue > currentValue =
267  this->GetFunctor().GetOutsideValue();
268  VariableLengthVector< TValue > zeroVector( currentValue.GetSize() );
270 
271  if ( currentValue == zeroVector )
272  {
273  zeroVector.SetSize( this->GetOutput()->GetVectorLength() );
274  zeroVector.Fill( NumericTraits< TValue >::ZeroValue() );
275  this->GetFunctor().SetOutsideValue( zeroVector );
276  }
277  else if ( this->GetFunctor().GetOutsideValue().GetSize() !=
278  this->GetOutput()->GetVectorLength() )
279  {
280  itkExceptionMacro(
281  << "Number of components in OutsideValue: "
282  << this->GetFunctor().GetOutsideValue().GetSize()
283  << " is not the same as the "
284  << "number of components in the image: "
285  << this->GetOutput()->GetVectorLength());
286  }
287  }
288 
289 };
290 } // end namespace itk
291 
292 #endif
void InitializeOutsideValue(TPixelType *)
void PrintSelf(std::ostream &os, Indent indent) const override
const MaskImageType * GetMaskImage()
virtual ~MaskImageFilter() override
TOutput operator()(const TInput &A, const TMask &B) const
bool operator!=(const MaskInput &) const
SmartPointer< const Self > ConstPointer
void SetMaskingValue(const TMask &maskingValue)
const TOutputImage::PixelType & GetOutsideValue() const
BinaryFunctorImageFilter< TInputImage, TMaskImage, TOutputImage, Functor::MaskInput< typename TInputImage::PixelType, typename TMaskImage::PixelType, typename TOutputImage::PixelType > > Superclass
Base class for all process objects that output image data.
unsigned int GetSize(void) const
void CheckOutsideValue(const VariableLengthVector< TValue > *)
Represents an array whose length can be defined at run-time.
NumericTraits< TInput >::AccumulateType AccumulatorType
virtual void PrintSelf(std::ostream &os, Indent indent) const override
const TMaskImage::PixelType & GetMaskingValue() const
void BeforeThreadedGenerateData() override
const TMask & GetMaskingValue() const
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition: itkMath.h:721
virtual void Modified() const
void InitializeOutsideValue(VariableLengthVector< TValue > *)
DataObject * GetInput(const DataObjectIdentifierType &key)
Return an input.
SmartPointer< Self > Pointer
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 *)
Define additional traits for native types such as int or float.
virtual void SetNthInput(DataObjectPointerArraySizeType num, DataObject *input)
void SetMaskImage(const MaskImageType *maskImage)
void Fill(TValue const &v)
#define itkConceptMacro(name, concept)
Implements pixel-wise generic operation of two images, or of an image and a constant.
bool operator==(const MaskInput &other) const
void SetMaskingValue(const typename TMaskImage::PixelType &maskingValue)
void SetOutsideValue(const typename TOutputImage::PixelType &outsideValue)