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 ~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
60 return m_OutsideValue;
61 }
62
64 void
65 SetOutsideValue(const TOutput & outsideValue)
66 {
67 m_OutsideValue = outsideValue;
68 }
69
71 const TOutput &
73 {
74 return m_OutsideValue;
75 }
76
78 void
79 SetMaskingValue(const TMask & maskingValue)
80 {
81 m_MaskingValue = maskingValue;
82 }
83
85 const TMask &
87 {
88 return m_MaskingValue;
89 }
90
91private:
92 template <typename TPixelType>
93 TPixelType
94 DefaultOutsideValue(TPixelType *)
95 {
96 return TPixelType{};
97 }
98
99 template <typename TValue>
102 {
103 // set the outside value to be of zero length
105 }
106 TOutput m_OutsideValue{ DefaultOutsideValue(static_cast<TOutput *>(nullptr)) };
108};
109} // namespace Functor
110
145template <typename TInputImage, typename TMaskImage, typename TOutputImage = TInputImage>
146class ITK_TEMPLATE_EXPORT MaskImageFilter : public BinaryGeneratorImageFilter<TInputImage, TMaskImage, TOutputImage>
147
148{
149public:
150 ITK_DISALLOW_COPY_AND_MOVE(MaskImageFilter);
151
155
156 using FunctorType = Functor::
157 MaskInput<typename TInputImage::PixelType, typename TMaskImage::PixelType, typename TOutputImage::PixelType>;
158
161
163 itkNewMacro(Self);
164
166 itkOverrideGetNameOfClassMacro(MaskImageFilter);
167
169 using MaskImageType = TMaskImage;
170
175 void
176 SetMaskImage(const MaskImageType * maskImage)
177 {
178 // Process object is not const-correct so the const casting is required.
179 this->SetNthInput(1, const_cast<MaskImageType *>(maskImage));
180 }
181 const MaskImageType *
183 {
184 return static_cast<const MaskImageType *>(this->ProcessObject::GetInput(1));
185 }
186
187
189 void
190 SetOutsideValue(const typename TOutputImage::PixelType & outsideValue)
191 {
192 if (Math::NotExactlyEquals(this->GetOutsideValue(), outsideValue))
193 {
194 this->Modified();
195 this->GetFunctor().SetOutsideValue(outsideValue);
196 }
197 }
198
199
200 const typename TOutputImage::PixelType &
202 {
203 return this->GetFunctor().GetOutsideValue();
204 }
205
207 void
208 SetMaskingValue(const typename TMaskImage::PixelType & maskingValue)
209 {
210 if (this->GetMaskingValue() != maskingValue)
211 {
212 this->Modified();
213 this->GetFunctor().SetMaskingValue(maskingValue);
214 }
215 }
216
217
219 const typename TMaskImage::PixelType &
221 {
222 return this->GetFunctor().GetMaskingValue();
223 }
224
226 itkConceptMacro(InputConvertibleToOutputCheck,
228
229protected:
230 MaskImageFilter() = default;
231 ~MaskImageFilter() 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 this->SetFunctor(this->GetFunctor());
247 }
248
249private:
250 itkGetConstReferenceMacro(Functor, FunctorType);
253 {
254 return m_Functor;
255 }
256
258
259 template <typename TPixelType>
260 void
261 CheckOutsideValue(const TPixelType *)
262 {}
263
264 template <typename TValue>
265 void
267 {
268 // Check to see if the outside value contains only zeros. If so,
269 // resize it to have the same number of zeros as the output
270 // image. Otherwise, check that the number of components in the
271 // outside value is the same as the number of components in the
272 // output image. If not, throw an exception.
273 const VariableLengthVector<TValue> currentValue = this->GetFunctor().GetOutsideValue();
274 VariableLengthVector<TValue> zeroVector(currentValue.GetSize());
275 zeroVector.Fill(TValue{});
276
277 if (currentValue == zeroVector)
278 {
279 zeroVector.SetSize(this->GetOutput()->GetVectorLength());
280 zeroVector.Fill(TValue{});
281 this->GetFunctor().SetOutsideValue(zeroVector);
282 }
283 else if (this->GetFunctor().GetOutsideValue().GetSize() != this->GetOutput()->GetVectorLength())
284 {
285 itkExceptionMacro("Number of components in OutsideValue: "
286 << this->GetFunctor().GetOutsideValue().GetSize() << " is not the same as the "
287 << "number of components in the image: " << this->GetOutput()->GetVectorLength());
288 }
289 }
290};
291} // end namespace itk
292
293#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:50
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)
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition itkMath.h:731
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....