Medical Imaging Interaction Toolkit  2018.4.99-b20efe7f
Medical Imaging Interaction Toolkit
mitkMinMaxLabelmageFilterWithIndex.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef MITK_MINMAXLABELIMAGEFILTERWITHINDEX_H
14 #define MITK_MINMAXLABELIMAGEFILTERWITHINDEX_H
15 
17 
18 #include <itkImage.h>
19 #include <itkImageToImageFilter.h>
20 #include <itkImageRegionConstIteratorWithIndex.h>
21 #include "itksys/hash_map.hxx"
22 
23 
24 namespace itk
25 {
26 template <typename TInputImage, typename TLabelImage>
27 class MinMaxLabelImageFilterWithIndex: public itk::ImageToImageFilter<TInputImage, TInputImage>
28 {
29 public:
32  typedef ImageToImageFilter< TInputImage, TInputImage > Superclass;
35 
37  itkNewMacro(Self);
38 
40  itkTypeMacro(MinMaxLabelImageFilterWithIndex, ImageToImageFilter);
41 
42  typedef typename TInputImage::RegionType RegionType;
43  typedef typename TInputImage::SizeType SizeType;
44  typedef typename TInputImage::IndexType IndexType;
45  typedef typename TInputImage::PixelType PixelType;
46  typedef typename NumericTraits< PixelType >::RealType RealType;
47 
48  typedef typename TLabelImage::RegionType LabelRegionType;
49  typedef typename TLabelImage::SizeType LabelSizeType;
50  typedef typename TLabelImage::IndexType LabelIndexType;
51  typedef typename TLabelImage::PixelType LabelPixelType;
52 
57  {
58  public:
59  PixelType m_Min, m_Max;
60  IndexType m_MinIndex, m_MaxIndex;
61 
63  m_Min(std::numeric_limits<PixelType>::max()),
64  m_Max(std::numeric_limits<PixelType>::min())
65  {}
66  };
67 
68  typedef typename itksys::hash_map<LabelPixelType, LabelExtrema> ExtremaMapType;
69  typedef typename ExtremaMapType::iterator ExtremaMapTypeIterator;
70  typedef typename ExtremaMapType::const_iterator ExtremaMapTypeConstIterator;
71  typedef typename ExtremaMapType::value_type MapValueType;
72 
73  PixelType GetMin(LabelPixelType label) const
74  {
75  ExtremaMapTypeConstIterator it = m_LabelExtrema.find(label);
76  if (it == m_LabelExtrema.end())
77  {
78  MITK_ERROR << "invalid label";
79  }
80 
81  return (*it).second.m_Min;
82  }
83 
84  PixelType GetMax(LabelPixelType label) const
85  {
86  ExtremaMapTypeConstIterator it = m_LabelExtrema.find(label);
87  if (it == m_LabelExtrema.end())
88  {
89  MITK_ERROR << "invalid label";
90  }
91 
92  return (*it).second.m_Max;
93  }
94 
98  std::vector<LabelPixelType> GetRelevantLabels() const
99  {
100  std::vector<LabelPixelType> labels;
101  for (auto&& it:m_LabelExtrema)
102  {
103  labels.push_back(it.first);
104  }
105  return labels;
106  }
107 
108  IndexType GetMinIndex(LabelPixelType label) const
109  {
110  ExtremaMapTypeConstIterator it = m_LabelExtrema.find(label);
111  if (it == m_LabelExtrema.end())
112  {
113  MITK_ERROR << "invalid label";
114  }
115 
116  return (*it).second.m_MinIndex;
117 
118  }
119 
120  IndexType GetMaxIndex(LabelPixelType label) const
121  {
122  ExtremaMapTypeConstIterator it = m_LabelExtrema.find(label);
123  if (it == m_LabelExtrema.end())
124  {
125  MITK_ERROR << "invalid label";
126  }
127 
128  return (*it).second.m_MaxIndex;
129 
130  }
131 
132  PixelType GetGlobalMin() const
133  {
134  return m_GlobalMin;
135  }
136 
137  PixelType GetGlobalMax() const
138  {
139  return m_GlobalMax;
140  }
141 
142  IndexType GetGlobalMinIndex() const
143  {
144  return m_GlobalMinIndex;
145  }
146 
147  IndexType GetGlobalMaxIndex() const
148  {
149  return m_GlobalMaxIndex;
150  }
151 
153  void SetLabelInput(const TLabelImage *input)
154  {
155  // Process object is not const-correct so the const casting is required.
156  this->SetNthInput( 1, const_cast< TLabelImage * >( input ) );
157  }
158 
160  const TLabelImage * GetLabelInput() const
161  {
162  return itkDynamicCastInDebugMode< TLabelImage * >( const_cast< DataObject * >( this->ProcessObject::GetInput(1) ) );
163  }
164 
165 protected:
166  void AllocateOutputs() override;
167 
168  void ThreadedGenerateData(const RegionType &
169  outputRegionForThread,
170  ThreadIdType threadId) override;
171 
172  void BeforeThreadedGenerateData() override;
173 
174  void AfterThreadedGenerateData() override;
175 
176 private:
177  std::vector<ExtremaMapType> m_ThreadExtrema;
178 
179  ExtremaMapType m_LabelExtrema;
180  PixelType m_GlobalMin;
181  PixelType m_GlobalMax;
182  IndexType m_GlobalMinIndex, m_GlobalMaxIndex;
183 };
184 }
185 
186 #include "mitkMinMaxLabelmageFilterWithIndex.hxx"
187 
188 
189 #endif
itksys::hash_map< LabelPixelType, LabelExtrema > ExtremaMapType
#define MITK_ERROR
Definition: mitkLogMacros.h:20
NumericTraits< PixelType >::RealType RealType
std::vector< LabelPixelType > GetRelevantLabels() const
Returns a std::vector containing all labels for which min and max values (and indices) have been comp...
STL namespace.
PixelType GetMax(LabelPixelType label) const
static T max(T x, T y)
Definition: svm.cpp:56
IndexType GetMaxIndex(LabelPixelType label) const
IndexType GetMinIndex(LabelPixelType label) const
static T min(T x, T y)
Definition: svm.cpp:53
PixelType GetMin(LabelPixelType label) const
void ThreadedGenerateData(const RegionType &outputRegionForThread, ThreadIdType threadId) override
The LabelExtrema class is just a container for global min/max values and their indices as well as all...
ImageToImageFilter< TInputImage, TInputImage > Superclass
ExtremaMapType::const_iterator ExtremaMapTypeConstIterator