ITK  6.0.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 
60  return m_OutsideValue;
61  }
62 
64  void
65  SetOutsideValue(const TOutput & outsideValue)
66  {
67  m_OutsideValue = outsideValue;
68  }
69 
71  const TOutput &
73  {
74  return m_OutsideValue;
75  }
76 
78  void
79  SetMaskingValue(const TMask & maskingValue)
80  {
81  m_MaskingValue = maskingValue;
82  }
83 
85  const TMask &
87  {
88  return m_MaskingValue;
89  }
90 
91 private:
92  template <typename TPixelType>
93  TPixelType
94  DefaultOutsideValue(TPixelType *)
95  {
96  return TPixelType{};
97  }
98 
99  template <typename TValue>
102  {
103  // set the outside value to be of zero length
105  }
106  TOutput m_OutsideValue{ DefaultOutsideValue(static_cast<TOutput *>(nullptr)) };
107  TMask m_MaskingValue{};
108 };
109 } // namespace Functor
110 
145 template <typename TInputImage, typename TMaskImage, typename TOutputImage = TInputImage>
146 class ITK_TEMPLATE_EXPORT MaskImageFilter : public BinaryGeneratorImageFilter<TInputImage, TMaskImage, TOutputImage>
147 
148 {
149 public:
150  ITK_DISALLOW_COPY_AND_MOVE(MaskImageFilter);
151 
155 
156  using FunctorType = Functor::
157  MaskInput<typename TInputImage::PixelType, typename TMaskImage::PixelType, typename TOutputImage::PixelType>;
158 
161 
163  itkNewMacro(Self);
164 
166  itkOverrideGetNameOfClassMacro(MaskImageFilter);
167 
169  using MaskImageType = TMaskImage;
170 
175  void
176  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 *
183  {
184  return static_cast<const MaskImageType *>(this->ProcessObject::GetInput(1));
185  }
189  void
190  SetOutsideValue(const typename TOutputImage::PixelType & outsideValue)
191  {
192  if (Math::NotExactlyEquals(this->GetOutsideValue(), outsideValue))
193  {
194  this->Modified();
195  this->GetFunctor().SetOutsideValue(outsideValue);
196  }
197  }
200  const typename TOutputImage::PixelType &
202  {
203  return this->GetFunctor().GetOutsideValue();
204  }
205 
207  void
208  SetMaskingValue(const typename TMaskImage::PixelType & maskingValue)
209  {
210  if (this->GetMaskingValue() != maskingValue)
211  {
212  this->Modified();
213  this->GetFunctor().SetMaskingValue(maskingValue);
214  }
215  }
219  const typename TMaskImage::PixelType &
221  {
222  return this->GetFunctor().GetMaskingValue();
223  }
224 
225 #ifdef ITK_USE_CONCEPT_CHECKING
226  // Begin concept checking
228  itkConceptMacro(InputConvertibleToOutputCheck,
230  // End concept checking
231 #endif
232 
233 protected:
234  MaskImageFilter() = default;
235  ~MaskImageFilter() override = default;
236 
237  void
238  PrintSelf(std::ostream & os, Indent indent) const override
239  {
240  Superclass::PrintSelf(os, indent);
241  os << indent << "OutsideValue: " << this->GetOutsideValue() << std::endl;
242  }
243 
244  void
246  {
247  using PixelType = typename TOutputImage::PixelType;
248  this->CheckOutsideValue(static_cast<PixelType *>(nullptr));
249 
250  this->SetFunctor(this->GetFunctor());
251  }
252 
253 private:
254  itkGetConstReferenceMacro(Functor, FunctorType);
255  FunctorType &
257  {
258  return m_Functor;
259  }
260 
261  FunctorType m_Functor{};
262 
263  template <typename TPixelType>
264  void
265  CheckOutsideValue(const TPixelType *)
266  {}
267 
268  template <typename TValue>
269  void
271  {
272  // Check to see if the outside value contains only zeros. If so,
273  // resize it to have the same number of zeros as the output
274  // image. Otherwise, check that the number of components in the
275  // outside value is the same as the number of components in the
276  // output image. If not, throw an exception.
277  const VariableLengthVector<TValue> currentValue = this->GetFunctor().GetOutsideValue();
278  VariableLengthVector<TValue> zeroVector(currentValue.GetSize());
279  zeroVector.Fill(TValue{});
280 
281  if (currentValue == zeroVector)
282  {
283  zeroVector.SetSize(this->GetOutput()->GetVectorLength());
284  zeroVector.Fill(TValue{});
285  this->GetFunctor().SetOutsideValue(zeroVector);
286  }
287  else if (this->GetFunctor().GetOutsideValue().GetSize() != this->GetOutput()->GetVectorLength())
288  {
289  itkExceptionMacro("Number of components in OutsideValue: "
290  << this->GetFunctor().GetOutsideValue().GetSize() << " is not the same as the "
291  << "number of components in the image: " << this->GetOutput()->GetVectorLength());
292  }
293  }
294 };
295 } // end namespace itk
296 
297 #endif
itk::Functor::MaskInput::~MaskInput
~MaskInput()=default
itkVariableLengthVector.h
itk::MaskImageFilter::GetOutsideValue
const TOutputImage::PixelType & GetOutsideValue() const
Definition: itkMaskImageFilter.h:201
itk::Functor::MaskInput::GetMaskingValue
const TMask & GetMaskingValue() const
Definition: itkMaskImageFilter.h:86
itk::VariableLengthVector::GetSize
unsigned int GetSize() const
Definition: itkVariableLengthVector.h:592
itk::MaskImageFilter::BeforeThreadedGenerateData
void BeforeThreadedGenerateData() override
Definition: itkMaskImageFilter.h:245
itk::MaskImageFilter::MaskImageType
TMaskImage MaskImageType
Definition: itkMaskImageFilter.h:169
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:190
itk::MaskImageFilter::CheckOutsideValue
void CheckOutsideValue(const TPixelType *)
Definition: itkMaskImageFilter.h:265
itkBinaryGeneratorImageFilter.h
itk::MaskImageFilter::SetMaskingValue
void SetMaskingValue(const typename TMaskImage::PixelType &maskingValue)
Definition: itkMaskImageFilter.h:208
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:94
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:146
itk::Functor::MaskInput::m_OutsideValue
TOutput m_OutsideValue
Definition: itkMaskImageFilter.h:106
itk::Functor::MaskInput
Definition: itkMaskImageFilter.h:36
itk::MaskImageFilter::GetMaskingValue
const TMaskImage::PixelType & GetMaskingValue() const
Definition: itkMaskImageFilter.h:220
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:176
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:270
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:60
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: itkAnatomicalOrientation.h:29
itk::Functor::MaskInput::DefaultOutsideValue
VariableLengthVector< TValue > DefaultOutsideValue(VariableLengthVector< TValue > *)
Definition: itkMaskImageFilter.h:101
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:256
itk::Concept::Convertible
Definition: itkConceptChecking.h:217
itk::MaskImageFilter::PrintSelf
void PrintSelf(std::ostream &os, Indent indent) const override
Definition: itkMaskImageFilter.h:238
itk::Functor::MaskInput::SetOutsideValue
void SetOutsideValue(const TOutput &outsideValue)
Definition: itkMaskImageFilter.h:65
itk::Functor::MaskInput::SetMaskingValue
void SetMaskingValue(const TMask &maskingValue)
Definition: itkMaskImageFilter.h:79
itk::MaskImageFilter::GetMaskImage
const MaskImageType * GetMaskImage()
Definition: itkMaskImageFilter.h:182
itkNumericTraits.h
itk::VariableLengthVector::Fill
void Fill(const TValue &v)
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:107
itk::Functor::MaskInput::GetOutsideValue
const TOutput & GetOutsideValue() const
Definition: itkMaskImageFilter.h:72
itkMath.h
itk::ProcessObject::GetInput
DataObject * GetInput(const DataObjectIdentifierType &key)
Return an input.
itk::Concept::EqualityComparable
Definition: itkConceptChecking.h:307
itk::Functor::MaskInput::operator()
TOutput operator()(const TInput &A, const TMask &B) const
Definition: itkMaskImageFilter.h:53