ITK  5.3.0
Insight Toolkit
itkMaskImageFilter.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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 itkMaskImageFilter_h
19 #define itkMaskImageFilter_h
20 
22 #include "itkNumericTraits.h"
24 #include "itkMath.h"
25 
26 namespace itk
27 {
28 namespace Functor
29 {
35 template <typename TInput, typename TMask, typename TOutput = TInput>
36 class MaskInput
37 {
38 public:
40 
42  {
44  InitializeOutsideValue(static_cast<TOutput *>(nullptr));
45  }
46  ~MaskInput() = default;
47  bool
48  operator==(const MaskInput &) const
49  {
50  return true;
51  }
52 
54 
55  inline TOutput
56  operator()(const TInput & A, const TMask & B) const
57  {
58  if (B != m_MaskingValue)
59  {
60  return static_cast<TOutput>(A);
61  }
62  else
63  {
64  return m_OutsideValue;
65  }
66  }
67 
69  void
70  SetOutsideValue(const TOutput & outsideValue)
71  {
72  m_OutsideValue = outsideValue;
73  }
74 
76  const TOutput &
78  {
79  return m_OutsideValue;
80  }
81 
83  void
84  SetMaskingValue(const TMask & maskingValue)
85  {
86  m_MaskingValue = maskingValue;
87  }
88 
90  const TMask &
92  {
93  return m_MaskingValue;
94  }
95 
96 private:
97  template <typename TPixelType>
98  void
99  InitializeOutsideValue(TPixelType *)
100  {
102  }
103 
104  template <typename TValue>
105  void
107  {
108  // set the outside value to be of zero length
110  }
111 
112  TOutput m_OutsideValue;
114 };
115 } // namespace Functor
116 
151 template <typename TInputImage, typename TMaskImage, typename TOutputImage = TInputImage>
152 class ITK_TEMPLATE_EXPORT MaskImageFilter : public BinaryGeneratorImageFilter<TInputImage, TMaskImage, TOutputImage>
153 
154 {
155 public:
156  ITK_DISALLOW_COPY_AND_MOVE(MaskImageFilter);
157 
161 
162  using FunctorType = Functor::
163  MaskInput<typename TInputImage::PixelType, typename TMaskImage::PixelType, typename TOutputImage::PixelType>;
164 
167 
169  itkNewMacro(Self);
170 
173 
175  using MaskImageType = TMaskImage;
176 
181  void
182  SetMaskImage(const MaskImageType * maskImage)
183  {
184  // Process object is not const-correct so the const casting is required.
185  this->SetNthInput(1, const_cast<MaskImageType *>(maskImage));
186  }
187  const MaskImageType *
189  {
190  return static_cast<const MaskImageType *>(this->ProcessObject::GetInput(1));
191  }
193 
195  void
196  SetOutsideValue(const typename TOutputImage::PixelType & outsideValue)
197  {
198  if (Math::NotExactlyEquals(this->GetOutsideValue(), outsideValue))
199  {
200  this->Modified();
201  this->GetFunctor().SetOutsideValue(outsideValue);
202  }
203  }
205 
206  const typename TOutputImage::PixelType &
208  {
209  return this->GetFunctor().GetOutsideValue();
210  }
211 
213  void
214  SetMaskingValue(const typename TMaskImage::PixelType & maskingValue)
215  {
216  if (this->GetMaskingValue() != maskingValue)
217  {
218  this->Modified();
219  this->GetFunctor().SetMaskingValue(maskingValue);
220  }
221  }
223 
225  const typename TMaskImage::PixelType &
227  {
228  return this->GetFunctor().GetMaskingValue();
229  }
230 
231 #ifdef ITK_USE_CONCEPT_CHECKING
232  // Begin concept checking
234  itkConceptMacro(InputConvertibleToOutputCheck,
236  // End concept checking
237 #endif
238 
239 protected:
240  MaskImageFilter() = default;
241  ~MaskImageFilter() override = default;
242 
243  void
244  PrintSelf(std::ostream & os, Indent indent) const override
245  {
246  Superclass::PrintSelf(os, indent);
247  os << indent << "OutsideValue: " << this->GetOutsideValue() << std::endl;
248  }
249 
250  void
252  {
253  using PixelType = typename TOutputImage::PixelType;
254  this->CheckOutsideValue(static_cast<PixelType *>(nullptr));
255 
256  this->SetFunctor(this->GetFunctor());
257  }
258 
259 private:
260  itkGetConstReferenceMacro(Functor, FunctorType);
261  FunctorType &
263  {
264  return m_Functor;
265  }
266 
268 
269  template <typename TPixelType>
270  void
271  CheckOutsideValue(const TPixelType *)
272  {}
273 
274  template <typename TValue>
275  void
277  {
278  // Check to see if the outside value contains only zeros. If so,
279  // resize it to have the same number of zeros as the output
280  // image. Otherwise, check that the number of components in the
281  // outside value is the same as the number of components in the
282  // output image. If not, throw an exception.
283  VariableLengthVector<TValue> currentValue = this->GetFunctor().GetOutsideValue();
284  VariableLengthVector<TValue> zeroVector(currentValue.GetSize());
286 
287  if (currentValue == zeroVector)
288  {
289  zeroVector.SetSize(this->GetOutput()->GetVectorLength());
290  zeroVector.Fill(NumericTraits<TValue>::ZeroValue());
291  this->GetFunctor().SetOutsideValue(zeroVector);
292  }
293  else if (this->GetFunctor().GetOutsideValue().GetSize() != this->GetOutput()->GetVectorLength())
294  {
295  itkExceptionMacro(<< "Number of components in OutsideValue: " << this->GetFunctor().GetOutsideValue().GetSize()
296  << " is not the same as the "
297  << "number of components in the image: " << this->GetOutput()->GetVectorLength());
298  }
299  }
300 };
301 } // end namespace itk
302 
303 #endif
itk::VariableLengthVector::Fill
void Fill(TValue const &v)
itk::Functor::MaskInput::InitializeOutsideValue
void InitializeOutsideValue(VariableLengthVector< TValue > *)
Definition: itkMaskImageFilter.h:106
itk::Functor::MaskInput::~MaskInput
~MaskInput()=default
itkVariableLengthVector.h
itk::MaskImageFilter::GetOutsideValue
const TOutputImage::PixelType & GetOutsideValue() const
Definition: itkMaskImageFilter.h:207
itk::Functor::MaskInput::GetMaskingValue
const TMask & GetMaskingValue() const
Definition: itkMaskImageFilter.h:91
itk::VariableLengthVector::GetSize
unsigned int GetSize() const
Definition: itkVariableLengthVector.h:595
itk::MaskImageFilter::BeforeThreadedGenerateData
void BeforeThreadedGenerateData() override
Definition: itkMaskImageFilter.h:251
itk::MaskImageFilter::MaskImageType
TMaskImage MaskImageType
Definition: itkMaskImageFilter.h:175
itk::BinaryGeneratorImageFilter
Implements pixel-wise generic operation of two images, or of an image and a constant.
Definition: itkBinaryGeneratorImageFilter.h:56
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::Math::NotExactlyEquals
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition: itkMath.h:733
itk::MaskImageFilter::SetOutsideValue
void SetOutsideValue(const typename TOutputImage::PixelType &outsideValue)
Definition: itkMaskImageFilter.h:196
itk::MaskImageFilter::CheckOutsideValue
void CheckOutsideValue(const TPixelType *)
Definition: itkMaskImageFilter.h:271
itkBinaryGeneratorImageFilter.h
itk::MaskImageFilter::SetMaskingValue
void SetMaskingValue(const typename TMaskImage::PixelType &maskingValue)
Definition: itkMaskImageFilter.h:214
itk::ImageSource
Base class for all process objects that output image data.
Definition: itkImageSource.h:67
itk::Functor::MaskInput::ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(MaskInput)
itk::MaskImageFilter
Mask an image with a mask.
Definition: itkMaskImageFilter.h:152
itk::Functor::MaskInput::InitializeOutsideValue
void InitializeOutsideValue(TPixelType *)
Definition: itkMaskImageFilter.h:99
itk::Functor::MaskInput::m_OutsideValue
TOutput m_OutsideValue
Definition: itkMaskImageFilter.h:112
itk::MaskImageFilter::m_Functor
FunctorType m_Functor
Definition: itkMaskImageFilter.h:267
itk::Functor::MaskInput::MaskInput
MaskInput()
Definition: itkMaskImageFilter.h:41
itk::Functor::MaskInput
Definition: itkMaskImageFilter.h:36
itk::MaskImageFilter::GetMaskingValue
const TMaskImage::PixelType & GetMaskingValue() const
Definition: itkMaskImageFilter.h:226
itk::Functor::MaskInput::operator==
bool operator==(const MaskInput &) const
Definition: itkMaskImageFilter.h:48
itk::MaskImageFilter::SetMaskImage
void SetMaskImage(const MaskImageType *maskImage)
Definition: itkMaskImageFilter.h:182
itk::VariableLengthVector
Represents an array whose length can be defined at run-time.
Definition: itkConstantBoundaryCondition.h:28
itk::MaskImageFilter::CheckOutsideValue
void CheckOutsideValue(const VariableLengthVector< TValue > *)
Definition: itkMaskImageFilter.h:276
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:58
itk::NumericTraits::ZeroValue
static T ZeroValue()
Definition: itkNumericTraits.h:148
itkConceptMacro
#define itkConceptMacro(name, concept)
Definition: itkConceptChecking.h:65
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::ProcessObject
The base class for all process objects (source, filters, mappers) in the Insight data processing pipe...
Definition: itkProcessObject.h:138
itk::MaskImageFilter::GetFunctor
FunctorType & GetFunctor()
Definition: itkMaskImageFilter.h:262
itk::Concept::Convertible
Definition: itkConceptChecking.h:216
itk::MaskImageFilter::PrintSelf
void PrintSelf(std::ostream &os, Indent indent) const override
Definition: itkMaskImageFilter.h:244
itk::Functor::MaskInput::SetOutsideValue
void SetOutsideValue(const TOutput &outsideValue)
Definition: itkMaskImageFilter.h:70
itk::Functor::MaskInput::SetMaskingValue
void SetMaskingValue(const TMask &maskingValue)
Definition: itkMaskImageFilter.h:84
itk::MaskImageFilter::GetMaskImage
const MaskImageType * GetMaskImage()
Definition: itkMaskImageFilter.h:188
itkNumericTraits.h
itk::Functor::MaskInput< typename TInputImage::PixelType, typename TMaskImage::PixelType, typename TOutputImage::PixelType >::AccumulatorType
typename NumericTraits< typename TInputImage::PixelType >::AccumulateType AccumulatorType
Definition: itkMaskImageFilter.h:39
itk::Functor::MaskInput::m_MaskingValue
TMask m_MaskingValue
Definition: itkMaskImageFilter.h:113
itk::Functor::MaskInput::GetOutsideValue
const TOutput & GetOutsideValue() const
Definition: itkMaskImageFilter.h:77
itkMath.h
itk::ProcessObject::GetInput
DataObject * GetInput(const DataObjectIdentifierType &key)
Return an input.
itk::Concept::EqualityComparable
Definition: itkConceptChecking.h:306
itk::Functor::MaskInput::operator()
TOutput operator()(const TInput &A, const TMask &B) const
Definition: itkMaskImageFilter.h:56