1 /*=========================================================================
3 Program: Insight Segmentation & Registration Toolkit
4 Module: $RCSfile: itkLabelOverlapMeasuresImageFilter.h,v $
9 Copyright (c) Insight Software Consortium. All rights reserved.
10 See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
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.
16 =========================================================================*/
17 #ifndef __itkLabelOverlapMeasuresImageFilter_h
18 #define __itkLabelOverlapMeasuresImageFilter_h
20 #include "itkImageToImageFilter.h"
21 #include "itkFastMutexLock.h"
22 #include "itkNumericTraits.h"
24 //#include "itk_hash_map.h"
25 #include "itksys/hash_map.hxx"
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.
33 * \sa LabelOverlapMeasuresImageFilter
35 * \ingroup MultiThreaded
37 template<class TLabelImage>
38 class ITK_EXPORT LabelOverlapMeasuresImageFilter :
39 public ImageToImageFilter<TLabelImage, TLabelImage>
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;
48 /** Method for creation through the object factory. */
51 /** Runtime information support. */
52 itkTypeMacro( LabelOverlapMeasuresImageFilter, ImageToImageFilter );
54 virtual void VerifyInputInformation() { }
57 /** Image related typedefs. */
58 typedef TLabelImage LabelImageType;
59 typedef typename TLabelImage::Pointer LabelImagePointer;
60 typedef typename TLabelImage::ConstPointer LabelImageConstPointer;
62 typedef typename TLabelImage::RegionType RegionType;
63 typedef typename TLabelImage::SizeType SizeType;
64 typedef typename TLabelImage::IndexType IndexType;
66 typedef typename TLabelImage::PixelType LabelType;
68 /** Type to use form computations. */
69 typedef typename NumericTraits<LabelType>::RealType RealType;
71 /** \class LabelLabelOverlapMeasuress
72 * \brief Metrics stored per label */
73 class LabelSetMeasures
76 // default constructor
83 m_SourceComplement = 0;
84 m_TargetComplement = 0;
87 // added for completeness
88 LabelSetMeasures& operator=( const LabelSetMeasures& l )
90 m_Source = l.m_Source;
91 m_Target = l.m_Target;
93 m_Intersection = l.m_Intersection;
94 m_SourceComplement = l.m_SourceComplement;
95 m_TargetComplement = l.m_TargetComplement;
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;
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;
111 /** Image related typedefs. */
112 itkStaticConstMacro( ImageDimension, unsigned int,
113 TLabelImage::ImageDimension );
115 /** Set the source image. */
116 void SetSourceImage( const LabelImageType * image )
117 { this->SetNthInput( 0, const_cast<LabelImageType *>( image ) ); }
119 /** Set the target image. */
120 void SetTargetImage( const LabelImageType * image )
121 { this->SetNthInput( 1, const_cast<LabelImageType *>( image ) ); }
123 /** Get the source image. */
124 const LabelImageType * GetSourceImage( void )
125 { return this->GetInput( 0 ); }
127 /** Get the target image. */
128 const LabelImageType * GetTargetImage( void )
129 { return this->GetInput( 1 ); }
131 /** Get the label set measures */
132 MapType GetLabelSetMeasures()
133 { return this->m_LabelSetMeasures; }
136 * tric overlap measures
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 ); }
163 #ifdef ITK_USE_CONCEPT_CHECKING
164 /** Begin concept checking */
165 itkConceptMacro( Input1HasNumericTraitsCheck,
166 ( Concept::HasNumericTraits<LabelType> ) );
167 /** End concept checking */
171 LabelOverlapMeasuresImageFilter();
172 ~LabelOverlapMeasuresImageFilter(){};
173 void PrintSelf( std::ostream& os, Indent indent ) const;
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.
180 void AllocateOutputs();
182 void BeforeThreadedGenerateData();
184 void AfterThreadedGenerateData();
186 /** Multi-thread version GenerateData. */
187 void ThreadedGenerateData( const RegionType&, int );
189 // Override since the filter needs all the data for the algorithm
190 void GenerateInputRequestedRegion();
192 // Override since the filter produces all of its output
193 void EnlargeOutputRequestedRegion( DataObject *data );
196 LabelOverlapMeasuresImageFilter( const Self& ); //purposely not implemented
197 void operator=( const Self& ); //purposely not implemented
199 std::vector<MapType> m_LabelSetMeasuresPerThread;
200 MapType m_LabelSetMeasures;
202 SimpleFastMutexLock m_Mutex;
206 } // end namespace itk
208 #ifndef ITK_MANUAL_INSTANTIATION
209 #include "itkLabelOverlapMeasuresImageFilter.txx"