]> Creatis software - clitk.git/blob - itk/itkLabelOverlapMeasuresImageFilter.h
First version to convert image to dicomrtstruct
[clitk.git] / itk / itkLabelOverlapMeasuresImageFilter.h
1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkLabelOverlapMeasuresImageFilter.h,v $
5   Language:  C++
6   Date:      $Date: $
7   Version:   $Revision: $
8
9   Copyright (c) Insight Software Consortium. All rights reserved.
10   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11
12      This software is distributed WITHOUT ANY WARRANTY; without even
13      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14      PURPOSE.  See the above copyright notices for more information.
15
16 =========================================================================*/
17 #ifndef __itkLabelOverlapMeasuresImageFilter_h
18 #define __itkLabelOverlapMeasuresImageFilter_h
19
20 #include "itkImageToImageFilter.h"
21 #include "itkFastMutexLock.h"
22 #include "itkNumericTraits.h"
23
24 //#include "itk_hash_map.h"
25 #include "itksys/hash_map.hxx"
26
27 namespace itk {
28
29 /** \class LabelOverlapMeasuresImageFilter
30  * \brief Computes overlap measures between the set same set of labels of
31  * pixels of two images.  Background is assumed to be 0.
32  *
33  * \sa LabelOverlapMeasuresImageFilter
34  *
35  * \ingroup MultiThreaded
36  */
37 template<class TLabelImage>
38 class ITK_EXPORT LabelOverlapMeasuresImageFilter :
39     public ImageToImageFilter<TLabelImage, TLabelImage>
40 {
41 public:
42   /** Standard Self typedef */
43   typedef LabelOverlapMeasuresImageFilter                Self;
44   typedef ImageToImageFilter<TLabelImage,TLabelImage>    Superclass;
45   typedef SmartPointer<Self>                             Pointer;
46   typedef SmartPointer<const Self>                       ConstPointer;
47
48   /** Method for creation through the object factory. */
49   itkNewMacro( Self );
50
51   /** Runtime information support. */
52   itkTypeMacro( LabelOverlapMeasuresImageFilter, ImageToImageFilter );
53
54     virtual void VerifyInputInformation() { }
55
56
57   /** Image related typedefs. */
58   typedef TLabelImage                                   LabelImageType;
59   typedef typename TLabelImage::Pointer                 LabelImagePointer;
60   typedef typename TLabelImage::ConstPointer            LabelImageConstPointer;
61
62   typedef typename TLabelImage::RegionType              RegionType;
63   typedef typename TLabelImage::SizeType                SizeType;
64   typedef typename TLabelImage::IndexType               IndexType;
65
66   typedef typename TLabelImage::PixelType               LabelType;
67
68   /** Type to use form computations. */
69   typedef typename NumericTraits<LabelType>::RealType RealType;
70
71   /** \class LabelLabelOverlapMeasuress
72    * \brief Metrics stored per label */
73   class LabelSetMeasures
74     {
75     public:
76     // default constructor
77     LabelSetMeasures()
78       {
79       m_Source = 0;
80       m_Target = 0;
81       m_Union = 0;
82       m_Intersection = 0;
83       m_SourceComplement = 0;
84       m_TargetComplement = 0;
85       }
86
87   // added for completeness
88     LabelSetMeasures& operator=( const LabelSetMeasures& l )
89       {
90       m_Source = l.m_Source;
91       m_Target = l.m_Target;
92       m_Union = l.m_Union;
93       m_Intersection = l.m_Intersection;
94       m_SourceComplement = l.m_SourceComplement;
95       m_TargetComplement = l.m_TargetComplement;
96       }
97
98     unsigned long m_Source;
99     unsigned long m_Target;
100     unsigned long m_Union;
101     unsigned long m_Intersection;
102     unsigned long m_SourceComplement;
103     unsigned long m_TargetComplement;
104     };
105
106   /** Type of the map used to store data per label */
107   typedef itksys::hash_map<LabelType, LabelSetMeasures> MapType;
108   typedef typename MapType::iterator MapIterator;
109   typedef typename MapType::const_iterator MapConstIterator;
110
111   /** Image related typedefs. */
112   itkStaticConstMacro( ImageDimension, unsigned int,
113     TLabelImage::ImageDimension );
114
115   /** Set the source image. */
116   void SetSourceImage( const LabelImageType * image )
117     { this->SetNthInput( 0, const_cast<LabelImageType *>( image ) ); }
118
119   /** Set the target image. */
120   void SetTargetImage( const LabelImageType * image )
121     { this->SetNthInput( 1, const_cast<LabelImageType *>( image ) ); }
122
123   /** Get the source image. */
124   const LabelImageType * GetSourceImage( void )
125     { return this->GetInput( 0 ); }
126
127   /** Get the target image. */
128   const LabelImageType * GetTargetImage( void )
129     { return this->GetInput( 1 ); }
130
131   /** Get the label set measures */
132   MapType GetLabelSetMeasures()
133     { return this->m_LabelSetMeasures; }
134
135   /**
136    * tric overlap measures
137    */
138   /** measures over all labels */
139   RealType GetTotalOverlap();
140   RealType GetUnionOverlap();
141   RealType GetMeanOverlap();
142   RealType GetVolumeSimilarity();
143   RealType GetFalseNegativeError();
144   RealType GetFalsePositiveError();
145   /** measures over individual labels */
146   RealType GetTargetOverlap( LabelType );
147   RealType GetUnionOverlap( LabelType );
148   RealType GetMeanOverlap( LabelType );
149   RealType GetVolumeSimilarity( LabelType );
150   RealType GetFalseNegativeError( LabelType );
151   RealType GetFalsePositiveError( LabelType );
152   /** alternative names */
153   RealType GetJaccardCoefficient()
154     { return this->GetUnionOverlap(); }
155   RealType GetJaccardCoefficient( LabelType label )
156     { return this->GetUnionOverlap( label ); }
157   RealType GetDiceCoefficient()
158     { return this->GetMeanOverlap(); }
159   RealType GetDiceCoefficient( LabelType label )
160     { return this->GetMeanOverlap( label ); }
161
162
163 #ifdef ITK_USE_CONCEPT_CHECKING
164   /** Begin concept checking */
165   itkConceptMacro( Input1HasNumericTraitsCheck,
166     ( Concept::HasNumericTraits<LabelType> ) );
167   /** End concept checking */
168 #endif
169
170 protected:
171   LabelOverlapMeasuresImageFilter();
172   ~LabelOverlapMeasuresImageFilter(){};
173   void PrintSelf( std::ostream& os, Indent indent ) const;
174
175   /**
176    * Pass the input through unmodified. Do this by setting the output to the
177    * source this by setting the output to the source image in the
178    * AllocateOutputs() method.
179    */
180   void AllocateOutputs();
181
182   void BeforeThreadedGenerateData();
183
184   void AfterThreadedGenerateData();
185
186   /** Multi-thread version GenerateData. */
187   void ThreadedGenerateData( const RegionType&, int );
188
189   // Override since the filter needs all the data for the algorithm
190   void GenerateInputRequestedRegion();
191
192   // Override since the filter produces all of its output
193   void EnlargeOutputRequestedRegion( DataObject *data );
194
195 private:
196   LabelOverlapMeasuresImageFilter( const Self& ); //purposely not implemented
197   void operator=( const Self& ); //purposely not implemented
198
199   std::vector<MapType>                            m_LabelSetMeasuresPerThread;
200   MapType                                         m_LabelSetMeasures;
201
202   SimpleFastMutexLock                             m_Mutex;
203
204 }; // end of class
205
206 } // end namespace itk
207
208 #ifndef ITK_MANUAL_INSTANTIATION
209 #include "itkLabelOverlapMeasuresImageFilter.txx"
210 #endif
211
212 #endif