ITK  5.4.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  * https://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 
41  MaskInput() = default;
42 
43  ~MaskInput() = default;
44  bool
45  operator==(const MaskInput &) const
46  {
47  return true;
48  }
49 
51 
52  inline TOutput
53  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
67  SetOutsideValue(const TOutput & outsideValue)
68  {
69  m_OutsideValue = outsideValue;
70  }
71 
73  const TOutput &
75  {
76  return m_OutsideValue;
77  }
78 
80  void
81  SetMaskingValue(const TMask & maskingValue)
82  {
83  m_MaskingValue = maskingValue;
84  }
85 
87  const TMask &
89  {
90  return m_MaskingValue;
91  }
92 
93 private:
94  template <typename TPixelType>
95  TPixelType
96  DefaultOutsideValue(TPixelType *)
97  {
98  return TPixelType{};
99  }
100 
101  template <typename TValue>
104  {
105  // set the outside value to be of zero length
107  }
108  TOutput m_OutsideValue{ DefaultOutsideValue(static_cast<TOutput *>(nullptr)) };
109  TMask m_MaskingValue{};
110 };
111 } // namespace Functor
112 
147 template <typename TInputImage, typename TMaskImage, typename TOutputImage = TInputImage>
148 class ITK_TEMPLATE_EXPORT MaskImageFilter : public BinaryGeneratorImageFilter<TInputImage, TMaskImage, TOutputImage>
149 
150 {
151 public:
152  ITK_DISALLOW_COPY_AND_MOVE(MaskImageFilter);
153 
157 
158  using FunctorType = Functor::
159  MaskInput<typename TInputImage::PixelType, typename TMaskImage::PixelType, typename TOutputImage::PixelType>;
160 
163 
165  itkNewMacro(Self);
166 
168  itkOverrideGetNameOfClassMacro(MaskImageFilter);
169 
171  using MaskImageType = TMaskImage;
172 
177  void
178  SetMaskImage(const MaskImageType * maskImage)
179  {
180  // Process object is not const-correct so the const casting is required.
181  this->SetNthInput(1, const_cast<MaskImageType *>(maskImage));
182  }
183  const MaskImageType *
185  {
186  return static_cast<const MaskImageType *>(this->ProcessObject::GetInput(1));
187  }
191  void
192  SetOutsideValue(const typename TOutputImage::PixelType & outsideValue)
193  {
194  if (Math::NotExactlyEquals(this->GetOutsideValue(), outsideValue))
195  {
196  this->Modified();
197  this->GetFunctor().SetOutsideValue(outsideValue);
198  }
199  }
202  const typename TOutputImage::PixelType &
204  {
205  return this->GetFunctor().GetOutsideValue();
206  }
207 
209  void
210  SetMaskingValue(const typename TMaskImage::PixelType & maskingValue)
211  {
212  if (this->GetMaskingValue() != maskingValue)
213  {
214  this->Modified();
215  this->GetFunctor().SetMaskingValue(maskingValue);
216  }
217  }
221  const typename TMaskImage::PixelType &
223  {
224  return this->GetFunctor().GetMaskingValue();
225  }
226 
227 #ifdef ITK_USE_CONCEPT_CHECKING
228  // Begin concept checking
230  itkConceptMacro(InputConvertibleToOutputCheck,
232  // End concept checking
233 #endif
234 
235 protected:
236  MaskImageFilter() = default;
237  ~MaskImageFilter() override = default;
238 
239  void
240  PrintSelf(std::ostream & os, Indent indent) const override
241  {
242  Superclass::PrintSelf(os, indent);
243  os << indent << "OutsideValue: " << this->GetOutsideValue() << std::endl;
244  }
245 
246  void
248  {
249  using PixelType = typename TOutputImage::PixelType;
250  this->CheckOutsideValue(static_cast<PixelType *>(nullptr));
251 
252  this->SetFunctor(this->GetFunctor());
253  }
254 
255 private:
256  itkGetConstReferenceMacro(Functor, FunctorType);
257  FunctorType &
259  {
260  return m_Functor;
261  }
262 
263  FunctorType m_Functor{};
264 
265  template <typename TPixelType>
266  void
267  CheckOutsideValue(const TPixelType *)
268  {}
269 
270  template <typename TValue>
271  void
273  {
274  // Check to see if the outside value contains only zeros. If so,
275  // resize it to have the same number of zeros as the output
276  // image. Otherwise, check that the number of components in the
277  // outside value is the same as the number of components in the
278  // output image. If not, throw an exception.
279  VariableLengthVector<TValue> currentValue = this->GetFunctor().GetOutsideValue();
280  VariableLengthVector<TValue> zeroVector(currentValue.GetSize());
281  zeroVector.Fill(TValue{});
282 
283  if (currentValue == zeroVector)
284  {
285  zeroVector.SetSize(this->GetOutput()->GetVectorLength());
286  zeroVector.Fill(TValue{});
287  this->GetFunctor().SetOutsideValue(zeroVector);
288  }
289  else if (this->GetFunctor().GetOutsideValue().GetSize() != this->GetOutput()->GetVectorLength())
290  {
291  itkExceptionMacro("Number of components in OutsideValue: "
292  << this->GetFunctor().GetOutsideValue().GetSize() << " is not the same as the "
293  << "number of components in the image: " << this->GetOutput()->GetVectorLength());
294  }
295  }
296 };
297 } // end namespace itk
298 
299 #endif
itk::VariableLengthVector::Fill
void Fill(TValue const &v)
itk::Functor::MaskInput::~MaskInput
~MaskInput()=default
itkVariableLengthVector.h
itk::MaskImageFilter::GetOutsideValue
const TOutputImage::PixelType & GetOutsideValue() const
Definition: itkMaskImageFilter.h:203
itk::Functor::MaskInput::GetMaskingValue
const TMask & GetMaskingValue() const
Definition: itkMaskImageFilter.h:88
itk::VariableLengthVector::GetSize
unsigned int GetSize() const
Definition: itkVariableLengthVector.h:595
itk::MaskImageFilter::BeforeThreadedGenerateData
void BeforeThreadedGenerateData() override
Definition: itkMaskImageFilter.h:247
itk::MaskImageFilter::MaskImageType
TMaskImage MaskImageType
Definition: itkMaskImageFilter.h:171
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:736
itk::MaskImageFilter::SetOutsideValue
void SetOutsideValue(const typename TOutputImage::PixelType &outsideValue)
Definition: itkMaskImageFilter.h:192
itk::MaskImageFilter::CheckOutsideValue
void CheckOutsideValue(const TPixelType *)
Definition: itkMaskImageFilter.h:267
itkBinaryGeneratorImageFilter.h
itk::MaskImageFilter::SetMaskingValue
void SetMaskingValue(const typename TMaskImage::PixelType &maskingValue)
Definition: itkMaskImageFilter.h:210
itk::ImageSource
Base class for all process objects that output image data.
Definition: itkImageSource.h:67
itk::Functor::MaskInput::DefaultOutsideValue
TPixelType DefaultOutsideValue(TPixelType *)
Definition: itkMaskImageFilter.h:96
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:148
itk::Functor::MaskInput::m_OutsideValue
TOutput m_OutsideValue
Definition: itkMaskImageFilter.h:108
itk::Functor::MaskInput
Definition: itkMaskImageFilter.h:36
itk::MaskImageFilter::GetMaskingValue
const TMaskImage::PixelType & GetMaskingValue() const
Definition: itkMaskImageFilter.h:222
itk::Functor::MaskInput::operator==
bool operator==(const MaskInput &) const
Definition: itkMaskImageFilter.h:45
FunctorType
Functor::Add2< typename TInputImage1::PixelType, typename TInputImage2::PixelType, typename TOutputImage::PixelType > FunctorType
Definition: itkAddImageFilter.h:97
itk::MaskImageFilter::SetMaskImage
void SetMaskImage(const MaskImageType *maskImage)
Definition: itkMaskImageFilter.h:178
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:272
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:59
itk::Functor::MaskInput::MaskInput
MaskInput()=default
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::Functor::MaskInput::DefaultOutsideValue
VariableLengthVector< TValue > DefaultOutsideValue(VariableLengthVector< TValue > *)
Definition: itkMaskImageFilter.h:103
itk::ProcessObject
The base class for all process objects (source, filters, mappers) in the Insight data processing pipe...
Definition: itkProcessObject.h:139
itk::MaskImageFilter::GetFunctor
FunctorType & GetFunctor()
Definition: itkMaskImageFilter.h:258
itk::Concept::Convertible
Definition: itkConceptChecking.h:216
itk::MaskImageFilter::PrintSelf
void PrintSelf(std::ostream &os, Indent indent) const override
Definition: itkMaskImageFilter.h:240
itk::Functor::MaskInput::SetOutsideValue
void SetOutsideValue(const TOutput &outsideValue)
Definition: itkMaskImageFilter.h:67
itk::Functor::MaskInput::SetMaskingValue
void SetMaskingValue(const TMask &maskingValue)
Definition: itkMaskImageFilter.h:81
itk::MaskImageFilter::GetMaskImage
const MaskImageType * GetMaskImage()
Definition: itkMaskImageFilter.h:184
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:109
itk::Functor::MaskInput::GetOutsideValue
const TOutput & GetOutsideValue() const
Definition: itkMaskImageFilter.h:74
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:53