Medical Imaging Interaction Toolkit  2022.04.99-b878213f
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 }
139 template< typename TInputImage, typename TMaskImage, typename TOutputImage = TInputImage >
141  public
142  BinaryFunctorImageFilter< TInputImage, TMaskImage, TOutputImage,
143  Functor::MaskInput2<
144  typename TInputImage::PixelType,
145  typename TMaskImage::PixelType,
146  typename TOutputImage::PixelType > >
147 
148 {
149 public:
152  typedef BinaryFunctorImageFilter< TInputImage, TMaskImage, TOutputImage,
154  typename TInputImage::PixelType,
155  typename TMaskImage::PixelType,
156  typename TOutputImage::PixelType >
158 
161 
163  itkNewMacro(Self);
164 
166  itkTypeMacro(MaskImageFilter2,
167  BinaryFunctorImageFilter);
168 
170  typedef TMaskImage MaskImageType;
171 
176  void SetMaskImage(const MaskImageType *maskImage)
177  {
178  // Process object is not const-correct so the const casting is required.
179  this->SetNthInput( 1, const_cast< MaskImageType * >( maskImage ) );
180  }
181  const MaskImageType * GetMaskImage()
182  {
183  return static_cast<const MaskImageType*>(this->ProcessObject::GetInput(1));
184  }
185 
187  void SetOutsideValue(const typename TOutputImage::PixelType & outsideValue)
188  {
189  if ( this->GetOutsideValue() != outsideValue )
190  {
191  this->Modified();
192  this->GetFunctor().SetOutsideValue(outsideValue);
193  }
194  }
195 
196  const typename TOutputImage::PixelType & GetOutsideValue() const
197  {
198  return this->GetFunctor().GetOutsideValue();
199  }
200 
202  void SetMaskingValue(const typename TMaskImage::PixelType & maskingValue)
203  {
204  if ( this->GetMaskingValue() != maskingValue )
205  {
206  this->Modified();
207  this->GetFunctor().SetMaskingValue(maskingValue);
208  }
209  }
210 
212  const typename TMaskImage::PixelType & GetMaskingValue() const
213  {
214  return this->GetFunctor().GetMaskingValue();
215  }
216 
218  {
219  typedef typename TOutputImage::PixelType PixelType;
220  this->CheckOutsideValue( static_cast<PixelType*>(nullptr) );
221  }
222 
223 #ifdef ITK_USE_CONCEPT_CHECKING
224  // Begin concept checking
225  itkConceptMacro( MaskEqualityComparableCheck,
226  ( Concept::EqualityComparable< typename TMaskImage::PixelType > ) );
227  itkConceptMacro( InputConvertibleToOutputCheck,
228  ( Concept::Convertible< typename TInputImage::PixelType,
229  typename TOutputImage::PixelType > ) );
230  // End concept checking
231 #endif
232 
233 protected:
235  ~MaskImageFilter2() override {}
236 
237  void PrintSelf(std::ostream & os, Indent indent) const override
238  {
239  Superclass::PrintSelf(os, indent);
240  os << indent << "OutsideValue: " << this->GetOutsideValue() << std::endl;
241  }
242 
243 private:
244  MaskImageFilter2(const Self &); //purposely not implemented
245  void operator=(const Self &); //purposely not implemented
246 
247  template < typename TPixelType >
248  void CheckOutsideValue( const TPixelType * ) {}
249 
250  template < typename TValue >
251  void CheckOutsideValue( const VariableLengthVector< TValue > * )
252  {
253  // Check to see if the outside value contains only zeros. If so,
254  // resize it to have the same number of zeros as the output
255  // image. Otherwise, check that the number of components in the
256  // outside value is the same as the number of components in the
257  // output image. If not, throw an exception.
258  VariableLengthVector< TValue > currentValue =
259  this->GetFunctor().GetOutsideValue();
260  VariableLengthVector< TValue > zeroVector( currentValue.GetSize() );
261  zeroVector.Fill( NumericTraits< TValue >::ZeroValue() );
262 
263  if ( currentValue == zeroVector )
264  {
265  zeroVector.SetSize( this->GetOutput()->GetVectorLength() );
266  zeroVector.Fill( NumericTraits< TValue >::ZeroValue() );
267  this->GetFunctor().SetOutsideValue( zeroVector );
268  }
269  else if ( this->GetFunctor().GetOutsideValue().GetSize() !=
270  this->GetOutput()->GetVectorLength() )
271  {
272  itkExceptionMacro(
273  << "Number of components in OutsideValue: "
274  << this->GetFunctor().GetOutsideValue().GetSize()
275  << " is not the same as the "
276  << "number of components in the image: "
277  << this->GetOutput()->GetVectorLength());
278  }
279  }
280 
281 };
282 } // end namespace itk
283 
284 #endif
285 
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