ITK  5.2.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 false;
51  }
52 
53  bool
54  operator==(const MaskInput & other) const
55  {
56  return !(*this != other);
57  }
58 
59  inline TOutput
60  operator()(const TInput & A, const TMask & B) const
61  {
62  if (B != m_MaskingValue)
63  {
64  return static_cast<TOutput>(A);
65  }
66  else
67  {
68  return m_OutsideValue;
69  }
70  }
71 
73  void
74  SetOutsideValue(const TOutput & outsideValue)
75  {
76  m_OutsideValue = outsideValue;
77  }
78 
80  const TOutput &
82  {
83  return m_OutsideValue;
84  }
85 
87  void
88  SetMaskingValue(const TMask & maskingValue)
89  {
90  m_MaskingValue = maskingValue;
91  }
92 
94  const TMask &
96  {
97  return m_MaskingValue;
98  }
99 
100 private:
101  template <typename TPixelType>
102  void
104  {
106  }
107 
108  template <typename TValue>
109  void
111  {
112  // set the outside value to be of zero length
114  }
115 
116  TOutput m_OutsideValue;
118 };
119 } // namespace Functor
120 
155 template <typename TInputImage, typename TMaskImage, typename TOutputImage = TInputImage>
156 class MaskImageFilter : public BinaryGeneratorImageFilter<TInputImage, TMaskImage, TOutputImage>
157 
158 {
159 public:
160  ITK_DISALLOW_COPY_AND_MOVE(MaskImageFilter);
161 
165 
166  using FunctorType = Functor::
167  MaskInput<typename TInputImage::PixelType, typename TMaskImage::PixelType, typename TOutputImage::PixelType>;
168 
171 
173  itkNewMacro(Self);
174 
177 
179  using MaskImageType = TMaskImage;
180 
185  void
186  SetMaskImage(const MaskImageType * maskImage)
187  {
188  // Process object is not const-correct so the const casting is required.
189  this->SetNthInput(1, const_cast<MaskImageType *>(maskImage));
190  }
191  const MaskImageType *
193  {
194  return static_cast<const MaskImageType *>(this->ProcessObject::GetInput(1));
195  }
197 
199  void
200  SetOutsideValue(const typename TOutputImage::PixelType & outsideValue)
201  {
202  if (Math::NotExactlyEquals(this->GetOutsideValue(), outsideValue))
203  {
204  this->Modified();
205  this->GetFunctor().SetOutsideValue(outsideValue);
206  }
207  }
209 
210  const typename TOutputImage::PixelType &
212  {
213  return this->GetFunctor().GetOutsideValue();
214  }
215 
217  void
218  SetMaskingValue(const typename TMaskImage::PixelType & maskingValue)
219  {
220  if (this->GetMaskingValue() != maskingValue)
221  {
222  this->Modified();
223  this->GetFunctor().SetMaskingValue(maskingValue);
224  }
225  }
227 
229  const typename TMaskImage::PixelType &
231  {
232  return this->GetFunctor().GetMaskingValue();
233  }
234 
235 #ifdef ITK_USE_CONCEPT_CHECKING
236  // Begin concept checking
238  itkConceptMacro(InputConvertibleToOutputCheck,
240  // End concept checking
241 #endif
242 
243 protected:
244  MaskImageFilter() = default;
245  ~MaskImageFilter() override = default;
246 
247  void
248  PrintSelf(std::ostream & os, Indent indent) const override
249  {
250  Superclass::PrintSelf(os, indent);
251  os << indent << "OutsideValue: " << this->GetOutsideValue() << std::endl;
252  }
253 
254  void
256  {
257  using PixelType = typename TOutputImage::PixelType;
258  this->CheckOutsideValue(static_cast<PixelType *>(nullptr));
259 
260  this->SetFunctor(this->GetFunctor());
261  }
262 
263 private:
264  itkGetConstReferenceMacro(Functor, FunctorType);
265  FunctorType &
267  {
268  return m_Functor;
269  }
270 
272 
273  template <typename TPixelType>
274  void
275  CheckOutsideValue(const TPixelType *)
276  {}
277 
278  template <typename TValue>
279  void
281  {
282  // Check to see if the outside value contains only zeros. If so,
283  // resize it to have the same number of zeros as the output
284  // image. Otherwise, check that the number of components in the
285  // outside value is the same as the number of components in the
286  // output image. If not, throw an exception.
288  VariableLengthVector<TValue> zeroVector(currentValue.GetSize());
290 
291  if (currentValue == zeroVector)
292  {
293  zeroVector.SetSize(this->GetOutput()->GetVectorLength());
294  zeroVector.Fill(NumericTraits<TValue>::ZeroValue());
295  this->GetFunctor().SetOutsideValue(zeroVector);
296  }
297  else if (this->GetFunctor().GetOutsideValue().GetSize() != this->GetOutput()->GetVectorLength())
298  {
299  itkExceptionMacro(<< "Number of components in OutsideValue: " << this->GetFunctor().GetOutsideValue().GetSize()
300  << " is not the same as the "
301  << "number of components in the image: " << this->GetOutput()->GetVectorLength());
302  }
303  }
304 };
305 } // end namespace itk
306 
307 #endif
itk::VariableLengthVector::Fill
void Fill(TValue const &v)
itk::Functor::MaskInput::InitializeOutsideValue
void InitializeOutsideValue(VariableLengthVector< TValue > *)
Definition: itkMaskImageFilter.h:110
itk::Functor::MaskInput::~MaskInput
~MaskInput()=default
itkVariableLengthVector.h
itk::MaskImageFilter::GetOutsideValue
const TOutputImage::PixelType & GetOutsideValue() const
Definition: itkMaskImageFilter.h:211
itk::Functor::MaskInput::GetMaskingValue
const TMask & GetMaskingValue() const
Definition: itkMaskImageFilter.h:95
itk::VariableLengthVector::GetSize
unsigned int GetSize() const
Definition: itkVariableLengthVector.h:597
itk::MaskImageFilter::BeforeThreadedGenerateData
void BeforeThreadedGenerateData() override
Definition: itkMaskImageFilter.h:255
itk::MaskImageFilter::MaskImageType
TMaskImage MaskImageType
Definition: itkMaskImageFilter.h:179
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::GetFunctor
virtual const FunctorType & GetFunctor() const
itk::MaskImageFilter::SetOutsideValue
void SetOutsideValue(const typename TOutputImage::PixelType &outsideValue)
Definition: itkMaskImageFilter.h:200
itk::MaskImageFilter::CheckOutsideValue
void CheckOutsideValue(const TPixelType *)
Definition: itkMaskImageFilter.h:275
itk::MaskImageFilter::MaskImageFilter
MaskImageFilter()=default
itkBinaryGeneratorImageFilter.h
itk::MaskImageFilter::SetMaskingValue
void SetMaskingValue(const typename TMaskImage::PixelType &maskingValue)
Definition: itkMaskImageFilter.h:218
itk::ImageSource
Base class for all process objects that output image data.
Definition: itkImageSource.h:67
itk::MaskImageFilter
Mask an image with a mask.
Definition: itkMaskImageFilter.h:156
itk::Functor::MaskInput::InitializeOutsideValue
void InitializeOutsideValue(TPixelType *)
Definition: itkMaskImageFilter.h:103
itk::InPlaceImageFilter::PrintSelf
void PrintSelf(std::ostream &os, Indent indent) const override
itk::Functor::MaskInput::m_OutsideValue
TOutput m_OutsideValue
Definition: itkMaskImageFilter.h:116
itk::MaskImageFilter::m_Functor
FunctorType m_Functor
Definition: itkMaskImageFilter.h:271
itk::Functor::MaskInput::MaskInput
MaskInput()
Definition: itkMaskImageFilter.h:41
itk::Functor::MaskInput::operator!=
bool operator!=(const MaskInput &) const
Definition: itkMaskImageFilter.h:48
itk::Functor::MaskInput
Definition: itkMaskImageFilter.h:36
itk::MaskImageFilter::GetMaskingValue
const TMaskImage::PixelType & GetMaskingValue() const
Definition: itkMaskImageFilter.h:230
itk::MaskImageFilter::~MaskImageFilter
~MaskImageFilter() override=default
itk::MaskImageFilter::SetMaskImage
void SetMaskImage(const MaskImageType *maskImage)
Definition: itkMaskImageFilter.h:186
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:280
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::Functor::MaskInput::operator==
bool operator==(const MaskInput &other) const
Definition: itkMaskImageFilter.h:54
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::ProcessObject::SetNthInput
virtual void SetNthInput(DataObjectPointerArraySizeType idx, DataObject *input)
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:266
itk::Concept::Convertible
Definition: itkConceptChecking.h:216
itk::MaskImageFilter::PrintSelf
void PrintSelf(std::ostream &os, Indent indent) const override
Definition: itkMaskImageFilter.h:248
itk::Functor::MaskInput::SetOutsideValue
void SetOutsideValue(const TOutput &outsideValue)
Definition: itkMaskImageFilter.h:74
itk::Functor::MaskInput::SetMaskingValue
void SetMaskingValue(const TMask &maskingValue)
Definition: itkMaskImageFilter.h:88
itk::MaskImageFilter::GetMaskImage
const MaskImageType * GetMaskImage()
Definition: itkMaskImageFilter.h:192
itkNumericTraits.h
itk::ImageSource::GetOutput
OutputImageType * GetOutput()
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:117
itk::Functor::MaskInput::GetOutsideValue
const TOutput & GetOutsideValue() const
Definition: itkMaskImageFilter.h:81
itk::Object::Modified
virtual void Modified() const
itkMath.h
itk::ProcessObject::GetInput
DataObject * GetInput(const DataObjectIdentifierType &key)
Return an input.
itk::Concept::EqualityComparable
Definition: itkConceptChecking.h:306
itk::BinaryGeneratorImageFilter< TInputImage, TMaskImage, TOutputImage >::SetFunctor
void SetFunctor(const std::function< ConstRefFunctionType > &f)
Definition: itkBinaryGeneratorImageFilter.h:150
itk::MaskImageFilter::FunctorType
Functor::MaskInput< typename TInputImage::PixelType, typename TMaskImage::PixelType, typename TOutputImage::PixelType > FunctorType
Definition: itkMaskImageFilter.h:167
itk::Functor::MaskInput::operator()
TOutput operator()(const TInput &A, const TMask &B) const
Definition: itkMaskImageFilter.h:60