ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
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
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
88private:
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)) };
105};
106} // namespace Functor
107
142template <typename TInputImage, typename TMaskImage, typename TOutputImage = TInputImage>
143class ITK_TEMPLATE_EXPORT MaskNegatedImageFilter
144 : public BinaryGeneratorImageFilter<TInputImage, TMaskImage, TOutputImage>
145{
146public:
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 }
177
178
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 }
195
196
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
222 itkConceptMacro(InputConvertibleToOutputCheck,
224
225protected:
227 ~MaskNegatedImageFilter() override = default;
228
229 void
230 PrintSelf(std::ostream & os, Indent indent) const override
231 {
232 Superclass::PrintSelf(os, indent);
233 os << indent << "OutsideValue: " << this->GetOutsideValue() << std::endl;
234 }
235
236 void
238 {
239 using PixelType = typename TOutputImage::PixelType;
240 this->CheckOutsideValue(static_cast<PixelType *>(nullptr));
241
242
243 this->SetFunctor(this->GetFunctor());
244 }
245
246private:
247 itkGetConstReferenceMacro(Functor, FunctorType);
250 {
251 return m_Functor;
252 }
253
255
256 template <typename TPixelType>
257 void
258 CheckOutsideValue(const TPixelType *)
259 {}
260
261 template <typename TValue>
262 void
264 {
265 // Check to see if the outside value contains only zeros. If so,
266 // resize it to have the same number of zeros as the output
267 // image. Otherwise, check that the number of components in the
268 // outside value is the same as the number of components in the
269 // output image. If not, throw an exception.
270 const VariableLengthVector<TValue> currentValue = this->GetFunctor().GetOutsideValue();
271 VariableLengthVector<TValue> zeroVector(currentValue.GetSize());
272 zeroVector.Fill(TValue{});
273
274 if (currentValue == zeroVector)
275 {
276 zeroVector.SetSize(this->GetOutput()->GetVectorLength());
277 zeroVector.Fill(TValue{});
278 this->GetFunctor().SetOutsideValue(zeroVector);
279 }
280 else if (this->GetFunctor().GetOutsideValue().GetSize() != this->GetOutput()->GetVectorLength())
281 {
282 itkExceptionMacro("Number of components in OutsideValue: "
283 << this->GetFunctor().GetOutsideValue().GetSize() << " is not the same as the "
284 << "number of components in the image: " << this->GetOutput()->GetVectorLength());
285 }
286 }
287};
288} // end namespace itk
289
290#endif
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
OutputImageType * GetOutput()
void PrintSelf(std::ostream &os, Indent indent) const override
Control indentation during Print() invocation.
Definition itkIndent.h:50
Functor:: MaskNegatedInput< typename TInputImage::PixelType, typename TMaskImage::PixelType, typename TOutputImage::PixelType > FunctorType
BinaryGeneratorImageFilter< TInputImage, TMaskImage, TOutputImage > Superclass
void SetOutsideValue(const typename TOutputImage::PixelType &outsideValue)
void SetMaskImage(const MaskImageType *maskImage)
void CheckOutsideValue(const TPixelType *)
const TMaskImage::PixelType & GetMaskingValue() const
virtual const FunctorType & GetFunctor() 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
SmartPointer< const Self > ConstPointer
virtual void Modified() const
virtual void SetNthInput(DataObjectPointerArraySizeType idx, DataObject *input)
DataObject * GetInput(const DataObjectIdentifierType &key)
Return an input.
Implements transparent reference counting.
#define itkConceptMacro(name, concept)
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition itkMath.h:731
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....