ITK  5.3.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  * 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 itkAttributeMorphologyBaseImageFilter_h
19 #define itkAttributeMorphologyBaseImageFilter_h
20 
21 #include "itkImageToImageFilter.h"
22 #include <vector>
23 #include <memory> // For unique_ptr.
24 
25 namespace itk
26 {
54 template <typename TInputImage, typename TOutputImage, typename TAttribute, typename TFunction>
55 class ITK_TEMPLATE_EXPORT AttributeMorphologyBaseImageFilter : public ImageToImageFilter<TInputImage, TOutputImage>
56 {
57 public:
63 
67  using typename Superclass::InputImagePointer;
68 
73  using OutputPixelType = typename TOutputImage::PixelType;
74  using OutputInternalPixelType = typename TOutputImage::InternalPixelType;
75  using InputPixelType = typename TInputImage::PixelType;
76  using InputInternalPixelType = typename TInputImage::InternalPixelType;
78  using OffsetType = typename TInputImage::OffsetType;
79  using SizeType = typename TInputImage::SizeType;
80 
81  static constexpr unsigned int ImageDimension = TOutputImage::ImageDimension;
82 
86  using InputImageType = TInputImage;
87  using OutputImageType = TOutputImage;
88  // using IndexType = typename TInputImage::IndexType;
89  // using SizeType = typename TInputImage::SizeType;
91  using ListType = std::list<IndexType>;
92  using AttributeType = TAttribute;
93 
99 
104 
108  itkNewMacro(Self);
109 
116  itkSetMacro(FullyConnected, bool);
117  itkGetConstReferenceMacro(FullyConnected, bool);
118  itkBooleanMacro(FullyConnected);
126  itkSetMacro(Lambda, AttributeType);
127  itkGetConstMacro(Lambda, AttributeType);
130 protected:
132  {
133  m_FullyConnected = false;
134  m_AttributeValuePerPixel = 1;
135  m_Lambda = 0;
136  }
137 
138  ~AttributeMorphologyBaseImageFilter() override = default;
140  void
141  PrintSelf(std::ostream & os, Indent indent) const override;
142 
146  void
147  GenerateData() override;
148 
152  void
153  GenerateInputRequestedRegion() override;
154 
159  void
160  EnlargeOutputRequestedRegion(DataObject * itkNotUsed(output)) override;
161 
163 
164 private:
167 
168  // some constants used several times in the code
169  static constexpr OffsetValueType INACTIVE = -1;
170  static constexpr OffsetValueType ACTIVE = -2;
171 
172  // Just used for area/volume openings at the moment
173  std::unique_ptr<AttributeType[]> m_AuxData;
174 
175  using OffsetVecType = std::vector<OffsetType>;
176  // offset in the linear array.
177  using OffsetDirectVecType = std::vector<OffsetValueType>;
178 
179  void
180  SetupOffsetVec(OffsetDirectVecType & PosOffsets, OffsetVecType & Offsets);
181 
182  // m_SortPixels contains offsets into the raw image
183  // it is sorted with a stable sort by grey level as the
184  // first step in the algorithm. The sorting step avoids
185  // the need to explicitly locate regional extrema.
186  std::unique_ptr<OffsetValueType[]> m_SortPixels;
187  std::unique_ptr<OffsetValueType[]> m_Parent;
188 
189  // This is a bit ugly, but I can't see an easy way around
190  std::unique_ptr<InputPixelType[]> m_Raw;
191 
193  {
194  public:
195  TFunction m_TFunction;
196  // buf contains the raw data, which is what
197  // we want to sort by. i.e. the first value in
198  // the sorted buffer will be the location of the
199  // largest or smallest pixel.
201  bool
202  operator()(OffsetValueType const & l, OffsetValueType const & r) const
203  {
204  return (m_TFunction(buf[l], buf[r]));
205  }
206  };
207 
208  CompareOffsetType m_CompareOffset;
209  // version from PAMI. Note - using the AuxData array rather than the
210  // parent array to store area
211  void
213  {
214  m_Parent[x] = ACTIVE;
215  m_AuxData[x] = m_AttributeValuePerPixel;
216  }
217 
220  {
221  if (m_Parent[x] >= 0)
222  {
223  m_Parent[x] = FindRoot(m_Parent[x]);
224  return (m_Parent[x]);
225  }
226  else
227  {
228  return (x);
229  }
230  }
231 
232  bool
234  {
235  return ((m_Raw[x] == m_Raw[y]) || (m_AuxData[x] < m_Lambda));
236  }
237 
238  void
240  {
241  OffsetValueType r = FindRoot(n);
242 
243  if (r != p)
244  {
245  if (Criterion(r, p))
246  {
247  m_AuxData[p] += m_AuxData[r];
248  m_Parent[r] = p;
249  }
250  else
251  {
252  m_AuxData[p] = m_Lambda;
253  }
254  }
255  }
256 };
257 } // end namespace itk
258 
259 #ifndef ITK_MANUAL_INSTANTIATION
260 # include "itkAttributeMorphologyBaseImageFilter.hxx"
261 #endif
262 
263 #endif
itk::AttributeMorphologyBaseImageFilter::m_AuxData
std::unique_ptr< AttributeType[]> m_AuxData
Definition: itkAttributeMorphologyBaseImageFilter.h:173
itk::AttributeMorphologyBaseImageFilter< TInputImage, TOutputImage, TAttribute, std::greater< TInputImage::PixelType > >::OutputPixelType
typename TOutputImage::PixelType OutputPixelType
Definition: itkAttributeMorphologyBaseImageFilter.h:73
itk::AttributeMorphologyBaseImageFilter::m_AttributeValuePerPixel
AttributeType m_AttributeValuePerPixel
Definition: itkAttributeMorphologyBaseImageFilter.h:162
itk::AttributeMorphologyBaseImageFilter< TInputImage, TOutputImage, TAttribute, std::greater< TInputImage::PixelType > >::OffsetDirectVecType
std::vector< OffsetValueType > OffsetDirectVecType
Definition: itkAttributeMorphologyBaseImageFilter.h:177
itk::AttributeMorphologyBaseImageFilter< TInputImage, TOutputImage, TAttribute, std::greater< TInputImage::PixelType > >::OffsetType
typename TInputImage::OffsetType OffsetType
Definition: itkAttributeMorphologyBaseImageFilter.h:78
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:166
itk::AttributeMorphologyBaseImageFilter< TInputImage, TOutputImage, TAttribute, std::greater< TInputImage::PixelType > >::OffsetVecType
std::vector< OffsetType > OffsetVecType
Definition: itkAttributeMorphologyBaseImageFilter.h:175
itk::AttributeMorphologyBaseImageFilter::CompareOffsetType::buf
InputPixelType * buf
Definition: itkAttributeMorphologyBaseImageFilter.h:200
itk::AttributeMorphologyBaseImageFilter::Criterion
bool Criterion(OffsetValueType x, OffsetValueType y)
Definition: itkAttributeMorphologyBaseImageFilter.h:233
itk::AttributeMorphologyBaseImageFilter::m_SortPixels
std::unique_ptr< OffsetValueType[]> m_SortPixels
Definition: itkAttributeMorphologyBaseImageFilter.h:186
itk::AttributeMorphologyBaseImageFilter< TInputImage, TOutputImage, TAttribute, std::greater< TInputImage::PixelType > >::InputPixelType
typename TInputImage::PixelType InputPixelType
Definition: itkAttributeMorphologyBaseImageFilter.h:75
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:139
itk::AttributeMorphologyBaseImageFilter< TInputImage, TOutputImage, TAttribute, std::greater< TInputImage::PixelType > >::InputInternalPixelType
typename TInputImage::InternalPixelType InputInternalPixelType
Definition: itkAttributeMorphologyBaseImageFilter.h:76
itk::AttributeMorphologyBaseImageFilter< TInputImage, TOutputImage, TAttribute, std::greater< TInputImage::PixelType > >::SizeType
typename TInputImage::SizeType SizeType
Definition: itkAttributeMorphologyBaseImageFilter.h:79
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:77
itk::AttributeMorphologyBaseImageFilter::CompareOffsetType::m_TFunction
TFunction m_TFunction
Definition: itkAttributeMorphologyBaseImageFilter.h:195
itk::AttributeMorphologyBaseImageFilter
Morphological opening by attributes.
Definition: itkAttributeMorphologyBaseImageFilter.h:55
itk::ImageToImageFilter::InputImageType
TInputImage InputImageType
Definition: itkImageToImageFilter.h:129
itk::AttributeMorphologyBaseImageFilter::Union
void Union(OffsetValueType n, OffsetValueType p)
Definition: itkAttributeMorphologyBaseImageFilter.h:239
itk::AttributeMorphologyBaseImageFilter::MakeSet
void MakeSet(OffsetValueType x)
Definition: itkAttributeMorphologyBaseImageFilter.h:212
itkImageToImageFilter.h
itk::OffsetValueType
long OffsetValueType
Definition: itkIntTypes.h:94
itk::AttributeMorphologyBaseImageFilter< TInputImage, TOutputImage, TAttribute, std::greater< TInputImage::PixelType > >::RegionType
typename TOutputImage::RegionType RegionType
Definition: itkAttributeMorphologyBaseImageFilter.h:90
itk::AttributeMorphologyBaseImageFilter::m_CompareOffset
CompareOffsetType m_CompareOffset
Definition: itkAttributeMorphologyBaseImageFilter.h:208
itk::AttributeMorphologyBaseImageFilter::FindRoot
OffsetValueType FindRoot(OffsetValueType x)
Definition: itkAttributeMorphologyBaseImageFilter.h:219
itk::AttributeMorphologyBaseImageFilter< TInputImage, TOutputImage, TAttribute, std::greater< TInputImage::PixelType > >::AttributeType
TAttribute AttributeType
Definition: itkAttributeMorphologyBaseImageFilter.h:92
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::ProcessObject
The base class for all process objects (source, filters, mappers) in the Insight data processing pipe...
Definition: itkProcessObject.h:139
itk::AttributeMorphologyBaseImageFilter< TInputImage, TOutputImage, TAttribute, std::greater< TInputImage::PixelType > >::OutputInternalPixelType
typename TOutputImage::InternalPixelType OutputInternalPixelType
Definition: itkAttributeMorphologyBaseImageFilter.h:74
itk::AttributeMorphologyBaseImageFilter::m_Raw
std::unique_ptr< InputPixelType[]> m_Raw
Definition: itkAttributeMorphologyBaseImageFilter.h:190
itk::AttributeMorphologyBaseImageFilter::CompareOffsetType
Definition: itkAttributeMorphologyBaseImageFilter.h:192
itk::AttributeMorphologyBaseImageFilter::m_FullyConnected
bool m_FullyConnected
Definition: itkAttributeMorphologyBaseImageFilter.h:165
itk::AttributeMorphologyBaseImageFilter< TInputImage, TOutputImage, TAttribute, std::greater< TInputImage::PixelType > >::ListType
std::list< IndexType > ListType
Definition: itkAttributeMorphologyBaseImageFilter.h:91
itk::AttributeMorphologyBaseImageFilter::CompareOffsetType::operator()
bool operator()(OffsetValueType const &l, OffsetValueType const &r) const
Definition: itkAttributeMorphologyBaseImageFilter.h:202
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::m_Parent
std::unique_ptr< OffsetValueType[]> m_Parent
Definition: itkAttributeMorphologyBaseImageFilter.h:187
itk::AttributeMorphologyBaseImageFilter::AttributeMorphologyBaseImageFilter
AttributeMorphologyBaseImageFilter()
Definition: itkAttributeMorphologyBaseImageFilter.h:131