ITK  4.3.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< class TInput, class TMask, class TOutput = TInput >
35 class MaskInput
36 {
37 public:
39 
41  {
42  InitializeOutsideValue( static_cast<TOutput*>( NULL ) );
43  }
45  bool operator!=(const MaskInput &) const
46  {
47  return false;
48  }
49 
50  bool operator==(const MaskInput & other) const
51  {
52  return !( *this != other );
53  }
54 
55  inline TOutput operator()(const TInput & A, const TMask & B) const
56  {
58  {
59  return static_cast< TOutput >( A );
60  }
61  else
62  {
63  return m_OutsideValue;
64  }
65  }
66 
68  void SetOutsideValue(const TOutput & outsideValue)
69  {
70  m_OutsideValue = outsideValue;
71  }
72 
74  const TOutput & GetOutsideValue() const
75  {
76  return m_OutsideValue;
77  }
78 
79 private:
80 
81  template < class TPixelType >
82  void InitializeOutsideValue( TPixelType * )
83  {
85  }
86 
87  template < class TValueType >
89  {
90  // set the outside value to be of zero length
92  }
93 
94  TOutput m_OutsideValue;
95 };
96 }
131 template< class TInputImage, class TMaskImage, class TOutputImage = TInputImage >
132 class ITK_EXPORT MaskImageFilter:
133  public
134  BinaryFunctorImageFilter< TInputImage, TMaskImage, TOutputImage,
135  Functor::MaskInput<
136  typename TInputImage::PixelType,
137  typename TMaskImage::PixelType,
138  typename TOutputImage::PixelType > >
139 
140 {
141 public:
144  typedef BinaryFunctorImageFilter< TInputImage, TMaskImage, TOutputImage,
146  typename TInputImage::PixelType,
147  typename TMaskImage::PixelType,
148  typename TOutputImage::PixelType >
150 
153 
155  itkNewMacro(Self);
156 
158  itkTypeMacro(MaskImageFilter,
160 
162  typedef TMaskImage MaskImageType;
163 
168  void SetMaskImage(const MaskImageType *maskImage)
169  {
170  // Process object is not const-correct so the const casting is required.
171  this->SetNthInput( 1, const_cast< MaskImageType * >( maskImage ) );
172  }
173  const MaskImageType * GetMaskImage()
174  {
175  return static_cast<const MaskImageType*>(this->ProcessObject::GetInput(1));
176  }
178 
180  void SetOutsideValue(const typename TOutputImage::PixelType & outsideValue)
181  {
182  if ( this->GetOutsideValue() != outsideValue )
183  {
184  this->Modified();
185  this->GetFunctor().SetOutsideValue(outsideValue);
186  }
187  }
189 
190  const typename TOutputImage::PixelType & GetOutsideValue() const
191  {
192  return this->GetFunctor().GetOutsideValue();
193  }
194 
195  void BeforeThreadedGenerateData()
196  {
197  typedef typename TOutputImage::PixelType PixelType;
198  this->CheckOutsideValue( static_cast<PixelType*>(NULL) );
199  }
200 
201 #ifdef ITK_USE_CONCEPT_CHECKING
202 
203  itkConceptMacro( MaskEqualityComparableCheck,
205  itkConceptMacro( InputConvertibleToOutputCheck,
206  ( Concept::Convertible< typename TInputImage::PixelType,
207  typename TOutputImage::PixelType > ) );
208 
210 #endif
211 
212 protected:
214  virtual ~MaskImageFilter() {}
215 
216  void PrintSelf(std::ostream & os, Indent indent) const
217  {
218  Superclass::PrintSelf(os, indent);
219  os << indent << "OutsideValue: " << this->GetOutsideValue() << std::endl;
220  }
221 
222 private:
223  MaskImageFilter(const Self &); //purposely not implemented
224  void operator=(const Self &); //purposely not implemented
225 
226  template < class TPixelType >
227  void CheckOutsideValue( const TPixelType * ) {}
228 
229  template < class TValue >
230  void CheckOutsideValue( const VariableLengthVector< TValue > * )
231  {
232  // Check to see if the outside value contains only zeros. If so,
233  // resize it to have the same number of zeros as the output
234  // image. Otherwise, check that the number of components in the
235  // outside value is the same as the number of components in the
236  // output image. If not, throw an exception.
237  VariableLengthVector< TValue > currentValue =
238  this->GetFunctor().GetOutsideValue();
239  VariableLengthVector< TValue > zeroVector( currentValue.GetSize() );
240  zeroVector.Fill( NumericTraits< TValue >::Zero );
241 
242  if ( currentValue == zeroVector )
243  {
244  zeroVector.SetSize( this->GetOutput()->GetVectorLength() );
245  zeroVector.Fill( NumericTraits< TValue >::Zero );
246  this->GetFunctor().SetOutsideValue( zeroVector );
247  }
248  else if ( this->GetFunctor().GetOutsideValue().GetSize() !=
249  this->GetOutput()->GetVectorLength() )
250  {
251  itkExceptionMacro(
252  << "Number of components in OutsideValue: "
253  << this->GetFunctor().GetOutsideValue().GetSize()
254  << " is not the same as the "
255  << "number of components in the image: "
256  << this->GetOutput()->GetVectorLength());
257  }
258  }
259 
260 };
261 } // end namespace itk
262 
263 #endif
264