ITK  5.2.0
Insight Toolkit
itkAttributeMorphologyBaseImageFilter.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  * http://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 itkAttributeMorphologyBaseImageFilter_h
19 #define itkAttributeMorphologyBaseImageFilter_h
20 
21 #include "itkImageToImageFilter.h"
22 #include <vector>
23 
24 #define PAMI
25 
26 namespace itk
27 {
62 template <typename TInputImage, typename TOutputImage, typename TAttribute, typename TFunction>
63 class ITK_TEMPLATE_EXPORT AttributeMorphologyBaseImageFilter : public ImageToImageFilter<TInputImage, TOutputImage>
64 {
65 public:
71 
75  using InputImagePointer = typename Superclass::InputImagePointer;
76 
81  using OutputPixelType = typename TOutputImage::PixelType;
82  using OutputInternalPixelType = typename TOutputImage::InternalPixelType;
83  using InputPixelType = typename TInputImage::PixelType;
84  using InputInternalPixelType = typename TInputImage::InternalPixelType;
86  using OffsetType = typename TInputImage::OffsetType;
87  using SizeType = typename TInputImage::SizeType;
88 
89  static constexpr unsigned int ImageDimension = TOutputImage::ImageDimension;
90 
94  using InputImageType = TInputImage;
95  using OutputImageType = TOutputImage;
96  // using IndexType = typename TInputImage::IndexType;
97  // using SizeType = typename TInputImage::SizeType;
99  using ListType = std::list<IndexType>;
100  using AttributeType = TAttribute;
101 
107 
112 
116  itkNewMacro(Self);
117 
124  itkSetMacro(FullyConnected, bool);
125  itkGetConstReferenceMacro(FullyConnected, bool);
126  itkBooleanMacro(FullyConnected);
128 
134  itkSetMacro(Lambda, AttributeType);
135  itkGetConstMacro(Lambda, AttributeType);
137 
138 protected:
140  {
141  m_FullyConnected = false;
142  m_AttributeValuePerPixel = 1;
143  m_Lambda = 0;
144  }
145 
146  ~AttributeMorphologyBaseImageFilter() override = default;
148  void
149  PrintSelf(std::ostream & os, Indent indent) const override;
150 
154  void
155  GenerateData() override;
156 
160  void
161  GenerateInputRequestedRegion() override;
162 
167  void
168  EnlargeOutputRequestedRegion(DataObject * itkNotUsed(output)) override;
169 
171 
172 private:
175 
176  // some constants used several times in the code
177  static constexpr OffsetValueType INACTIVE = -1;
178  static constexpr OffsetValueType ACTIVE = -2;
179  static constexpr OffsetValueType ROOT = -3;
180 
181  // Just used for area/volume openings at the moment
183 
184  using OffsetVecType = std::vector<OffsetType>;
185  // offset in the linear array.
186  using OffsetDirectVecType = std::vector<OffsetValueType>;
187 
188  void
189  SetupOffsetVec(OffsetDirectVecType & PosOffsets, OffsetVecType & Offsets);
190 
192  {
193  public:
196  };
197 
198  GreyAndPos * m_SortPixels;
200 #ifndef PAMI
201  bool * m_Processed;
202 #endif
203  // This is a bit ugly, but I can't see an easy way around
205 
207  {
208  public:
209  TFunction m_TFunction;
210  bool
211  operator()(GreyAndPos const & l, GreyAndPos const & r) const
212  {
213  if (m_TFunction(l.Val, r.Val))
214  {
215  return true;
216  }
217  if (l.Val == r.Val)
218  {
219  return (l.Pos < r.Pos);
220  }
221  return false;
222  }
223  };
224 
225 #ifdef PAMI
226  // version from PAMI. Note - using the AuxData array rather than the
227  // parent array to store area
228  void
230  {
231  m_Parent[x] = ACTIVE;
232  m_AuxData[x] = m_AttributeValuePerPixel;
233  }
234 
237  {
238  if (m_Parent[x] >= 0)
239  {
240  m_Parent[x] = FindRoot(m_Parent[x]);
241  return (m_Parent[x]);
242  }
243  else
244  {
245  return (x);
246  }
247  }
248 
249  bool
251  {
252  return ((m_Raw[x] == m_Raw[y]) || (m_AuxData[x] < m_Lambda));
253  }
254 
255  void
257  {
258  OffsetValueType r = FindRoot(n);
259 
260  if (r != p)
261  {
262  if (Criterion(r, p))
263  {
264  m_AuxData[p] += m_AuxData[r];
265  m_Parent[r] = p;
266  }
267  else
268  {
269  m_AuxData[p] = m_Lambda;
270  }
271  }
272  }
273 
274 #else
275  // version from ISMM paper
276  void
277  MakeSet(OffsetValueType x)
278  {
279  m_Parent[x] = ACTIVE;
280  m_AuxData[x] = m_AttributeValuePerPixel;
281  }
282 
283  void
285  {
286  if ((m_Parent[y] == ACTIVE) && (m_Parent[x] == ACTIVE))
287  {
288  // should be a call to MergeAuxData
289  m_AuxData[y] = m_AuxData[x] + m_AuxData[y];
290  m_AuxData[x] = -m_AttributeValuePerPixel;
291  }
292  else if (m_Parent[x] == ACTIVE)
293  {
294  m_AuxData[x] = -m_AttributeValuePerPixel;
295  }
296  else
297  {
298  m_AuxData[y] = -m_AttributeValuePerPixel;
299  m_Parent[y] = INACTIVE;
300  }
301  m_Parent[x] = y;
302  }
303 
305  FindRoot(OffsetValueType x)
306  {
307  if (m_Parent[x] >= 0)
308  {
309  m_Parent[x] = FindRoot(m_Parent[x]);
310  return (m_Parent[x]);
311  }
312  else
313  {
314  return (x);
315  }
316  }
317 
318  bool
319  Equiv(OffsetValueType x, OffsetValueType y)
320  {
321  return ((m_Raw[x] == m_Raw[y]) || (m_Parent[x] == ACTIVE));
322  }
323 
324  void
325  Union(OffsetValueType n, OffsetValueType p)
326  {
327  OffsetValueType r = FindRoot(n);
328 
329  if (r != p)
330  {
331  if (Equiv(r, p))
332  {
333  Link(r, p);
334  }
335  else if (m_Parent[p] == ACTIVE)
336  {
337  m_Parent[p] = INACTIVE;
338  m_AuxData[p] = -m_AttributeValuePerPixel;
339  }
340  }
341  }
342 
343 #endif
344 };
345 } // end namespace itk
346 
347 #ifndef ITK_MANUAL_INSTANTIATION
348 # include "itkAttributeMorphologyBaseImageFilter.hxx"
349 #endif
350 
351 #endif
itk::AttributeMorphologyBaseImageFilter< TInputImage, TOutputImage, TAttribute, std::greater< TInputImage::PixelType > >::OutputPixelType
typename TOutputImage::PixelType OutputPixelType
Definition: itkAttributeMorphologyBaseImageFilter.h:81
itk::AttributeMorphologyBaseImageFilter::m_AttributeValuePerPixel
AttributeType m_AttributeValuePerPixel
Definition: itkAttributeMorphologyBaseImageFilter.h:170
itk::AttributeMorphologyBaseImageFilter::GreyAndPos
Definition: itkAttributeMorphologyBaseImageFilter.h:191
itk::AttributeMorphologyBaseImageFilter< TInputImage, TOutputImage, TAttribute, std::greater< TInputImage::PixelType > >::OffsetDirectVecType
std::vector< OffsetValueType > OffsetDirectVecType
Definition: itkAttributeMorphologyBaseImageFilter.h:186
itk::AttributeMorphologyBaseImageFilter< TInputImage, TOutputImage, TAttribute, std::greater< TInputImage::PixelType > >::OffsetType
typename TInputImage::OffsetType OffsetType
Definition: itkAttributeMorphologyBaseImageFilter.h:86
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::AttributeMorphologyBaseImageFilter::m_Lambda
AttributeType m_Lambda
Definition: itkAttributeMorphologyBaseImageFilter.h:174
itk::AttributeMorphologyBaseImageFilter< TInputImage, TOutputImage, TAttribute, std::greater< TInputImage::PixelType > >::OffsetVecType
std::vector< OffsetType > OffsetVecType
Definition: itkAttributeMorphologyBaseImageFilter.h:184
itk::AttributeMorphologyBaseImageFilter::Criterion
bool Criterion(OffsetValueType x, OffsetValueType y)
Definition: itkAttributeMorphologyBaseImageFilter.h:250
itk::AttributeMorphologyBaseImageFilter::ComparePixStruct
Definition: itkAttributeMorphologyBaseImageFilter.h:206
itk::AttributeMorphologyBaseImageFilter< TInputImage, TOutputImage, TAttribute, std::greater< TInputImage::PixelType > >::InputPixelType
typename TInputImage::PixelType InputPixelType
Definition: itkAttributeMorphologyBaseImageFilter.h:83
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::ImageToImageFilter
Base class for filters that take an image as input and produce an image as output.
Definition: itkImageToImageFilter.h:108
itk::ImageSource
Base class for all process objects that output image data.
Definition: itkImageSource.h:67
itk::AttributeMorphologyBaseImageFilter::AttributeMorphologyBaseImageFilter
AttributeMorphologyBaseImageFilter(const Self &)
Definition: itkAttributeMorphologyBaseImageFilter.h:147
itk::AttributeMorphologyBaseImageFilter::GreyAndPos::Val
InputPixelType Val
Definition: itkAttributeMorphologyBaseImageFilter.h:194
itk::AttributeMorphologyBaseImageFilter< TInputImage, TOutputImage, TAttribute, std::greater< TInputImage::PixelType > >::InputInternalPixelType
typename TInputImage::InternalPixelType InputInternalPixelType
Definition: itkAttributeMorphologyBaseImageFilter.h:84
itk::AttributeMorphologyBaseImageFilter< TInputImage, TOutputImage, TAttribute, std::greater< TInputImage::PixelType > >::SizeType
typename TInputImage::SizeType SizeType
Definition: itkAttributeMorphologyBaseImageFilter.h:87
itk::ImageToImageFilter::InputImagePointer
typename InputImageType::Pointer InputImagePointer
Definition: itkImageToImageFilter.h:130
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itk::AttributeMorphologyBaseImageFilter< TInputImage, TOutputImage, TAttribute, std::greater< TInputImage::PixelType > >::IndexType
typename TInputImage::IndexType IndexType
Definition: itkAttributeMorphologyBaseImageFilter.h:85
itk::AttributeMorphologyBaseImageFilter
Morphological opening by attributes.
Definition: itkAttributeMorphologyBaseImageFilter.h:63
itk::ImageToImageFilter::InputImageType
TInputImage InputImageType
Definition: itkImageToImageFilter.h:129
itk::AttributeMorphologyBaseImageFilter::Union
void Union(OffsetValueType n, OffsetValueType p)
Definition: itkAttributeMorphologyBaseImageFilter.h:256
itk::AttributeMorphologyBaseImageFilter::MakeSet
void MakeSet(OffsetValueType x)
Definition: itkAttributeMorphologyBaseImageFilter.h:229
itkImageToImageFilter.h
itk::AttributeMorphologyBaseImageFilter< TInputImage, TOutputImage, TAttribute, std::greater< TInputImage::PixelType > >::RegionType
typename TOutputImage::RegionType RegionType
Definition: itkAttributeMorphologyBaseImageFilter.h:98
itk::AttributeMorphologyBaseImageFilter::ComparePixStruct::operator()
bool operator()(GreyAndPos const &l, GreyAndPos const &r) const
Definition: itkAttributeMorphologyBaseImageFilter.h:211
itk::AttributeMorphologyBaseImageFilter::m_Parent
OffsetValueType * m_Parent
Definition: itkAttributeMorphologyBaseImageFilter.h:199
itk::AttributeMorphologyBaseImageFilter::FindRoot
OffsetValueType FindRoot(OffsetValueType x)
Definition: itkAttributeMorphologyBaseImageFilter.h:236
itk::AttributeMorphologyBaseImageFilter::GreyAndPos::Pos
OffsetValueType Pos
Definition: itkAttributeMorphologyBaseImageFilter.h:195
itk::AttributeMorphologyBaseImageFilter< TInputImage, TOutputImage, TAttribute, std::greater< TInputImage::PixelType > >::AttributeType
TAttribute AttributeType
Definition: itkAttributeMorphologyBaseImageFilter.h:100
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::OffsetValueType
signed long OffsetValueType
Definition: itkIntTypes.h:94
itk::ProcessObject
The base class for all process objects (source, filters, mappers) in the Insight data processing pipe...
Definition: itkProcessObject.h:138
itk::AttributeMorphologyBaseImageFilter::m_SortPixels
GreyAndPos * m_SortPixels
Definition: itkAttributeMorphologyBaseImageFilter.h:198
itk::AttributeMorphologyBaseImageFilter< TInputImage, TOutputImage, TAttribute, std::greater< TInputImage::PixelType > >::OutputInternalPixelType
typename TOutputImage::InternalPixelType OutputInternalPixelType
Definition: itkAttributeMorphologyBaseImageFilter.h:82
itk::AttributeMorphologyBaseImageFilter::m_AuxData
AttributeType * m_AuxData
Definition: itkAttributeMorphologyBaseImageFilter.h:182
itk::AttributeMorphologyBaseImageFilter::m_FullyConnected
bool m_FullyConnected
Definition: itkAttributeMorphologyBaseImageFilter.h:173
itk::AttributeMorphologyBaseImageFilter< TInputImage, TOutputImage, TAttribute, std::greater< TInputImage::PixelType > >::ListType
std::list< IndexType > ListType
Definition: itkAttributeMorphologyBaseImageFilter.h:99
itk::AttributeMorphologyBaseImageFilter::ComparePixStruct::m_TFunction
TFunction m_TFunction
Definition: itkAttributeMorphologyBaseImageFilter.h:209
itk::AttributeMorphologyBaseImageFilter::m_Raw
InputPixelType * m_Raw
Definition: itkAttributeMorphologyBaseImageFilter.h:204
itk::ImageSource::OutputImageType
TOutputImage OutputImageType
Definition: itkImageSource.h:90
itk::DataObject
Base class for all data objects in ITK.
Definition: itkDataObject.h:293
itk::AttributeMorphologyBaseImageFilter::AttributeMorphologyBaseImageFilter
AttributeMorphologyBaseImageFilter()
Definition: itkAttributeMorphologyBaseImageFilter.h:139