ITK  5.4.0
Insight Toolkit
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 ~MaskInput() = default;
44 bool
45 operator==(const MaskInput &) const
46 {
47 return true;
48 }
49
51
52 inline TOutput
53 operator()(const TInput & A, const TMask & B) const
54 {
55 if (B != m_MaskingValue)
56 {
57 return static_cast<TOutput>(A);
58 }
59 else
60 {
61 return m_OutsideValue;
62 }
63 }
64
66 void
67 SetOutsideValue(const TOutput & outsideValue)
68 {
69 m_OutsideValue = outsideValue;
70 }
71
73 const TOutput &
75 {
76 return m_OutsideValue;
77 }
78
80 void
81 SetMaskingValue(const TMask & maskingValue)
82 {
83 m_MaskingValue = maskingValue;
84 }
85
87 const TMask &
89 {
90 return m_MaskingValue;
91 }
92
93private:
94 template <typename TPixelType>
95 TPixelType
96 DefaultOutsideValue(TPixelType *)
97 {
98 return TPixelType{};
99 }
100
101 template <typename TValue>
104 {
105 // set the outside value to be of zero length
107 }
108 TOutput m_OutsideValue{ DefaultOutsideValue(static_cast<TOutput *>(nullptr)) };
110};
111} // namespace Functor
112
147template <typename TInputImage, typename TMaskImage, typename TOutputImage = TInputImage>
148class ITK_TEMPLATE_EXPORT MaskImageFilter : public BinaryGeneratorImageFilter<TInputImage, TMaskImage, TOutputImage>
149
150{
151public:
152 ITK_DISALLOW_COPY_AND_MOVE(MaskImageFilter);
153
157
158 using FunctorType = Functor::
159 MaskInput<typename TInputImage::PixelType, typename TMaskImage::PixelType, typename TOutputImage::PixelType>;
160
163
165 itkNewMacro(Self);
166
168 itkOverrideGetNameOfClassMacro(MaskImageFilter);
169
171 using MaskImageType = TMaskImage;
172
177 void
178 SetMaskImage(const MaskImageType * maskImage)
179 {
180 // Process object is not const-correct so the const casting is required.
181 this->SetNthInput(1, const_cast<MaskImageType *>(maskImage));
182 }
183 const MaskImageType *
185 {
186 return static_cast<const MaskImageType *>(this->ProcessObject::GetInput(1));
187 }
191 void
192 SetOutsideValue(const typename TOutputImage::PixelType & outsideValue)
193 {
194 if (Math::NotExactlyEquals(this->GetOutsideValue(), outsideValue))
195 {
196 this->Modified();
197 this->GetFunctor().SetOutsideValue(outsideValue);
198 }
199 }
202 const typename TOutputImage::PixelType &
204 {
205 return this->GetFunctor().GetOutsideValue();
206 }
207
209 void
210 SetMaskingValue(const typename TMaskImage::PixelType & maskingValue)
211 {
212 if (this->GetMaskingValue() != maskingValue)
213 {
214 this->Modified();
215 this->GetFunctor().SetMaskingValue(maskingValue);
216 }
217 }
221 const typename TMaskImage::PixelType &
223 {
224 return this->GetFunctor().GetMaskingValue();
225 }
226
227#ifdef ITK_USE_CONCEPT_CHECKING
228 // Begin concept checking
230 itkConceptMacro(InputConvertibleToOutputCheck,
232 // End concept checking
233#endif
234
235protected:
236 MaskImageFilter() = default;
237 ~MaskImageFilter() override = default;
238
239 void
240 PrintSelf(std::ostream & os, Indent indent) const override
241 {
242 Superclass::PrintSelf(os, indent);
243 os << indent << "OutsideValue: " << this->GetOutsideValue() << std::endl;
244 }
245
246 void
248 {
249 using PixelType = typename TOutputImage::PixelType;
250 this->CheckOutsideValue(static_cast<PixelType *>(nullptr));
251
252 this->SetFunctor(this->GetFunctor());
253 }
254
255private:
256 itkGetConstReferenceMacro(Functor, FunctorType);
259 {
260 return m_Functor;
261 }
262
263 FunctorType m_Functor{};
264
265 template <typename TPixelType>
266 void
267 CheckOutsideValue(const TPixelType *)
268 {}
269
270 template <typename TValue>
271 void
273 {
274 // Check to see if the outside value contains only zeros. If so,
275 // resize it to have the same number of zeros as the output
276 // image. Otherwise, check that the number of components in the
277 // outside value is the same as the number of components in the
278 // output image. If not, throw an exception.
279 VariableLengthVector<TValue> currentValue = this->GetFunctor().GetOutsideValue();
280 VariableLengthVector<TValue> zeroVector(currentValue.GetSize());
281 zeroVector.Fill(TValue{});
282
283 if (currentValue == zeroVector)
284 {
285 zeroVector.SetSize(this->GetOutput()->GetVectorLength());
286 zeroVector.Fill(TValue{});
287 this->GetFunctor().SetOutsideValue(zeroVector);
288 }
289 else if (this->GetFunctor().GetOutsideValue().GetSize() != this->GetOutput()->GetVectorLength())
290 {
291 itkExceptionMacro("Number of components in OutsideValue: "
292 << this->GetFunctor().GetOutsideValue().GetSize() << " is not the same as the "
293 << "number of components in the image: " << this->GetOutput()->GetVectorLength());
294 }
295 }
296};
297} // end namespace itk
298
299#endif
Implements pixel-wise generic operation of two images, or of an image and a constant.
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
Control indentation during Print() invocation.
Definition: itkIndent.h:50
Light weight base class for most itk classes.
Mask an image with a mask.
void CheckOutsideValue(const VariableLengthVector< TValue > *)
void BeforeThreadedGenerateData() override
void SetMaskingValue(const typename TMaskImage::PixelType &maskingValue)
const MaskImageType * GetMaskImage()
const TMaskImage::PixelType & GetMaskingValue() const
void CheckOutsideValue(const TPixelType *)
void PrintSelf(std::ostream &os, Indent indent) const override
MaskImageFilter()=default
const TOutputImage::PixelType & GetOutsideValue() const
~MaskImageFilter() override=default
void SetMaskImage(const MaskImageType *maskImage)
void SetOutsideValue(const typename TOutputImage::PixelType &outsideValue)
FunctorType & GetFunctor()
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:737
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....