ITK  4.4.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  {
43  InitializeOutsideValue( static_cast<TOutput*>( NULL ) );
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 < class TPixelType >
95  void InitializeOutsideValue( TPixelType * )
96  {
98  }
99 
100  template < class TValueType >
102  {
103  // set the outside value to be of zero length
105  }
106 
107  TOutput m_OutsideValue;
109 };
110 }
144 template< class TInputImage, class TMaskImage, class TOutputImage = TInputImage >
145 class ITK_EXPORT MaskImageFilter:
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  }
186  const MaskImageType * GetMaskImage()
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()
226  {
227  typedef typename TOutputImage::PixelType PixelType;
228  this->CheckOutsideValue( static_cast<PixelType*>(NULL) );
229  }
230 
231 #ifdef ITK_USE_CONCEPT_CHECKING
232 
233  itkConceptMacro( MaskEqualityComparableCheck,
235  itkConceptMacro( InputConvertibleToOutputCheck,
236  ( Concept::Convertible< typename TInputImage::PixelType,
237  typename TOutputImage::PixelType > ) );
238 
240 #endif
241 
242 protected:
244  virtual ~MaskImageFilter() {}
245 
246  void PrintSelf(std::ostream & os, Indent indent) const
247  {
248  Superclass::PrintSelf(os, indent);
249  os << indent << "OutsideValue: " << this->GetOutsideValue() << std::endl;
250  }
251 
252 private:
253  MaskImageFilter(const Self &); //purposely not implemented
254  void operator=(const Self &); //purposely not implemented
255 
256  template < class TPixelType >
257  void CheckOutsideValue( const TPixelType * ) {}
258 
259  template < class TValue >
260  void CheckOutsideValue( const VariableLengthVector< TValue > * )
261  {
262  // Check to see if the outside value contains only zeros. If so,
263  // resize it to have the same number of zeros as the output
264  // image. Otherwise, check that the number of components in the
265  // outside value is the same as the number of components in the
266  // output image. If not, throw an exception.
267  VariableLengthVector< TValue > currentValue =
268  this->GetFunctor().GetOutsideValue();
269  VariableLengthVector< TValue > zeroVector( currentValue.GetSize() );
270  zeroVector.Fill( NumericTraits< TValue >::Zero );
271 
272  if ( currentValue == zeroVector )
273  {
274  zeroVector.SetSize( this->GetOutput()->GetVectorLength() );
275  zeroVector.Fill( NumericTraits< TValue >::Zero );
276  this->GetFunctor().SetOutsideValue( zeroVector );
277  }
278  else if ( this->GetFunctor().GetOutsideValue().GetSize() !=
279  this->GetOutput()->GetVectorLength() )
280  {
281  itkExceptionMacro(
282  << "Number of components in OutsideValue: "
283  << this->GetFunctor().GetOutsideValue().GetSize()
284  << " is not the same as the "
285  << "number of components in the image: "
286  << this->GetOutput()->GetVectorLength());
287  }
288  }
289 
290 };
291 } // end namespace itk
292 
293 #endif
294