00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef __itkLabelStatisticsImageFilter_h
00018 #define __itkLabelStatisticsImageFilter_h
00019
00020 #include "itkImageToImageFilter.h"
00021 #include "itkNumericTraits.h"
00022 #include "itkArray.h"
00023 #include "itkSimpleDataObjectDecorator.h"
00024 #include "itk_hash_map.h"
00025 #include "itkHistogram.h"
00026 #include <vector>
00027
00028 namespace itk {
00029
00053 template<class TInputImage, class TLabelImage>
00054 class ITK_EXPORT LabelStatisticsImageFilter :
00055 public ImageToImageFilter<TInputImage, TInputImage>
00056 {
00057 public:
00059 typedef LabelStatisticsImageFilter Self;
00060 typedef ImageToImageFilter<TInputImage,TInputImage> Superclass;
00061 typedef SmartPointer<Self> Pointer;
00062 typedef SmartPointer<const Self> ConstPointer;
00063
00065 itkNewMacro(Self);
00066
00068 itkTypeMacro(LabelStatisticsImageFilter, ImageToImageFilter);
00069
00071 typedef typename TInputImage::Pointer InputImagePointer;
00072 typedef typename TInputImage::RegionType RegionType ;
00073 typedef typename TInputImage::SizeType SizeType ;
00074 typedef typename TInputImage::IndexType IndexType ;
00075 typedef typename TInputImage::PixelType PixelType ;
00076
00078 typedef TLabelImage LabelImageType;
00079 typedef typename TLabelImage::Pointer LabelImagePointer;
00080 typedef typename TLabelImage::RegionType LabelRegionType ;
00081 typedef typename TLabelImage::SizeType LabelSizeType ;
00082 typedef typename TLabelImage::IndexType LabelIndexType ;
00083 typedef typename TLabelImage::PixelType LabelPixelType ;
00084
00086 itkStaticConstMacro(ImageDimension, unsigned int,
00087 TInputImage::ImageDimension ) ;
00088
00090 typedef typename NumericTraits<PixelType>::RealType RealType;
00091
00093 typedef typename DataObject::Pointer DataObjectPointer;
00094
00096 typedef SimpleDataObjectDecorator<RealType> RealObjectType;
00097
00099 typedef std::vector<typename IndexType::IndexValueType> BoundingBoxType;
00100
00102 typedef itk::Statistics::Histogram<RealType,1> HistogramType;
00103 typedef typename HistogramType::Pointer HistogramPointer;
00104
00106 class LabelStatistics
00107 {
00108 public:
00109
00110
00111 LabelStatistics()
00112 {
00113
00114 m_Count = 0;
00115 m_Sum = NumericTraits<RealType>::Zero;
00116 m_SumOfSquares = NumericTraits<RealType>::Zero;
00117
00118
00119 m_Minimum = NumericTraits<RealType>::max();
00120 m_Maximum = NumericTraits<RealType>::NonpositiveMin();
00121
00122
00123 m_Mean = NumericTraits<RealType>::Zero;
00124 m_Sigma = NumericTraits<RealType>::Zero;
00125 m_Variance = NumericTraits<RealType>::Zero;
00126
00127 unsigned int imageDimension = itkGetStaticConstMacro(ImageDimension);
00128 m_BoundingBox.resize(imageDimension*2);
00129 for (unsigned int i = 0; i < imageDimension * 2; i += 2)
00130 {
00131 m_BoundingBox[i] = NumericTraits<ITK_TYPENAME IndexType::IndexValueType>::max();
00132 m_BoundingBox[i+1] = NumericTraits<ITK_TYPENAME IndexType::IndexValueType>::NonpositiveMin();
00133 }
00134 m_Histogram = 0;
00135
00136 }
00137
00138
00139 LabelStatistics(int size, RealType lowerBound, RealType upperBound)
00140 {
00141
00142
00143 m_Count = 0;
00144 m_Sum = NumericTraits<RealType>::Zero;
00145 m_SumOfSquares = NumericTraits<RealType>::Zero;
00146
00147
00148 m_Minimum = NumericTraits<RealType>::max();
00149 m_Maximum = NumericTraits<RealType>::NonpositiveMin();
00150
00151
00152 m_Mean = NumericTraits<RealType>::Zero;
00153 m_Sigma = NumericTraits<RealType>::Zero;
00154 m_Variance = NumericTraits<RealType>::Zero;
00155
00156 unsigned int imageDimension = itkGetStaticConstMacro(ImageDimension);
00157 m_BoundingBox.resize(imageDimension*2);
00158 for (unsigned int i = 0; i < imageDimension * 2; i += 2)
00159 {
00160 m_BoundingBox[i] = NumericTraits<ITK_TYPENAME IndexType::IndexValueType>::max();
00161 m_BoundingBox[i+1] = NumericTraits<ITK_TYPENAME IndexType::IndexValueType>::NonpositiveMin();
00162 }
00163
00164
00165 m_Histogram = HistogramType::New();
00166 typename HistogramType::SizeType hsize;
00167 hsize[0] = size;
00168 typename HistogramType::MeasurementVectorType lb;
00169 lb[0] = lowerBound;
00170 typename HistogramType::MeasurementVectorType ub;
00171 ub[0] = upperBound;
00172 m_Histogram->Initialize(hsize, lb, ub);
00173 }
00174
00175
00176 LabelStatistics(const LabelStatistics& l)
00177 {
00178 m_Count = l.m_Count;
00179 m_Minimum = l.m_Minimum;
00180 m_Maximum = l.m_Maximum;
00181 m_Mean = l.m_Mean;
00182 m_Sum = l.m_Sum;
00183 m_SumOfSquares = l.m_SumOfSquares;
00184 m_Sigma = l.m_Sigma;
00185 m_Variance = l.m_Variance;
00186 m_BoundingBox = l.m_BoundingBox;
00187 m_Histogram = l.m_Histogram;
00188 }
00189
00190
00191 LabelStatistics& operator= (const LabelStatistics& l)
00192 {
00193 m_Count = l.m_Count;
00194 m_Minimum = l.m_Minimum;
00195 m_Maximum = l.m_Maximum;
00196 m_Mean = l.m_Mean;
00197 m_Sum = l.m_Sum;
00198 m_SumOfSquares = l.m_SumOfSquares;
00199 m_Sigma = l.m_Sigma;
00200 m_Variance = l.m_Variance;
00201 m_BoundingBox = l.m_BoundingBox;
00202 m_Histogram = l.m_Histogram;
00203 }
00204
00205 unsigned long m_Count;
00206 RealType m_Minimum;
00207 RealType m_Maximum;
00208 RealType m_Mean;
00209 RealType m_Sum;
00210 RealType m_SumOfSquares;
00211 RealType m_Sigma;
00212 RealType m_Variance;
00213 BoundingBoxType m_BoundingBox;
00214 typename HistogramType::Pointer m_Histogram;
00215 };
00216
00218 typedef itk::hash_map<LabelPixelType, LabelStatistics> MapType;
00219 typedef typename itk::hash_map<LabelPixelType, LabelStatistics>::iterator MapIterator;
00220 typedef typename itk::hash_map<LabelPixelType, LabelStatistics>::const_iterator MapConstIterator;
00221
00222
00223 itkSetMacro(UseHistograms, bool);
00224 itkGetMacro(UseHistograms, bool);
00225 itkBooleanMacro(UseHistograms);
00226
00228 void SetLabelInput(TLabelImage *input)
00229 {
00230
00231 this->SetNthInput(1, const_cast<TLabelImage *>(input) );
00232 }
00233
00235 LabelImageType * GetLabelInput()
00236 {
00237 return static_cast<LabelImageType*>(const_cast<DataObject *>(this->ProcessObject::GetInput(1)));
00238 }
00239
00242 bool HasLabel(LabelPixelType label) const
00243 {
00244 return m_LabelStatistics.find(label) != m_LabelStatistics.end();
00245 }
00246
00248 unsigned long GetNumberOfObjects() const
00249 {
00250 return m_LabelStatistics.size();
00251 }
00252 unsigned long GetNumberOfLabels() const
00253 {
00254 return this->GetNumberOfObjects();
00255 }
00256
00257
00259 RealType GetMinimum(LabelPixelType label) const;
00260
00262 RealType GetMaximum(LabelPixelType label) const;
00263
00265 RealType GetMean(LabelPixelType label) const;
00266
00268 RealType GetMedian(LabelPixelType label) const;
00269
00271 RealType GetSigma(LabelPixelType label) const;
00272
00274 RealType GetVariance(LabelPixelType label) const;
00275
00277 BoundingBoxType GetBoundingBox(LabelPixelType label) const;
00278
00280 RegionType GetRegion(LabelPixelType label) const;
00281
00283 RealType GetSum(LabelPixelType label) const;
00284
00286 unsigned long GetCount(LabelPixelType label) const;
00287
00289 HistogramPointer GetHistogram(LabelPixelType label) const;
00290
00292 void SetHistogramParameters(const int numBins, RealType lowerBound,
00293 RealType upperBound) ;
00294
00295 protected:
00296 LabelStatisticsImageFilter();
00297 ~LabelStatisticsImageFilter(){};
00298 void PrintSelf(std::ostream& os, Indent indent) const;
00299
00301 void AllocateOutputs();
00302
00304 void BeforeThreadedGenerateData ();
00305
00307 void AfterThreadedGenerateData ();
00308
00310 void ThreadedGenerateData (const RegionType&
00311 outputRegionForThread,
00312 int threadId) ;
00313
00314
00315 void GenerateInputRequestedRegion();
00316
00317
00318 void EnlargeOutputRequestedRegion(DataObject *data);
00319
00320
00321 private:
00322 LabelStatisticsImageFilter(const Self&);
00323 void operator=(const Self&);
00324
00325 std::vector<MapType> m_LabelStatisticsPerThread;
00326 MapType m_LabelStatistics;
00327
00328 bool m_UseHistograms;
00329 typename HistogramType::SizeType m_NumBins;
00330 RealType m_LowerBound;
00331 RealType m_UpperBound;
00332
00333 } ;
00334
00335 }
00336
00337 #ifndef ITK_MANUAL_INSTANTIATION
00338 #include "itkLabelStatisticsImageFilter.txx"
00339 #endif
00340
00341 #endif