ITK  4.8.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 
25 namespace itk
26 {
27 namespace Functor
28 {
34 template< typename TInput, typename TMask, typename TOutput = TInput >
35 class MaskInput
36 {
37 public:
39 
41  {
43  InitializeOutsideValue( static_cast<TOutput*>( ITK_NULLPTR ) );
44  }
46  bool operator!=(const MaskInput &) const
47  {
48  return false;
49  }
50 
51  bool operator==(const MaskInput & other) const
52  {
53  return !( *this != other );
54  }
55 
56  inline TOutput operator()(const TInput & A, const TMask & B) const
57  {
58  if ( B != m_MaskingValue )
59  {
60  return static_cast< TOutput >( A );
61  }
62  else
63  {
64  return m_OutsideValue;
65  }
66  }
67 
69  void SetOutsideValue(const TOutput & outsideValue)
70  {
71  m_OutsideValue = outsideValue;
72  }
73 
75  const TOutput & GetOutsideValue() const
76  {
77  return m_OutsideValue;
78  }
79 
81  void SetMaskingValue(const TMask & maskingValue)
82  {
83  m_MaskingValue = maskingValue;
84  }
85 
87  const TMask & GetMaskingValue() const
88  {
89  return m_MaskingValue;
90  }
91 
92 private:
93 
94  template < typename TPixelType >
95  void InitializeOutsideValue( TPixelType * )
96  {
98  }
99 
100  template < typename TValue >
102  {
103  // set the outside value to be of zero length
105  }
106 
107  TOutput m_OutsideValue;
109 };
110 }
144 template< typename TInputImage, typename TMaskImage, typename TOutputImage = TInputImage >
146  public
147  BinaryFunctorImageFilter< TInputImage, TMaskImage, TOutputImage,
148  Functor::MaskInput<
149  typename TInputImage::PixelType,
150  typename TMaskImage::PixelType,
151  typename TOutputImage::PixelType > >
152 
153 {
154 public:
157  typedef BinaryFunctorImageFilter< TInputImage, TMaskImage, TOutputImage,
159  typename TInputImage::PixelType,
160  typename TMaskImage::PixelType,
161  typename TOutputImage::PixelType >
163 
166 
168  itkNewMacro(Self);
169 
171  itkTypeMacro(MaskImageFilter,
173 
175  typedef TMaskImage MaskImageType;
176 
181  void SetMaskImage(const MaskImageType *maskImage)
182  {
183  // Process object is not const-correct so the const casting is required.
184  this->SetNthInput( 1, const_cast< MaskImageType * >( maskImage ) );
185  }
187  {
188  return static_cast<const MaskImageType*>(this->ProcessObject::GetInput(1));
189  }
191 
193  void SetOutsideValue(const typename TOutputImage::PixelType & outsideValue)
194  {
195  if ( this->GetOutsideValue() != outsideValue )
196  {
197  this->Modified();
198  this->GetFunctor().SetOutsideValue(outsideValue);
199  }
200  }
202 
203  const typename TOutputImage::PixelType & GetOutsideValue() const
204  {
205  return this->GetFunctor().GetOutsideValue();
206  }
207 
209  void SetMaskingValue(const typename TMaskImage::PixelType & maskingValue)
210  {
211  if ( this->GetMaskingValue() != maskingValue )
212  {
213  this->Modified();
214  this->GetFunctor().SetMaskingValue(maskingValue);
215  }
216  }
218 
220  const typename TMaskImage::PixelType & GetMaskingValue() const
221  {
222  return this->GetFunctor().GetMaskingValue();
223  }
224 
225  void BeforeThreadedGenerateData() ITK_OVERRIDE
226  {
227  typedef typename TOutputImage::PixelType PixelType;
228  this->CheckOutsideValue( static_cast<PixelType*>(ITK_NULLPTR) );
229  }
230 
231 #ifdef ITK_USE_CONCEPT_CHECKING
232  // Begin concept checking
233  itkConceptMacro( MaskEqualityComparableCheck,
235  itkConceptMacro( InputConvertibleToOutputCheck,
236  ( Concept::Convertible< typename TInputImage::PixelType,
237  typename TOutputImage::PixelType > ) );
238  // End concept checking
239 #endif
240 
241 protected:
243  virtual ~MaskImageFilter() {}
244 
245  void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE
246  {
247  Superclass::PrintSelf(os, indent);
248  os << indent << "OutsideValue: " << this->GetOutsideValue() << std::endl;
249  }
250 
251 private:
252  MaskImageFilter(const Self &); //purposely not implemented
253  void operator=(const Self &); //purposely not implemented
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()
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 > *)
void operator=(const Self &)
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
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)