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 
57  return static_cast<TOutput>(A);
58  }
59 
61  void
62  SetOutsideValue(const TOutput & outsideValue)
63  {
64  m_OutsideValue = outsideValue;
65  }
66 
68  const TOutput &
70  {
71  return m_OutsideValue;
72  }
73 
75  void
76  SetMaskingValue(const TMask & maskingValue)
77  {
78  m_MaskingValue = maskingValue;
79  }
80 
82  const TMask &
84  {
85  return m_MaskingValue;
86  }
87 
88 private:
89  template <typename TPixelType>
90  TPixelType
91  DefaultOutsideValue(TPixelType *)
92  {
93  return TPixelType{};
94  }
95 
96  template <typename TValue>
99  {
100  // set the outside value to be of zero length
102  }
103  TOutput m_OutsideValue{ DefaultOutsideValue(static_cast<TOutput *>(nullptr)) };
104  TMask m_MaskingValue{};
105 };
106 } // namespace Functor
107 
142 template <typename TInputImage, typename TMaskImage, typename TOutputImage = TInputImage>
143 class ITK_TEMPLATE_EXPORT MaskNegatedImageFilter
144  : public BinaryGeneratorImageFilter<TInputImage, TMaskImage, TOutputImage>
145 {
146 public:
147  ITK_DISALLOW_COPY_AND_MOVE(MaskNegatedImageFilter);
148 
152 
153  using FunctorType = Functor::
154  MaskNegatedInput<typename TInputImage::PixelType, typename TMaskImage::PixelType, typename TOutputImage::PixelType>;
157 
159  itkNewMacro(Self);
160 
162  itkOverrideGetNameOfClassMacro(MaskNegatedImageFilter);
163 
165  using MaskImageType = TMaskImage;
166 
168  void
169  SetOutsideValue(const typename TOutputImage::PixelType & outsideValue)
170  {
171  if (Math::NotExactlyEquals(this->GetOutsideValue(), outsideValue))
172  {
173  this->Modified();
174  this->GetFunctor().SetOutsideValue(outsideValue);
175  }
176  }
179  const typename TOutputImage::PixelType &
181  {
182  return this->GetFunctor().GetOutsideValue();
183  }
184 
186  void
187  SetMaskingValue(const typename TMaskImage::PixelType & maskingValue)
188  {
189  if (this->GetMaskingValue() != maskingValue)
190  {
191  this->GetFunctor().SetMaskingValue(maskingValue);
192  this->Modified();
193  }
194  }
198  const typename TMaskImage::PixelType &
200  {
201  return this->GetFunctor().GetMaskingValue();
202  }
203 
208  void
209  SetMaskImage(const MaskImageType * maskImage)
210  {
211  // Process object is not const-correct so the const casting is required.
212  this->SetNthInput(1, const_cast<MaskImageType *>(maskImage));
213  }
214 
215  const MaskImageType *
217  {
218  return static_cast<const MaskImageType *>(this->ProcessObject::GetInput(1));
219  }
220 
221 #ifdef ITK_USE_CONCEPT_CHECKING
222  // Begin concept checking
224  itkConceptMacro(InputConvertibleToOutputCheck,
226  // End concept checking
227 #endif
228 
229 protected:
230  MaskNegatedImageFilter() = default;
231  ~MaskNegatedImageFilter() override = default;
232 
233  void
234  PrintSelf(std::ostream & os, Indent indent) const override
235  {
236  Superclass::PrintSelf(os, indent);
237  os << indent << "OutsideValue: " << this->GetOutsideValue() << std::endl;
238  }
239 
240  void
242  {
243  using PixelType = typename TOutputImage::PixelType;
244  this->CheckOutsideValue(static_cast<PixelType *>(nullptr));
245 
246 
247  this->SetFunctor(this->GetFunctor());
248  }
249 
250 private:
251  itkGetConstReferenceMacro(Functor, FunctorType);
252  FunctorType &
254  {
255  return m_Functor;
256  }
257 
258  FunctorType m_Functor{};
259 
260  template <typename TPixelType>
261  void
262  CheckOutsideValue(const TPixelType *)
263  {}
264 
265  template <typename TValue>
266  void
268  {
269  // Check to see if the outside value contains only zeros. If so,
270  // resize it to have the same number of zeros as the output
271  // image. Otherwise, check that the number of components in the
272  // outside value is the same as the number of components in the
273  // output image. If not, throw an exception.
274  const VariableLengthVector<TValue> currentValue = this->GetFunctor().GetOutsideValue();
275  VariableLengthVector<TValue> zeroVector(currentValue.GetSize());
276  zeroVector.Fill(TValue{});
277 
278  if (currentValue == zeroVector)
279  {
280  zeroVector.SetSize(this->GetOutput()->GetVectorLength());
281  zeroVector.Fill(TValue{});
282  this->GetFunctor().SetOutsideValue(zeroVector);
283  }
284  else if (this->GetFunctor().GetOutsideValue().GetSize() != this->GetOutput()->GetVectorLength())
285  {
286  itkExceptionMacro("Number of components in OutsideValue: "
287  << this->GetFunctor().GetOutsideValue().GetSize() << " is not the same as the "
288  << "number of components in the image: " << this->GetOutput()->GetVectorLength());
289  }
290  }
291 };
292 } // end namespace itk
293 
294 #endif
itk::Functor::MaskNegatedInput::m_MaskingValue
TMask m_MaskingValue
Definition: itkMaskNegatedImageFilter.h:104
itk::MaskNegatedImageFilter::PrintSelf
void PrintSelf(std::ostream &os, Indent indent) const override
Definition: itkMaskNegatedImageFilter.h:234
itkVariableLengthVector.h
itk::MaskNegatedImageFilter::CheckOutsideValue
void CheckOutsideValue(const VariableLengthVector< TValue > *)
Definition: itkMaskNegatedImageFilter.h:267
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:199
itk::MaskNegatedImageFilter::GetOutsideValue
const TOutputImage::PixelType & GetOutsideValue() const
Definition: itkMaskNegatedImageFilter.h:180
itk::Functor::MaskNegatedInput::DefaultOutsideValue
TPixelType DefaultOutsideValue(TPixelType *)
Definition: itkMaskNegatedImageFilter.h:91
itk::Functor::MaskNegatedInput
Definition: itkMaskNegatedImageFilter.h:36
itk::MaskNegatedImageFilter::GetMaskImage
const MaskImageType * GetMaskImage()
Definition: itkMaskNegatedImageFilter.h:216
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:62
itk::Functor::MaskNegatedInput::GetMaskingValue
const TMask & GetMaskingValue() const
Definition: itkMaskNegatedImageFilter.h:83
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:98
itk::MaskNegatedImageFilter
Mask an image with the negation (or logical compliment) of a mask.
Definition: itkMaskNegatedImageFilter.h:143
itk::MaskNegatedImageFilter::BeforeThreadedGenerateData
void BeforeThreadedGenerateData() override
Definition: itkMaskNegatedImageFilter.h:241
itk::MaskNegatedImageFilter::SetOutsideValue
void SetOutsideValue(const typename TOutputImage::PixelType &outsideValue)
Definition: itkMaskNegatedImageFilter.h:169
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:69
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:217
itkNumericTraits.h
itk::VariableLengthVector::Fill
void Fill(const TValue &v)
itk::MaskNegatedImageFilter::MaskImageType
TMaskImage MaskImageType
Definition: itkMaskNegatedImageFilter.h:165
itk::MaskNegatedImageFilter::GetFunctor
FunctorType & GetFunctor()
Definition: itkMaskNegatedImageFilter.h:253
itk::Functor::MaskNegatedInput::SetMaskingValue
void SetMaskingValue(const TMask &maskingValue)
Definition: itkMaskNegatedImageFilter.h:76
itk::MaskNegatedImageFilter::CheckOutsideValue
void CheckOutsideValue(const TPixelType *)
Definition: itkMaskNegatedImageFilter.h:262
itk::Functor::MaskNegatedInput::m_OutsideValue
TOutput m_OutsideValue
Definition: itkMaskNegatedImageFilter.h:103
itkMath.h
itk::ProcessObject::GetInput
DataObject * GetInput(const DataObjectIdentifierType &key)
Return an input.
itk::Concept::EqualityComparable
Definition: itkConceptChecking.h:307
itk::MaskNegatedImageFilter::SetMaskImage
void SetMaskImage(const MaskImageType *maskImage)
Definition: itkMaskNegatedImageFilter.h:209
itk::MaskNegatedImageFilter::SetMaskingValue
void SetMaskingValue(const typename TMaskImage::PixelType &maskingValue)
Definition: itkMaskNegatedImageFilter.h:187