ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkMaskImageFilter.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 itkMaskImageFilter_h
19#define itkMaskImageFilter_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 MaskInput() = default;
42
43 bool
44 operator==(const MaskInput &) const
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 static_cast<TOutput>(A);
57 }
58
59 return m_OutsideValue;
60 }
61
63 void
64 SetOutsideValue(const TOutput & outsideValue)
65 {
66 m_OutsideValue = outsideValue;
67 }
68
70 [[nodiscard]] const TOutput &
72 {
73 return m_OutsideValue;
74 }
75
77 void
78 SetMaskingValue(const TMask & maskingValue)
79 {
80 m_MaskingValue = maskingValue;
81 }
82
84 [[nodiscard]] 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 MaskImageFilter : public BinaryGeneratorImageFilter<TInputImage, TMaskImage, TOutputImage>
146{
147public:
148 ITK_DISALLOW_COPY_AND_MOVE(MaskImageFilter);
149
153
154 using FunctorType = Functor::
155 MaskInput<typename TInputImage::PixelType, typename TMaskImage::PixelType, typename TOutputImage::PixelType>;
156
159
161 itkNewMacro(Self);
162
164 itkOverrideGetNameOfClassMacro(MaskImageFilter);
165
167 using MaskImageType = TMaskImage;
168
174 void
175 SetMaskImage(const MaskImageType * maskImage)
176 {
177 // Process object is not const-correct so the const casting is required.
178 this->SetNthInput(1, const_cast<MaskImageType *>(maskImage));
179 }
180 const MaskImageType *
182 {
183 return static_cast<const MaskImageType *>(this->ProcessObject::GetInput(1));
184 }
185
186
188 void
189 SetOutsideValue(const typename TOutputImage::PixelType & outsideValue)
190 {
191 if (Math::NotExactlyEquals(this->GetOutsideValue(), outsideValue))
192 {
193 this->Modified();
194 this->GetFunctor().SetOutsideValue(outsideValue);
195 }
196 }
197
198 const typename TOutputImage::PixelType &
200 {
201 return this->GetFunctor().GetOutsideValue();
202 }
203
205 void
206 SetMaskingValue(const typename TMaskImage::PixelType & maskingValue)
207 {
208 if (this->GetMaskingValue() != maskingValue)
209 {
210 this->Modified();
211 this->GetFunctor().SetMaskingValue(maskingValue);
212 }
213 }
214
216 const typename TMaskImage::PixelType &
218 {
219 return this->GetFunctor().GetMaskingValue();
220 }
221
223 itkConceptMacro(InputConvertibleToOutputCheck,
225
226protected:
227 MaskImageFilter() = default;
228 ~MaskImageFilter() override = default;
229
230 void
231 PrintSelf(std::ostream & os, Indent indent) const override
232 {
233 Superclass::PrintSelf(os, indent);
234 os << indent << "OutsideValue: " << this->GetOutsideValue() << std::endl;
235 }
236
237 void
239 {
240 using PixelType = typename TOutputImage::PixelType;
241 this->CheckOutsideValue(static_cast<PixelType *>(nullptr));
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
VariableLengthVector< TValue > DefaultOutsideValue(VariableLengthVector< TValue > *)
void SetMaskingValue(const TMask &maskingValue)
TOutput operator()(const TInput &A, const TMask &B) const
const TOutput & GetOutsideValue() const
void SetOutsideValue(const TOutput &outsideValue)
typename NumericTraits< TInput >::AccumulateType AccumulatorType
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(MaskInput)
bool operator==(const MaskInput &) const
TPixelType DefaultOutsideValue(TPixelType *)
const TMask & GetMaskingValue() const
OutputImageType * GetOutput()
void PrintSelf(std::ostream &os, Indent indent) const override
Control indentation during Print() invocation.
Definition itkIndent.h:51
void CheckOutsideValue(const VariableLengthVector< TValue > *)
void BeforeThreadedGenerateData() override
void SetMaskingValue(const typename TMaskImage::PixelType &maskingValue)
SmartPointer< Self > Pointer
SmartPointer< const Self > ConstPointer
const MaskImageType * GetMaskImage()
const TMaskImage::PixelType & GetMaskingValue() const
void CheckOutsideValue(const TPixelType *)
virtual const FunctorType & GetFunctor() const
void PrintSelf(std::ostream &os, Indent indent) const override
MaskImageFilter()=default
const TOutputImage::PixelType & GetOutsideValue() const
~MaskImageFilter() override=default
BinaryGeneratorImageFilter< TInputImage, TMaskImage, TOutputImage > Superclass
void SetMaskImage(const MaskImageType *maskImage)
Functor:: MaskInput< typename TInputImage::PixelType, typename TMaskImage::PixelType, typename TOutputImage::PixelType > FunctorType
void SetOutsideValue(const typename TOutputImage::PixelType &outsideValue)
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)
constexpr bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition itkMath.h:754
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....