ITK  4.9.0
Insight Segmentation and Registration Toolkit
itkLabelStatisticsImageFilter.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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 itkLabelStatisticsImageFilter_h
19 #define itkLabelStatisticsImageFilter_h
20 
21 #include "itkImageToImageFilter.h"
22 #include "itkNumericTraits.h"
24 #include "itksys/hash_map.hxx"
25 #include "itkHistogram.h"
26 #include "itkFastMutexLock.h"
27 #include <vector>
28 
29 namespace itk
30 {
59 template< typename TInputImage, typename TLabelImage >
61  public ImageToImageFilter< TInputImage, TInputImage >
62 {
63 public:
69 
71  itkNewMacro(Self);
72 
75 
77  typedef typename TInputImage::Pointer InputImagePointer;
78  typedef typename TInputImage::RegionType RegionType;
79  typedef typename TInputImage::SizeType SizeType;
80  typedef typename TInputImage::IndexType IndexType;
81  typedef typename TInputImage::PixelType PixelType;
82 
84  typedef TLabelImage LabelImageType;
85  typedef typename TLabelImage::Pointer LabelImagePointer;
86  typedef typename TLabelImage::RegionType LabelRegionType;
87  typedef typename TLabelImage::SizeType LabelSizeType;
88  typedef typename TLabelImage::IndexType LabelIndexType;
89  typedef typename TLabelImage::PixelType LabelPixelType;
90 
92  itkStaticConstMacro(ImageDimension, unsigned int,
93  TInputImage::ImageDimension);
94 
97 
100 
103 
105  typedef std::vector< IndexValueType > BoundingBoxType;
106 
110 
116  {
117 public:
118 
119  // default constructor
121  {
122  // initialized to the default values
126 
127  // Set such that the first pixel encountered can be compared
130 
131  // Default these to zero
135 
136  unsigned int imageDimension = itkGetStaticConstMacro(ImageDimension);
137  m_BoundingBox.resize(imageDimension * 2);
138  for ( unsigned int i = 0; i < imageDimension * 2; i += 2 )
139  {
142  }
143  m_Histogram = ITK_NULLPTR;
144  }
145 
146  // constructor with histogram enabled
147  LabelStatistics(int size, RealType lowerBound, RealType upperBound)
148  {
149  // initialized to the default values
153 
154  // Set such that the first pixel encountered can be compared
157 
158  // Default these to zero
162 
163  unsigned int imageDimension = itkGetStaticConstMacro(ImageDimension);
164  m_BoundingBox.resize(imageDimension * 2);
165  for ( unsigned int i = 0; i < imageDimension * 2; i += 2 )
166  {
169  }
170 
171  // Histogram
173  typename HistogramType::SizeType hsize;
176  hsize.SetSize(1);
177  lb.SetSize(1);
178  ub.SetSize(1);
179  m_Histogram->SetMeasurementVectorSize(1);
180  hsize[0] = size;
181  lb[0] = lowerBound;
182  ub[0] = upperBound;
183  m_Histogram->Initialize(hsize, lb, ub);
184  }
185 
186  // need copy constructor because of smart pointer to histogram
188  {
189  m_Count = l.m_Count;
190  m_Minimum = l.m_Minimum;
191  m_Maximum = l.m_Maximum;
192  m_Mean = l.m_Mean;
193  m_Sum = l.m_Sum;
195  m_Sigma = l.m_Sigma;
199  }
200 
201  // added for completeness
203  {
204  if(this != &l)
205  {
206  m_Count = l.m_Count;
207  m_Minimum = l.m_Minimum;
208  m_Maximum = l.m_Maximum;
209  m_Mean = l.m_Mean;
210  m_Sum = l.m_Sum;
212  m_Sigma = l.m_Sigma;
216  }
217  return *this;
218  }
219 
230  };
231 
233  typedef itksys::hash_map< LabelPixelType, LabelStatistics > MapType;
234  typedef typename itksys::hash_map< LabelPixelType, LabelStatistics >::iterator MapIterator;
235  typedef typename itksys::hash_map< LabelPixelType, LabelStatistics >::const_iterator MapConstIterator;
237 
239  typedef std::vector<LabelPixelType> ValidLabelValuesContainerType;
240 
241  // macros for Histogram enables
242  itkSetMacro(UseHistograms, bool);
243  itkGetConstMacro(UseHistograms, bool);
244  itkBooleanMacro(UseHistograms);
245 
246 
248  {
249  return m_ValidLabelValues;
250  }
251 
253  void SetLabelInput(const TLabelImage *input)
254  {
255  // Process object is not const-correct so the const casting is required.
256  this->SetNthInput( 1, const_cast< TLabelImage * >( input ) );
257  }
258 
261  {
262  return itkDynamicCastInDebugMode< LabelImageType * >( const_cast< DataObject * >( this->ProcessObject::GetInput(1) ) );
263  }
264 
267  bool HasLabel(LabelPixelType label) const
268  {
269  return m_LabelStatistics.find(label) != m_LabelStatistics.end();
270  }
271 
274  {
275  return m_LabelStatistics.size();
276  }
277 
279  {
280  return this->GetNumberOfObjects();
281  }
282 
284  RealType GetMinimum(LabelPixelType label) const;
285 
287  RealType GetMaximum(LabelPixelType label) const;
288 
290  RealType GetMean(LabelPixelType label) const;
291 
294  RealType GetMedian(LabelPixelType label) const;
295 
297  RealType GetSigma(LabelPixelType label) const;
298 
300  RealType GetVariance(LabelPixelType label) const;
301 
304 
306  RegionType GetRegion(LabelPixelType label) const;
307 
309  RealType GetSum(LabelPixelType label) const;
310 
312  MapSizeType GetCount(LabelPixelType label) const;
313 
316 
318  void SetHistogramParameters(const int numBins, RealType lowerBound,
319  RealType upperBound);
320 
321 #ifdef ITK_USE_CONCEPT_CHECKING
322  // Begin concept checking
323  itkConceptMacro( InputHasNumericTraitsCheck,
325  // End concept checking
326 #endif
327 
328 protected:
331  void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
332 
335  void AllocateOutputs() ITK_OVERRIDE;
336 
338  void BeforeThreadedGenerateData() ITK_OVERRIDE;
339 
342  void AfterThreadedGenerateData() ITK_OVERRIDE;
343 
345  void ThreadedGenerateData(const RegionType &
346  outputRegionForThread,
347  ThreadIdType threadId) ITK_OVERRIDE;
348 
349  // Override since the filter produces all of its output
350  void EnlargeOutputRequestedRegion(DataObject *data) ITK_OVERRIDE;
351 
352 private:
353  LabelStatisticsImageFilter(const Self &) ITK_DELETE_FUNCTION;
354  void operator=(const Self &) ITK_DELETE_FUNCTION;
355 
359 
361 
363 
367 }; // end of class
368 } // end namespace itk
369 
370 #ifndef ITK_MANUAL_INSTANTIATION
371 #include "itkLabelStatisticsImageFilter.hxx"
372 #endif
373 
374 #endif
Critical section locking class that can be allocated on the stack.
itksys::hash_map< LabelPixelType, LabelStatistics >::iterator MapIterator
bool HasLabel(LabelPixelType label) const
RealType GetSigma(LabelPixelType label) const
void BeforeThreadedGenerateData() override
void SetLabelInput(const TLabelImage *input)
Light weight base class for most itk classes.
itksys::hash_map< LabelPixelType, LabelStatistics > MapType
virtual const ValidLabelValuesContainerType & GetValidLabelValues() const
std::vector< IndexValueType > BoundingBoxType
void PrintSelf(std::ostream &os, Indent indent) const override
NumericTraits< PixelType >::RealType RealType
SimpleDataObjectDecorator< RealType > RealObjectType
This class stores measurement vectors in the context of n-dimensional histogram.
Definition: itkHistogram.h:77
Given an intensity image and a label map, compute min, max, variance and mean of the pixels associate...
BoundingBoxType GetBoundingBox(LabelPixelType label) const
void AfterThreadedGenerateData() override
RealType GetSum(LabelPixelType label) const
RealType GetMinimum(LabelPixelType label) const
SizeValueType IdentifierType
Definition: itkIntTypes.h:147
RealType GetMean(LabelPixelType label) const
void SetSize(SizeValueType sz)
Decorates any &quot;simple&quot; data type (data types without smart pointers) with a DataObject API...
Superclass::MeasurementVectorType MeasurementVectorType
Definition: itkHistogram.h:101
itk::Statistics::Histogram< RealType > HistogramType
static T max(const T &)
RealType GetVariance(LabelPixelType label) const
ValidLabelValuesContainerType m_ValidLabelValues
std::vector< LabelPixelType > ValidLabelValuesContainerType
unsigned int ThreadIdType
Definition: itkIntTypes.h:159
DataObject * GetInput(const DataObjectIdentifierType &key)
Return an input.
itksys::hash_map< LabelPixelType, LabelStatistics >::const_iterator MapConstIterator
HistogramPointer GetHistogram(LabelPixelType label) const
void EnlargeOutputRequestedRegion(DataObject *data) override
void SetHistogramParameters(const int numBins, RealType lowerBound, RealType upperBound)
LabelStatistics & operator=(const LabelStatistics &l)
RegionType GetRegion(LabelPixelType label) const
Base class for filters that take an image as input and produce an image as output.
void ThreadedGenerateData(const RegionType &outputRegionForThread, ThreadIdType threadId) override
LabelStatistics(int size, RealType lowerBound, RealType upperBound)
Control indentation during Print() invocation.
Definition: itkIndent.h:49
const LabelImageType * GetLabelInput() const
Define additional traits for native types such as int or float.
virtual void SetNthInput(DataObjectPointerArraySizeType num, DataObject *input)
#define itkConceptMacro(name, concept)
RealType GetMaximum(LabelPixelType label) const
MapSizeType GetCount(LabelPixelType label) const
Base class for all data objects in ITK.
ImageToImageFilter< TInputImage, TInputImage > Superclass
RealType GetMedian(LabelPixelType label) const