Medical Imaging Interaction Toolkit  2018.4.99-663e373e
Medical Imaging Interaction Toolkit
mitkitkMaskImageFilter.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 __itkMaskImageFilter2_h
19 #define __itkMaskImageFilter2_h
20 
21 #include "itkBinaryFunctorImageFilter.h"
22 #include "itkNumericTraits.h"
23 #include "itkVariableLengthVector.h"
25 
26 namespace itk
27 {
28 namespace Functor
29 {
35 template< typename TInput, typename TMask, typename TOutput = TInput >
37 {
38 public:
39  typedef typename NumericTraits< TInput >::AccumulateType AccumulatorType;
40 
42  {
43  m_MaskingValue = NumericTraits< TMask >::ZeroValue();
44  InitializeOutsideValue( static_cast<TOutput*>( nullptr ) );
45  }
47  bool operator!=(const MaskInput2 &) const
48  {
49  return false;
50  }
51 
52  bool operator==(const MaskInput2 & 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  }
87  const TMask & GetMaskingValue() const
88  {
89  return m_MaskingValue;
90  }
91 
92 private:
93 
94  template < typename TPixelType >
95  void InitializeOutsideValue( TPixelType * )
96  {
97  this->m_OutsideValue = NumericTraits< TPixelType >::ZeroValue();
98  }
99 
100  template < typename TValue >
101  void InitializeOutsideValue( VariableLengthVector<TValue> * )
102  {
103  // set the outside value to be of zero length
104  this->m_OutsideValue = VariableLengthVector< TValue >(0);
105  }
106 
107  TOutput m_OutsideValue;
108  TMask m_MaskingValue;
109 };
110 }
143 template< typename TInputImage, typename TMaskImage, typename TOutputImage = TInputImage >
145  public
146  BinaryFunctorImageFilter< TInputImage, TMaskImage, TOutputImage,
147  Functor::MaskInput2<
148  typename TInputImage::PixelType,
149  typename TMaskImage::PixelType,
150  typename TOutputImage::PixelType > >
151 
152 {
153 public:
156  typedef BinaryFunctorImageFilter< TInputImage, TMaskImage, TOutputImage,
158  typename TInputImage::PixelType,
159  typename TMaskImage::PixelType,
160  typename TOutputImage::PixelType >
162 
165 
167  itkNewMacro(Self);
168 
170  itkTypeMacro(MaskImageFilter2,
171  BinaryFunctorImageFilter);
172 
174  typedef TMaskImage MaskImageType;
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  }
185  const MaskImageType * GetMaskImage()
186  {
187  return static_cast<const MaskImageType*>(this->ProcessObject::GetInput(1));
188  }
189 
191  void SetOutsideValue(const typename TOutputImage::PixelType & outsideValue)
192  {
193  if ( this->GetOutsideValue() != outsideValue )
194  {
195  this->Modified();
196  this->GetFunctor().SetOutsideValue(outsideValue);
197  }
198  }
199 
200  const typename TOutputImage::PixelType & GetOutsideValue() const
201  {
202  return this->GetFunctor().GetOutsideValue();
203  }
204 
206  void SetMaskingValue(const typename TMaskImage::PixelType & maskingValue)
207  {
208  if ( this->GetMaskingValue() != maskingValue )
209  {
210  this->Modified();
211  this->GetFunctor().SetMaskingValue(maskingValue);
212  }
213  }
214 
216  const typename TMaskImage::PixelType & GetMaskingValue() const
217  {
218  return this->GetFunctor().GetMaskingValue();
219  }
220 
222  {
223  typedef typename TOutputImage::PixelType PixelType;
224  this->CheckOutsideValue( static_cast<PixelType*>(nullptr) );
225  }
226 
227 #ifdef ITK_USE_CONCEPT_CHECKING
228  // Begin concept checking
229  itkConceptMacro( MaskEqualityComparableCheck,
230  ( Concept::EqualityComparable< typename TMaskImage::PixelType > ) );
231  itkConceptMacro( InputConvertibleToOutputCheck,
232  ( Concept::Convertible< typename TInputImage::PixelType,
233  typename TOutputImage::PixelType > ) );
234  // End concept checking
235 #endif
236 
237 protected:
239  ~MaskImageFilter2() override {}
240 
241  void PrintSelf(std::ostream & os, Indent indent) const override
242  {
243  Superclass::PrintSelf(os, indent);
244  os << indent << "OutsideValue: " << this->GetOutsideValue() << std::endl;
245  }
246 
247 private:
248  MaskImageFilter2(const Self &); //purposely not implemented
249  void operator=(const Self &); //purposely not implemented
250 
251  template < typename TPixelType >
252  void CheckOutsideValue( const TPixelType * ) {}
253 
254  template < typename TValue >
255  void CheckOutsideValue( const VariableLengthVector< TValue > * )
256  {
257  // Check to see if the outside value contains only zeros. If so,
258  // resize it to have the same number of zeros as the output
259  // image. Otherwise, check that the number of components in the
260  // outside value is the same as the number of components in the
261  // output image. If not, throw an exception.
262  VariableLengthVector< TValue > currentValue =
263  this->GetFunctor().GetOutsideValue();
264  VariableLengthVector< TValue > zeroVector( currentValue.GetSize() );
265  zeroVector.Fill( NumericTraits< TValue >::ZeroValue() );
266 
267  if ( currentValue == zeroVector )
268  {
269  zeroVector.SetSize( this->GetOutput()->GetVectorLength() );
270  zeroVector.Fill( NumericTraits< TValue >::ZeroValue() );
271  this->GetFunctor().SetOutsideValue( zeroVector );
272  }
273  else if ( this->GetFunctor().GetOutsideValue().GetSize() !=
274  this->GetOutput()->GetVectorLength() )
275  {
276  itkExceptionMacro(
277  << "Number of components in OutsideValue: "
278  << this->GetFunctor().GetOutsideValue().GetSize()
279  << " is not the same as the "
280  << "number of components in the image: "
281  << this->GetOutput()->GetVectorLength());
282  }
283  }
284 
285 };
286 } // end namespace itk
287 
288 #endif
289 
void SetOutsideValue(const typename TOutputImage::PixelType &outsideValue)
void SetMaskImage(const MaskImageType *maskImage)
NumericTraits< TInput >::AccumulateType AccumulatorType
void SetMaskingValue(const TMask &maskingValue)
#define MITKIMAGESTATISTICS_EXPORT
const TOutput & GetOutsideValue() const
SmartPointer< Self > Pointer
const MaskImageType * GetMaskImage()
bool operator==(const MaskInput2 &other) const
const TOutputImage::PixelType & GetOutsideValue() const
void PrintSelf(std::ostream &os, Indent indent) const override
SmartPointer< const Self > ConstPointer
void SetMaskingValue(const typename TMaskImage::PixelType &maskingValue)
const TMaskImage::PixelType & GetMaskingValue() const
TOutput operator()(const TInput &A, const TMask &B) const
bool operator!=(const MaskInput2 &) const
BinaryFunctorImageFilter< TInputImage, TMaskImage, TOutputImage, Functor::MaskInput2< typename TInputImage::PixelType, typename TMaskImage::PixelType, typename TOutputImage::PixelType > > Superclass
void SetOutsideValue(const TOutput &outsideValue)
void BeforeThreadedGenerateData() override
const TMask & GetMaskingValue() const