Medical Imaging Interaction Toolkit  2025.12.02
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
#define MITKIMAGESTATISTICS_EXPORT
bool operator==(const MaskInput2 &other) const
void SetMaskingValue(const TMask &maskingValue)
const TOutput & GetOutsideValue() const
NumericTraits< TInput >::AccumulateType AccumulatorType
const TMask & GetMaskingValue() const
bool operator!=(const MaskInput2 &) const
void SetOutsideValue(const TOutput &outsideValue)
TOutput operator()(const TInput &A, const TMask &B) const
const TOutputImage::PixelType & GetOutsideValue() const
void BeforeThreadedGenerateData() override
BinaryFunctorImageFilter< TInputImage, TMaskImage, TOutputImage, Functor::MaskInput2< typename TInputImage::PixelType, typename TMaskImage::PixelType, typename TOutputImage::PixelType > > Superclass
void SetMaskingValue(const typename TMaskImage::PixelType &maskingValue)
SmartPointer< const Self > ConstPointer
const TMaskImage::PixelType & GetMaskingValue() const
SmartPointer< Self > Pointer
void SetOutsideValue(const typename TOutputImage::PixelType &outsideValue)
const MaskImageType * GetMaskImage()
void PrintSelf(std::ostream &os, Indent indent) const override
void SetMaskImage(const MaskImageType *maskImage)