Medical Imaging Interaction Toolkit  2026.06.00
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 mitkMinMaxLabelmageFilterWithIndex_h
14 #define mitkMinMaxLabelmageFilterWithIndex_h
15 
17 
18 #include <itkImage.h>
19 #include <itkImageToImageFilter.h>
20 #include <itkImageRegionConstIteratorWithIndex.h>
21 #include <unordered_map>
22 
23 
24 namespace itk
25 {
39 template <typename TInputImage, typename TLabelImage>
40 class MinMaxLabelImageFilterWithIndex: public itk::ImageToImageFilter<TInputImage, TInputImage>
41 {
42 public:
45  typedef ImageToImageFilter< TInputImage, TInputImage > Superclass;
48 
50  itkNewMacro(Self);
51 
53  itkTypeMacro(MinMaxLabelImageFilterWithIndex, ImageToImageFilter);
54 
55  typedef typename TInputImage::RegionType RegionType;
56  typedef typename TInputImage::SizeType SizeType;
57  typedef typename TInputImage::IndexType IndexType;
58  typedef typename TInputImage::PixelType PixelType;
59  typedef typename NumericTraits< PixelType >::RealType RealType;
60 
61  typedef typename TLabelImage::RegionType LabelRegionType;
62  typedef typename TLabelImage::SizeType LabelSizeType;
63  typedef typename TLabelImage::IndexType LabelIndexType;
64  typedef typename TLabelImage::PixelType LabelPixelType;
65 
70  {
71  public:
74 
76  m_Min(std::numeric_limits<PixelType>::max()),
77  m_Max(std::numeric_limits<PixelType>::min())
78  {}
79  };
80 
81  typedef typename std::unordered_map<LabelPixelType, LabelExtrema> ExtremaMapType;
82  typedef typename ExtremaMapType::iterator ExtremaMapTypeIterator;
83  typedef typename ExtremaMapType::const_iterator ExtremaMapTypeConstIterator;
84  typedef typename ExtremaMapType::value_type MapValueType;
85 
92  {
93  ExtremaMapTypeConstIterator it = m_LabelExtrema.find(label);
94  if (it == m_LabelExtrema.end())
95  {
96  MITK_ERROR << "invalid label";
97  }
98 
99  return (*it).second.m_Min;
100  }
101 
108  {
109  ExtremaMapTypeConstIterator it = m_LabelExtrema.find(label);
110  if (it == m_LabelExtrema.end())
111  {
112  MITK_ERROR << "invalid label";
113  }
114 
115  return (*it).second.m_Max;
116  }
117 
122  std::vector<LabelPixelType> GetRelevantLabels() const
123  {
124  std::vector<LabelPixelType> labels;
125  for (auto&& it:m_LabelExtrema)
126  {
127  labels.push_back(it.first);
128  }
129  return labels;
130  }
131 
138  {
139  ExtremaMapTypeConstIterator it = m_LabelExtrema.find(label);
140  if (it == m_LabelExtrema.end())
141  {
142  MITK_ERROR << "invalid label";
143  }
144 
145  return (*it).second.m_MinIndex;
146 
147  }
148 
155  {
156  ExtremaMapTypeConstIterator it = m_LabelExtrema.find(label);
157  if (it == m_LabelExtrema.end())
158  {
159  MITK_ERROR << "invalid label";
160  }
161 
162  return (*it).second.m_MaxIndex;
163 
164  }
165 
171  {
172  return m_GlobalMin;
173  }
174 
180  {
181  return m_GlobalMax;
182  }
183 
189  {
190  return m_GlobalMinIndex;
191  }
192 
198  {
199  return m_GlobalMaxIndex;
200  }
201 
206  void SetLabelInput(const TLabelImage *input)
207  {
208  // Process object is not const-correct so the const casting is required.
209  this->SetNthInput( 1, const_cast< TLabelImage * >( input ) );
210  }
211 
216  const TLabelImage * GetLabelInput() const
217  {
218  return itkDynamicCastInDebugMode< TLabelImage * >( const_cast< DataObject * >( this->ProcessObject::GetInput(1) ) );
219  }
220 
221 protected:
223  {
224  this->DynamicMultiThreadingOff();
225  }
226 
227  void AllocateOutputs() override;
228 
230  outputRegionForThread,
231  ThreadIdType threadId) override;
232 
233  void BeforeThreadedGenerateData() override;
234 
235  void AfterThreadedGenerateData() override;
236 
237 private:
238  std::vector<ExtremaMapType> m_ThreadExtrema;
239 
240  ExtremaMapType m_LabelExtrema;
241  PixelType m_GlobalMin;
242  PixelType m_GlobalMax;
243  IndexType m_GlobalMinIndex, m_GlobalMaxIndex;
244 };
245 }
246 
247 #include <mitkMinMaxLabelmageFilterWithIndex.tpp>
248 
249 
250 #endif
Container holding minimum/maximum values and their indices for a single label.
Computes per-label and global minimum/maximum pixel values with their indices.
ExtremaMapType::const_iterator ExtremaMapTypeConstIterator
IndexType GetGlobalMinIndex() const
Get the index of the global minimum pixel value.
PixelType GetMax(LabelPixelType label) const
Get the maximum pixel value for the given label.
std::vector< LabelPixelType > GetRelevantLabels() const
Get all labels for which extrema have been computed.
std::unordered_map< LabelPixelType, LabelExtrema > ExtremaMapType
IndexType GetMinIndex(LabelPixelType label) const
Get the index of the minimum pixel value for the given label.
PixelType GetGlobalMin() const
Get the global minimum pixel value across all labels.
const TLabelImage * GetLabelInput() const
Get the label image.
PixelType GetGlobalMax() const
Get the global maximum pixel value across all labels.
IndexType GetGlobalMaxIndex() const
Get the index of the global maximum pixel value.
PixelType GetMin(LabelPixelType label) const
Get the minimum pixel value for the given label.
ImageToImageFilter< TInputImage, TInputImage > Superclass
NumericTraits< PixelType >::RealType RealType
IndexType GetMaxIndex(LabelPixelType label) const
Get the index of the maximum pixel value for the given label.
void SetLabelInput(const TLabelImage *input)
Set the label image defining label regions.
void ThreadedGenerateData(const RegionType &outputRegionForThread, ThreadIdType threadId) override
#define MITK_ERROR
Log an error message.
Definition: mitkLog.h:372