Medical Imaging Interaction Toolkit  2023.12.99-1652ac8d
Medical Imaging Interaction Toolkit
mitkitkMaskImageFilter.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef mitkitkMaskImageFilter_h
14 #define mitkitkMaskImageFilter_h
15 
16 // This file is based on ITK's itkMaskImageFilter.h
17 
18 #include "itkBinaryFunctorImageFilter.h"
19 #include "itkNumericTraits.h"
20 #include "itkVariableLengthVector.h"
22 
23 namespace itk
24 {
25 namespace Functor
26 {
32 template< typename TInput, typename TMask, typename TOutput = TInput >
34 {
35 public:
36  typedef typename NumericTraits< TInput >::AccumulateType AccumulatorType;
37 
39  {
40  m_MaskingValue = NumericTraits< TMask >::ZeroValue();
41  InitializeOutsideValue( static_cast<TOutput*>( nullptr ) );
42  }
44  bool operator!=(const MaskInput2 &) const
45  {
46  return false;
47  }
48 
49  bool operator==(const MaskInput2 & other) const
50  {
51  return !( *this != other );
52  }
53 
54  inline TOutput operator()(const TInput & A, const TMask & B) const
55  {
56  if ( B == m_MaskingValue )
57  {
58  return static_cast< TOutput >( A );
59  }
60  else
61  {
62  return m_OutsideValue;
63  }
64  }
65 
67  void SetOutsideValue(const TOutput & outsideValue)
68  {
69  m_OutsideValue = outsideValue;
70  }
71 
73  const TOutput & GetOutsideValue() const
74  {
75  return m_OutsideValue;
76  }
77 
79  void SetMaskingValue(const TMask & maskingValue)
80  {
81  m_MaskingValue = maskingValue;
82  }
84  const TMask & GetMaskingValue() const
85  {
86  return m_MaskingValue;
87  }
88 
89 private:
90 
91  template < typename TPixelType >
92  void InitializeOutsideValue( TPixelType * )
93  {
94  this->m_OutsideValue = NumericTraits< TPixelType >::ZeroValue();
95  }
96 
97  template < typename TValue >
98  void InitializeOutsideValue( VariableLengthVector<TValue> * )
99  {
100  // set the outside value to be of zero length
101  this->m_OutsideValue = VariableLengthVector< TValue >(0);
102  }
103 
104  TOutput m_OutsideValue;
105  TMask m_MaskingValue;
106 };
107 }
136 template< typename TInputImage, typename TMaskImage, typename TOutputImage = TInputImage >
138  public
139  BinaryFunctorImageFilter< TInputImage, TMaskImage, TOutputImage,
140  Functor::MaskInput2<
141  typename TInputImage::PixelType,
142  typename TMaskImage::PixelType,
143  typename TOutputImage::PixelType > >
144 
145 {
146 public:
149  typedef BinaryFunctorImageFilter< TInputImage, TMaskImage, TOutputImage,
151  typename TInputImage::PixelType,
152  typename TMaskImage::PixelType,
153  typename TOutputImage::PixelType >
155 
158 
160  itkNewMacro(Self);
161 
163  itkTypeMacro(MaskImageFilter2,
164  BinaryFunctorImageFilter);
165 
167  typedef TMaskImage MaskImageType;
168 
173  void SetMaskImage(const MaskImageType *maskImage)
174  {
175  // Process object is not const-correct so the const casting is required.
176  this->SetNthInput( 1, const_cast< MaskImageType * >( maskImage ) );
177  }
179  {
180  return static_cast<const MaskImageType*>(this->ProcessObject::GetInput(1));
181  }
182 
184  void SetOutsideValue(const typename TOutputImage::PixelType & outsideValue)
185  {
186  if ( this->GetOutsideValue() != outsideValue )
187  {
188  this->Modified();
189  this->GetFunctor().SetOutsideValue(outsideValue);
190  }
191  }
192 
193  const typename TOutputImage::PixelType & GetOutsideValue() const
194  {
195  return this->GetFunctor().GetOutsideValue();
196  }
197 
199  void SetMaskingValue(const typename TMaskImage::PixelType & maskingValue)
200  {
201  if ( this->GetMaskingValue() != maskingValue )
202  {
203  this->Modified();
204  this->GetFunctor().SetMaskingValue(maskingValue);
205  }
206  }
207 
209  const typename TMaskImage::PixelType & GetMaskingValue() const
210  {
211  return this->GetFunctor().GetMaskingValue();
212  }
213 
215  {
216  typedef typename TOutputImage::PixelType PixelType;
217  this->CheckOutsideValue( static_cast<PixelType*>(nullptr) );
218  }
219 
220 #ifdef ITK_USE_CONCEPT_CHECKING
221  // Begin concept checking
222  itkConceptMacro( MaskEqualityComparableCheck,
223  ( Concept::EqualityComparable< typename TMaskImage::PixelType > ) );
224  itkConceptMacro( InputConvertibleToOutputCheck,
225  ( Concept::Convertible< typename TInputImage::PixelType,
226  typename TOutputImage::PixelType > ) );
227  // End concept checking
228 #endif
229 
230 protected:
232  ~MaskImageFilter2() override {}
233 
234  void PrintSelf(std::ostream & os, Indent indent) const override
235  {
236  Superclass::PrintSelf(os, indent);
237  os << indent << "OutsideValue: " << this->GetOutsideValue() << std::endl;
238  }
239 
240 private:
241  MaskImageFilter2(const Self &); //purposely not implemented
242  void operator=(const Self &); //purposely not implemented
243 
244  template < typename TPixelType >
245  void CheckOutsideValue( const TPixelType * ) {}
246 
247  template < typename TValue >
248  void CheckOutsideValue( const VariableLengthVector< TValue > * )
249  {
250  // Check to see if the outside value contains only zeros. If so,
251  // resize it to have the same number of zeros as the output
252  // image. Otherwise, check that the number of components in the
253  // outside value is the same as the number of components in the
254  // output image. If not, throw an exception.
255  VariableLengthVector< TValue > currentValue =
256  this->GetFunctor().GetOutsideValue();
257  VariableLengthVector< TValue > zeroVector( currentValue.GetSize() );
258  zeroVector.Fill( NumericTraits< TValue >::ZeroValue() );
259 
260  if ( currentValue == zeroVector )
261  {
262  zeroVector.SetSize( this->GetOutput()->GetVectorLength() );
263  zeroVector.Fill( NumericTraits< TValue >::ZeroValue() );
264  this->GetFunctor().SetOutsideValue( zeroVector );
265  }
266  else if ( this->GetFunctor().GetOutsideValue().GetSize() !=
267  this->GetOutput()->GetVectorLength() )
268  {
269  itkExceptionMacro(
270  << "Number of components in OutsideValue: "
271  << this->GetFunctor().GetOutsideValue().GetSize()
272  << " is not the same as the "
273  << "number of components in the image: "
274  << this->GetOutput()->GetVectorLength());
275  }
276  }
277 
278 };
279 } // end namespace itk
280 
281 #endif
itk::Functor::MaskInput2::AccumulatorType
NumericTraits< TInput >::AccumulateType AccumulatorType
Definition: mitkitkMaskImageFilter.h:36
itk::MaskImageFilter2::SetMaskingValue
void SetMaskingValue(const typename TMaskImage::PixelType &maskingValue)
Definition: mitkitkMaskImageFilter.h:199
itk::Functor::MaskInput2::operator()
TOutput operator()(const TInput &A, const TMask &B) const
Definition: mitkitkMaskImageFilter.h:54
itk::MaskImageFilter2::GetOutsideValue
const TOutputImage::PixelType & GetOutsideValue() const
Definition: mitkitkMaskImageFilter.h:193
itk::MaskImageFilter2::GetMaskImage
const MaskImageType * GetMaskImage()
Definition: mitkitkMaskImageFilter.h:178
itk::Functor::MaskInput2::GetMaskingValue
const TMask & GetMaskingValue() const
Definition: mitkitkMaskImageFilter.h:84
itk::MaskImageFilter2::~MaskImageFilter2
~MaskImageFilter2() override
Definition: mitkitkMaskImageFilter.h:232
itk::Functor::MaskInput2::operator!=
bool operator!=(const MaskInput2 &) const
Definition: mitkitkMaskImageFilter.h:44
itk::MaskImageFilter2::GetMaskingValue
const TMaskImage::PixelType & GetMaskingValue() const
Definition: mitkitkMaskImageFilter.h:209
itk::Functor::MaskInput2::~MaskInput2
~MaskInput2()
Definition: mitkitkMaskImageFilter.h:43
itk::MaskImageFilter2::SetOutsideValue
void SetOutsideValue(const typename TOutputImage::PixelType &outsideValue)
Definition: mitkitkMaskImageFilter.h:184
itk::SmartPointer< Self >
itk::Functor::MaskInput2::SetMaskingValue
void SetMaskingValue(const TMask &maskingValue)
Definition: mitkitkMaskImageFilter.h:79
itk::MaskImageFilter2::Self
MaskImageFilter2 Self
Definition: mitkitkMaskImageFilter.h:148
MITKIMAGESTATISTICS_EXPORT
#define MITKIMAGESTATISTICS_EXPORT
Definition: MitkImageStatisticsExports.h:15
itk::Functor::MaskInput2::MaskInput2
MaskInput2()
Definition: mitkitkMaskImageFilter.h:38
itk::MaskImageFilter2::BeforeThreadedGenerateData
void BeforeThreadedGenerateData() override
Definition: mitkitkMaskImageFilter.h:214
itk::MaskImageFilter2::Superclass
BinaryFunctorImageFilter< TInputImage, TMaskImage, TOutputImage, Functor::MaskInput2< typename TInputImage::PixelType, typename TMaskImage::PixelType, typename TOutputImage::PixelType > > Superclass
Definition: mitkitkMaskImageFilter.h:154
itk::VariableLengthVector
Definition: mitkPixelTypeTraits.h:34
itk::MaskImageFilter2::SetMaskImage
void SetMaskImage(const MaskImageType *maskImage)
Definition: mitkitkMaskImageFilter.h:173
itk::MaskImageFilter2::MaskImageType
TMaskImage MaskImageType
Definition: mitkitkMaskImageFilter.h:164
itk::Functor::MaskInput2
Definition: mitkitkMaskImageFilter.h:33
itk::Functor::MaskInput2::SetOutsideValue
void SetOutsideValue(const TOutput &outsideValue)
Definition: mitkitkMaskImageFilter.h:67
itk
SET FUNCTIONS.
Definition: itkIntelligentBinaryClosingFilter.h:30
itk::MaskImageFilter2::ConstPointer
SmartPointer< const Self > ConstPointer
Definition: mitkitkMaskImageFilter.h:157
itk::MaskImageFilter2
Definition: mitkitkMaskImageFilter.h:137
itk::MaskImageFilter2::PrintSelf
void PrintSelf(std::ostream &os, Indent indent) const override
Definition: mitkitkMaskImageFilter.h:234
itk::Functor::MaskInput2::operator==
bool operator==(const MaskInput2 &other) const
Definition: mitkitkMaskImageFilter.h:49
itk::Functor::MaskInput2::GetOutsideValue
const TOutput & GetOutsideValue() const
Definition: mitkitkMaskImageFilter.h:73
MitkImageStatisticsExports.h
itk::MaskImageFilter2::MaskImageFilter2
MaskImageFilter2()
Definition: mitkitkMaskImageFilter.h:231
itk::MaskImageFilter2::Pointer
SmartPointer< Self > Pointer
Definition: mitkitkMaskImageFilter.h:156