ITK  6.0.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  bool
43  {
44  return true;
45  }
46 
48 
49  inline TOutput
50  operator()(const TInput & A, const TMask & B) const
51  {
52  if (B != m_MaskingValue)
53  {
54  return m_OutsideValue;
55  }
56  else
57  {
58  return static_cast<TOutput>(A);
59  }
60  }
61 
63  void
64  SetOutsideValue(const TOutput & outsideValue)
65  {
66  m_OutsideValue = outsideValue;
67  }
68 
70  const TOutput &
72  {
73  return m_OutsideValue;
74  }
75 
77  void
78  SetMaskingValue(const TMask & maskingValue)
79  {
80  m_MaskingValue = maskingValue;
81  }
82 
84  const TMask &
86  {
87  return m_MaskingValue;
88  }
89 
90 private:
91  template <typename TPixelType>
92  TPixelType
93  DefaultOutsideValue(TPixelType *)
94  {
95  return TPixelType{};
96  }
97 
98  template <typename TValue>
101  {
102  // set the outside value to be of zero length
104  }
105  TOutput m_OutsideValue{ DefaultOutsideValue(static_cast<TOutput *>(nullptr)) };
106  TMask m_MaskingValue{};
107 };
108 } // namespace Functor
109 
144 template <typename TInputImage, typename TMaskImage, typename TOutputImage = TInputImage>
145 class ITK_TEMPLATE_EXPORT MaskNegatedImageFilter
146  : public BinaryGeneratorImageFilter<TInputImage, TMaskImage, TOutputImage>
147 {
148 public:
149  ITK_DISALLOW_COPY_AND_MOVE(MaskNegatedImageFilter);
150 
154 
155  using FunctorType = Functor::
156  MaskNegatedInput<typename TInputImage::PixelType, typename TMaskImage::PixelType, typename TOutputImage::PixelType>;
159 
161  itkNewMacro(Self);
162 
164  itkOverrideGetNameOfClassMacro(MaskNegatedImageFilter);
165 
167  using MaskImageType = TMaskImage;
168 
170  void
171  SetOutsideValue(const typename TOutputImage::PixelType & outsideValue)
172  {
173  if (Math::NotExactlyEquals(this->GetOutsideValue(), outsideValue))
174  {
175  this->Modified();
176  this->GetFunctor().SetOutsideValue(outsideValue);
177  }
178  }
181  const typename TOutputImage::PixelType &
183  {
184  return this->GetFunctor().GetOutsideValue();
185  }
186 
188  void
189  SetMaskingValue(const typename TMaskImage::PixelType & maskingValue)
190  {
191  if (this->GetMaskingValue() != maskingValue)
192  {
193  this->GetFunctor().SetMaskingValue(maskingValue);
194  this->Modified();
195  }
196  }
200  const typename TMaskImage::PixelType &
202  {
203  return this->GetFunctor().GetMaskingValue();
204  }
205 
210  void
211  SetMaskImage(const MaskImageType * maskImage)
212  {
213  // Process object is not const-correct so the const casting is required.
214  this->SetNthInput(1, const_cast<MaskImageType *>(maskImage));
215  }
216 
217  const MaskImageType *
219  {
220  return static_cast<const MaskImageType *>(this->ProcessObject::GetInput(1));
221  }
222 
223 #ifdef ITK_USE_CONCEPT_CHECKING
224  // Begin concept checking
226  itkConceptMacro(InputConvertibleToOutputCheck,
228  // End concept checking
229 #endif
230 
231 protected:
232  MaskNegatedImageFilter() = default;
233  ~MaskNegatedImageFilter() override = default;
234 
235  void
236  PrintSelf(std::ostream & os, Indent indent) const override
237  {
238  Superclass::PrintSelf(os, indent);
239  os << indent << "OutsideValue: " << this->GetOutsideValue() << std::endl;
240  }
241 
242  void
244  {
245  using PixelType = typename TOutputImage::PixelType;
246  this->CheckOutsideValue(static_cast<PixelType *>(nullptr));
247 
248 
249  this->SetFunctor(this->GetFunctor());
250  }
251 
252 private:
253  itkGetConstReferenceMacro(Functor, FunctorType);
254  FunctorType &
256  {
257  return m_Functor;
258  }
259 
260  FunctorType m_Functor{};
261 
262  template <typename TPixelType>
263  void
264  CheckOutsideValue(const TPixelType *)
265  {}
266 
267  template <typename TValue>
268  void
270  {
271  // Check to see if the outside value contains only zeros. If so,
272  // resize it to have the same number of zeros as the output
273  // image. Otherwise, check that the number of components in the
274  // outside value is the same as the number of components in the
275  // output image. If not, throw an exception.
276  VariableLengthVector<TValue> currentValue = this->GetFunctor().GetOutsideValue();
277  VariableLengthVector<TValue> zeroVector(currentValue.GetSize());
278  zeroVector.Fill(TValue{});
279 
280  if (currentValue == zeroVector)
281  {
282  zeroVector.SetSize(this->GetOutput()->GetVectorLength());
283  zeroVector.Fill(TValue{});
284  this->GetFunctor().SetOutsideValue(zeroVector);
285  }
286  else if (this->GetFunctor().GetOutsideValue().GetSize() != this->GetOutput()->GetVectorLength())
287  {
288  itkExceptionMacro("Number of components in OutsideValue: "
289  << this->GetFunctor().GetOutsideValue().GetSize() << " is not the same as the "
290  << "number of components in the image: " << this->GetOutput()->GetVectorLength());
291  }
292  }
293 };
294 } // end namespace itk
295 
296 #endif
itk::VariableLengthVector::Fill
void Fill(TValue const &v)
itk::Functor::MaskNegatedInput::m_MaskingValue
TMask m_MaskingValue
Definition: itkMaskNegatedImageFilter.h:106
itk::MaskNegatedImageFilter::PrintSelf
void PrintSelf(std::ostream &os, Indent indent) const override
Definition: itkMaskNegatedImageFilter.h:236
itkVariableLengthVector.h
itk::MaskNegatedImageFilter::CheckOutsideValue
void CheckOutsideValue(const VariableLengthVector< TValue > *)
Definition: itkMaskNegatedImageFilter.h:269
itk::VariableLengthVector::GetSize
unsigned int GetSize() const
Definition: itkVariableLengthVector.h:592
itk::Functor::MaskNegatedInput::operator()
TOutput operator()(const TInput &A, const TMask &B) const
Definition: itkMaskNegatedImageFilter.h:50
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::MaskNegatedImageFilter::GetMaskingValue
const TMaskImage::PixelType & GetMaskingValue() const
Definition: itkMaskNegatedImageFilter.h:201
itk::MaskNegatedImageFilter::GetOutsideValue
const TOutputImage::PixelType & GetOutsideValue() const
Definition: itkMaskNegatedImageFilter.h:182
itk::Functor::MaskNegatedInput::DefaultOutsideValue
TPixelType DefaultOutsideValue(TPixelType *)
Definition: itkMaskNegatedImageFilter.h:93
itk::Functor::MaskNegatedInput
Definition: itkMaskNegatedImageFilter.h:36
itk::MaskNegatedImageFilter::GetMaskImage
const MaskImageType * GetMaskImage()
Definition: itkMaskNegatedImageFilter.h:218
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:64
itk::Functor::MaskNegatedInput::GetMaskingValue
const TMask & GetMaskingValue() const
Definition: itkMaskNegatedImageFilter.h:85
itk::Functor::MaskNegatedInput::operator==
bool operator==(const MaskNegatedInput &) const
Definition: itkMaskNegatedImageFilter.h:42
itk::Functor::MaskNegatedInput::DefaultOutsideValue
VariableLengthVector< TValue > DefaultOutsideValue(VariableLengthVector< TValue > *)
Definition: itkMaskNegatedImageFilter.h:100
itk::MaskNegatedImageFilter
Mask an image with the negation (or logical compliment) of a mask.
Definition: itkMaskNegatedImageFilter.h:145
itk::MaskNegatedImageFilter::BeforeThreadedGenerateData
void BeforeThreadedGenerateData() override
Definition: itkMaskNegatedImageFilter.h:243
itk::MaskNegatedImageFilter::SetOutsideValue
void SetOutsideValue(const typename TOutputImage::PixelType &outsideValue)
Definition: itkMaskNegatedImageFilter.h:171
FunctorType
Functor::Add2< typename TInputImage1::PixelType, typename TInputImage2::PixelType, typename TOutputImage::PixelType > FunctorType
Definition: itkAddImageFilter.h:97
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:60
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::Functor::MaskNegatedInput::GetOutsideValue
const TOutput & GetOutsideValue() const
Definition: itkMaskNegatedImageFilter.h:71
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::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:167
itk::MaskNegatedImageFilter::GetFunctor
FunctorType & GetFunctor()
Definition: itkMaskNegatedImageFilter.h:255
itk::Functor::MaskNegatedInput::SetMaskingValue
void SetMaskingValue(const TMask &maskingValue)
Definition: itkMaskNegatedImageFilter.h:78
itk::MaskNegatedImageFilter::CheckOutsideValue
void CheckOutsideValue(const TPixelType *)
Definition: itkMaskNegatedImageFilter.h:264
itk::Functor::MaskNegatedInput::m_OutsideValue
TOutput m_OutsideValue
Definition: itkMaskNegatedImageFilter.h:105
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:211
itk::MaskNegatedImageFilter::SetMaskingValue
void SetMaskingValue(const typename TMaskImage::PixelType &maskingValue)
Definition: itkMaskNegatedImageFilter.h:189