Medical Imaging Interaction Toolkit  2026.06.00
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 {
31 template< typename TInput, typename TMask, typename TOutput = TInput >
33 {
34 public:
35  typedef typename NumericTraits< TInput >::AccumulateType AccumulatorType;
36 
38  {
39  m_MaskingValue = NumericTraits< TMask >::ZeroValue();
40  InitializeOutsideValue( static_cast<TOutput*>( nullptr ) );
41  }
43  bool operator!=(const MaskInput2 &) const
44  {
45  return false;
46  }
47 
48  bool operator==(const MaskInput2 & other) const
49  {
50  return !( *this != other );
51  }
52 
53  inline TOutput operator()(const TInput & A, const TMask & B) const
54  {
55  if ( B == m_MaskingValue )
56  {
57  return static_cast< TOutput >( A );
58  }
59  else
60  {
61  return m_OutsideValue;
62  }
63  }
64 
66  void SetOutsideValue(const TOutput & outsideValue)
67  {
68  m_OutsideValue = outsideValue;
69  }
70 
72  const TOutput & GetOutsideValue() const
73  {
74  return m_OutsideValue;
75  }
76 
78  void SetMaskingValue(const TMask & maskingValue)
79  {
80  m_MaskingValue = maskingValue;
81  }
83  const TMask & GetMaskingValue() const
84  {
85  return m_MaskingValue;
86  }
87 
88 private:
89 
90  template < typename TPixelType >
91  void InitializeOutsideValue( TPixelType * )
92  {
93  this->m_OutsideValue = NumericTraits< TPixelType >::ZeroValue();
94  }
95 
96  template < typename TValue >
97  void InitializeOutsideValue( VariableLengthVector<TValue> * )
98  {
99  // set the outside value to be of zero length
100  this->m_OutsideValue = VariableLengthVector< TValue >(0);
101  }
102 
103  TOutput m_OutsideValue;
104  TMask m_MaskingValue;
105 };
106 }
132 template< typename TInputImage, typename TMaskImage, typename TOutputImage = TInputImage >
134  public
135  BinaryFunctorImageFilter< TInputImage, TMaskImage, TOutputImage,
136  Functor::MaskInput2<
137  typename TInputImage::PixelType,
138  typename TMaskImage::PixelType,
139  typename TOutputImage::PixelType > >
140 
141 {
142 public:
145  typedef BinaryFunctorImageFilter< TInputImage, TMaskImage, TOutputImage,
147  typename TInputImage::PixelType,
148  typename TMaskImage::PixelType,
149  typename TOutputImage::PixelType >
151 
154 
156  itkNewMacro(Self);
157 
159  itkTypeMacro(MaskImageFilter2,
160  BinaryFunctorImageFilter);
161 
163  typedef TMaskImage MaskImageType;
164 
169  void SetMaskImage(const MaskImageType *maskImage)
170  {
171  // Process object is not const-correct so the const casting is required.
172  this->SetNthInput( 1, const_cast< MaskImageType * >( maskImage ) );
173  }
175  {
176  return static_cast<const MaskImageType*>(this->ProcessObject::GetInput(1));
177  }
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  }
188 
189  const typename TOutputImage::PixelType & GetOutsideValue() const
190  {
191  return this->GetFunctor().GetOutsideValue();
192  }
193 
195  void SetMaskingValue(const typename TMaskImage::PixelType & maskingValue)
196  {
197  if ( this->GetMaskingValue() != maskingValue )
198  {
199  this->Modified();
200  this->GetFunctor().SetMaskingValue(maskingValue);
201  }
202  }
203 
205  const typename TMaskImage::PixelType & GetMaskingValue() const
206  {
207  return this->GetFunctor().GetMaskingValue();
208  }
209 
211  {
212  typedef typename TOutputImage::PixelType PixelType;
213  this->CheckOutsideValue( static_cast<PixelType*>(nullptr) );
214  }
215 
216 #ifdef ITK_USE_CONCEPT_CHECKING
217  // Begin concept checking
218  itkConceptMacro( MaskEqualityComparableCheck,
219  ( Concept::EqualityComparable< typename TMaskImage::PixelType > ) );
220  itkConceptMacro( InputConvertibleToOutputCheck,
221  ( Concept::Convertible< typename TInputImage::PixelType,
222  typename TOutputImage::PixelType > ) );
223  // End concept checking
224 #endif
225 
226 protected:
228  ~MaskImageFilter2() override {}
229 
230  void PrintSelf(std::ostream & os, Indent indent) const override
231  {
232  Superclass::PrintSelf(os, indent);
233  os << indent << "OutsideValue: " << this->GetOutsideValue() << std::endl;
234  }
235 
236 private:
237  MaskImageFilter2(const Self &); //purposely not implemented
238  void operator=(const Self &); //purposely not implemented
239 
240  template < typename TPixelType >
241  void CheckOutsideValue( const TPixelType * ) {}
242 
243  template < typename TValue >
244  void CheckOutsideValue( const VariableLengthVector< TValue > * )
245  {
246  // Check to see if the outside value contains only zeros. If so,
247  // resize it to have the same number of zeros as the output
248  // image. Otherwise, check that the number of components in the
249  // outside value is the same as the number of components in the
250  // output image. If not, throw an exception.
251  VariableLengthVector< TValue > currentValue =
252  this->GetFunctor().GetOutsideValue();
253  VariableLengthVector< TValue > zeroVector( currentValue.GetSize() );
254  zeroVector.Fill( NumericTraits< TValue >::ZeroValue() );
255 
256  if ( currentValue == zeroVector )
257  {
258  zeroVector.SetSize( this->GetOutput()->GetVectorLength() );
259  zeroVector.Fill( NumericTraits< TValue >::ZeroValue() );
260  this->GetFunctor().SetOutsideValue( zeroVector );
261  }
262  else if ( this->GetFunctor().GetOutsideValue().GetSize() !=
263  this->GetOutput()->GetVectorLength() )
264  {
265  itkExceptionMacro(
266  << "Number of components in OutsideValue: "
267  << this->GetFunctor().GetOutsideValue().GetSize()
268  << " is not the same as the "
269  << "number of components in the image: "
270  << this->GetOutput()->GetVectorLength());
271  }
272  }
273 
274 };
275 } // end namespace itk
276 
277 #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)