ITK  5.4.0
Insight Toolkit
itkMaskNegatedImageFilter.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 itkMaskNegatedImageFilter_h
19 #define itkMaskNegatedImageFilter_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>
37 {
38 public:
40 
41  MaskNegatedInput() = default;
42  ~MaskNegatedInput() = default;
43  bool
45  {
46  return true;
47  }
48 
50 
51  inline TOutput
52  operator()(const TInput & A, const TMask & B) const
53  {
54  if (B != m_MaskingValue)
55  {
56  return m_OutsideValue;
57  }
58  else
59  {
60  return static_cast<TOutput>(A);
61  }
62  }
63 
65  void
66  SetOutsideValue(const TOutput & outsideValue)
67  {
68  m_OutsideValue = outsideValue;
69  }
70 
72  const TOutput &
74  {
75  return m_OutsideValue;
76  }
77 
79  void
80  SetMaskingValue(const TMask & maskingValue)
81  {
82  m_MaskingValue = maskingValue;
83  }
84 
86  const TMask &
88  {
89  return m_MaskingValue;
90  }
91 
92 private:
93  template <typename TPixelType>
94  TPixelType
95  DefaultOutsideValue(TPixelType *)
96  {
98  }
99 
100  template <typename TValue>
103  {
104  // set the outside value to be of zero length
106  }
107  TOutput m_OutsideValue{ DefaultOutsideValue(static_cast<TOutput *>(nullptr)) };
109 };
110 } // namespace Functor
111 
146 template <typename TInputImage, typename TMaskImage, typename TOutputImage = TInputImage>
147 class ITK_TEMPLATE_EXPORT MaskNegatedImageFilter
148  : public BinaryGeneratorImageFilter<TInputImage, TMaskImage, TOutputImage>
149 {
150 public:
151  ITK_DISALLOW_COPY_AND_MOVE(MaskNegatedImageFilter);
152 
156 
157  using FunctorType = Functor::
158  MaskNegatedInput<typename TInputImage::PixelType, typename TMaskImage::PixelType, typename TOutputImage::PixelType>;
161 
163  itkNewMacro(Self);
164 
167 
169  using MaskImageType = TMaskImage;
170 
172  void
173  SetOutsideValue(const typename TOutputImage::PixelType & outsideValue)
174  {
175  if (Math::NotExactlyEquals(this->GetOutsideValue(), outsideValue))
176  {
177  this->Modified();
178  this->GetFunctor().SetOutsideValue(outsideValue);
179  }
180  }
183  const typename TOutputImage::PixelType &
185  {
186  return this->GetFunctor().GetOutsideValue();
187  }
188 
190  void
191  SetMaskingValue(const typename TMaskImage::PixelType & maskingValue)
192  {
193  if (this->GetMaskingValue() != maskingValue)
194  {
195  this->GetFunctor().SetMaskingValue(maskingValue);
196  this->Modified();
197  }
198  }
202  const typename TMaskImage::PixelType &
204  {
205  return this->GetFunctor().GetMaskingValue();
206  }
207 
212  void
213  SetMaskImage(const MaskImageType * maskImage)
214  {
215  // Process object is not const-correct so the const casting is required.
216  this->SetNthInput(1, const_cast<MaskImageType *>(maskImage));
217  }
218 
219  const MaskImageType *
221  {
222  return static_cast<const MaskImageType *>(this->ProcessObject::GetInput(1));
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  MaskNegatedImageFilter() = default;
235  ~MaskNegatedImageFilter() 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 
251  this->SetFunctor(this->GetFunctor());
252  }
253 
254 private:
255  itkGetConstReferenceMacro(Functor, FunctorType);
256  FunctorType &
258  {
259  return m_Functor;
260  }
261 
262  FunctorType m_Functor{};
263 
264  template <typename TPixelType>
265  void
266  CheckOutsideValue(const TPixelType *)
267  {}
268 
269  template <typename TValue>
270  void
272  {
273  // Check to see if the outside value contains only zeros. If so,
274  // resize it to have the same number of zeros as the output
275  // image. Otherwise, check that the number of components in the
276  // outside value is the same as the number of components in the
277  // output image. If not, throw an exception.
278  VariableLengthVector<TValue> currentValue = this->GetFunctor().GetOutsideValue();
279  VariableLengthVector<TValue> zeroVector(currentValue.GetSize());
281 
282  if (currentValue == zeroVector)
283  {
284  zeroVector.SetSize(this->GetOutput()->GetVectorLength());
285  zeroVector.Fill(NumericTraits<TValue>::ZeroValue());
286  this->GetFunctor().SetOutsideValue(zeroVector);
287  }
288  else if (this->GetFunctor().GetOutsideValue().GetSize() != this->GetOutput()->GetVectorLength())
289  {
290  itkExceptionMacro(<< "Number of components in OutsideValue: " << this->GetFunctor().GetOutsideValue().GetSize()
291  << " is not the same as the "
292  << "number of components in the image: " << this->GetOutput()->GetVectorLength());
293  }
294  }
295 };
296 } // end namespace itk
297 
298 #endif
itk::VariableLengthVector::Fill
void Fill(TValue const &v)
itk::Functor::MaskNegatedInput::~MaskNegatedInput
~MaskNegatedInput()=default
itk::Functor::MaskNegatedInput::m_MaskingValue
TMask m_MaskingValue
Definition: itkMaskNegatedImageFilter.h:108
itk::MaskNegatedImageFilter::PrintSelf
void PrintSelf(std::ostream &os, Indent indent) const override
Definition: itkMaskNegatedImageFilter.h:238
itkVariableLengthVector.h
itk::MaskNegatedImageFilter::CheckOutsideValue
void CheckOutsideValue(const VariableLengthVector< TValue > *)
Definition: itkMaskNegatedImageFilter.h:271
itk::VariableLengthVector::GetSize
unsigned int GetSize() const
Definition: itkVariableLengthVector.h:595
itk::Functor::MaskNegatedInput::operator()
TOutput operator()(const TInput &A, const TMask &B) const
Definition: itkMaskNegatedImageFilter.h:52
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:738
itk::MaskNegatedImageFilter::GetMaskingValue
const TMaskImage::PixelType & GetMaskingValue() const
Definition: itkMaskNegatedImageFilter.h:203
itk::MaskNegatedImageFilter::GetOutsideValue
const TOutputImage::PixelType & GetOutsideValue() const
Definition: itkMaskNegatedImageFilter.h:184
itk::Functor::MaskNegatedInput::DefaultOutsideValue
TPixelType DefaultOutsideValue(TPixelType *)
Definition: itkMaskNegatedImageFilter.h:95
itk::Functor::MaskNegatedInput
Definition: itkMaskNegatedImageFilter.h:36
itk::MaskNegatedImageFilter::GetMaskImage
const MaskImageType * GetMaskImage()
Definition: itkMaskNegatedImageFilter.h:220
itkBinaryGeneratorImageFilter.h
itk::ImageSource
Base class for all process objects that output image data.
Definition: itkImageSource.h:67
itk::Functor::MaskNegatedInput::SetOutsideValue
void SetOutsideValue(const TOutput &outsideValue)
Definition: itkMaskNegatedImageFilter.h:66
itk::Functor::MaskNegatedInput::GetMaskingValue
const TMask & GetMaskingValue() const
Definition: itkMaskNegatedImageFilter.h:87
itk::Functor::MaskNegatedInput::operator==
bool operator==(const MaskNegatedInput &) const
Definition: itkMaskNegatedImageFilter.h:44
itk::Functor::MaskNegatedInput::DefaultOutsideValue
VariableLengthVector< TValue > DefaultOutsideValue(VariableLengthVector< TValue > *)
Definition: itkMaskNegatedImageFilter.h:102
itk::Functor::MaskNegatedInput::MaskNegatedInput
MaskNegatedInput()=default
itk::MaskNegatedImageFilter
Mask an image with the negation (or logical compliment) of a mask.
Definition: itkMaskNegatedImageFilter.h:147
itk::MaskNegatedImageFilter::BeforeThreadedGenerateData
void BeforeThreadedGenerateData() override
Definition: itkMaskNegatedImageFilter.h:245
itk::MaskNegatedImageFilter::SetOutsideValue
void SetOutsideValue(const typename TOutputImage::PixelType &outsideValue)
Definition: itkMaskNegatedImageFilter.h:173
FunctorType
Functor::Add2< typename TInputImage1::PixelType, typename TInputImage2::PixelType, typename TOutputImage::PixelType > FunctorType
Definition: itkAddImageFilter.h:96
itk::Functor::MaskNegatedInput::ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(MaskNegatedInput)
itk::VariableLengthVector
Represents an array whose length can be defined at run-time.
Definition: itkConstantBoundaryCondition.h:28
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:59
itk::Functor::MaskNegatedInput< typename TInputImage::PixelType, typename TMaskImage::PixelType, typename TOutputImage::PixelType >::AccumulatorType
typename NumericTraits< typename TInputImage::PixelType >::AccumulateType AccumulatorType
Definition: itkMaskNegatedImageFilter.h:39
itk::NumericTraits::ZeroValue
static T ZeroValue()
Definition: itkNumericTraits.h:149
itk::Functor::MaskNegatedInput::GetOutsideValue
const TOutput & GetOutsideValue() const
Definition: itkMaskNegatedImageFilter.h:73
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:139
itk::Concept::Convertible
Definition: itkConceptChecking.h:216
itkNumericTraits.h
itk::MaskNegatedImageFilter::MaskImageType
TMaskImage MaskImageType
Definition: itkMaskNegatedImageFilter.h:169
itk::MaskNegatedImageFilter::GetFunctor
FunctorType & GetFunctor()
Definition: itkMaskNegatedImageFilter.h:257
itk::Functor::MaskNegatedInput::SetMaskingValue
void SetMaskingValue(const TMask &maskingValue)
Definition: itkMaskNegatedImageFilter.h:80
itk::MaskNegatedImageFilter::CheckOutsideValue
void CheckOutsideValue(const TPixelType *)
Definition: itkMaskNegatedImageFilter.h:266
itk::Functor::MaskNegatedInput::m_OutsideValue
TOutput m_OutsideValue
Definition: itkMaskNegatedImageFilter.h:107
itkMath.h
itk::ProcessObject::GetInput
DataObject * GetInput(const DataObjectIdentifierType &key)
Return an input.
itk::Concept::EqualityComparable
Definition: itkConceptChecking.h:306
itk::MaskNegatedImageFilter::SetMaskImage
void SetMaskImage(const MaskImageType *maskImage)
Definition: itkMaskNegatedImageFilter.h:213
itk::MaskNegatedImageFilter::SetMaskingValue
void SetMaskingValue(const typename TMaskImage::PixelType &maskingValue)
Definition: itkMaskNegatedImageFilter.h:191