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
26namespace itk
27{
28namespace Functor
29{
35template <typename TInput, typename TMask, typename TOutput = TInput>
37{
38public:
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
90private:
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)) };
107};
108} // namespace Functor
109
144template <typename TInputImage, typename TMaskImage, typename TOutputImage = TInputImage>
145class ITK_TEMPLATE_EXPORT MaskNegatedImageFilter
146 : public BinaryGeneratorImageFilter<TInputImage, TMaskImage, TOutputImage>
147{
148public:
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
231protected:
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
252private:
253 itkGetConstReferenceMacro(Functor, 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
Implements pixel-wise generic operation of two images, or of an image and a constant.
TPixelType DefaultOutsideValue(TPixelType *)
typename NumericTraits< TInput >::AccumulateType AccumulatorType
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(MaskNegatedInput)
VariableLengthVector< TValue > DefaultOutsideValue(VariableLengthVector< TValue > *)
void SetMaskingValue(const TMask &maskingValue)
void SetOutsideValue(const TOutput &outsideValue)
TOutput operator()(const TInput &A, const TMask &B) const
bool operator==(const MaskNegatedInput &) const
Control indentation during Print() invocation.
Definition: itkIndent.h:50
Light weight base class for most itk classes.
Mask an image with the negation (or logical compliment) of a mask.
void SetOutsideValue(const typename TOutputImage::PixelType &outsideValue)
void SetMaskImage(const MaskImageType *maskImage)
void CheckOutsideValue(const TPixelType *)
const TMaskImage::PixelType & GetMaskingValue() const
void CheckOutsideValue(const VariableLengthVector< TValue > *)
void SetMaskingValue(const typename TMaskImage::PixelType &maskingValue)
~MaskNegatedImageFilter() override=default
void PrintSelf(std::ostream &os, Indent indent) const override
const TOutputImage::PixelType & GetOutsideValue() const
const MaskImageType * GetMaskImage()
Define additional traits for native types such as int or float.
DataObject * GetInput(const DataObjectIdentifierType &key)
Return an input.
#define itkConceptMacro(name, concept)
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition: itkMath.h:733
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....