]> Creatis software - FrontAlgorithms.git/blob - appli/CTBronchi/MoriLabelling.h
...
[FrontAlgorithms.git] / appli / CTBronchi / MoriLabelling.h
1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
5 #ifndef __CTBronchi__MoriLabelling__h__
6 #define __CTBronchi__MoriLabelling__h__
7
8 #include <itkLabelStatisticsImageFilter.h>
9 #include <fpa/Filters/BaseMarksInterface.h>
10 #include <fpa/Filters/Image/SeedsFromLabelsInterface.h>
11 #include <fpa/Filters/Image/DefaultTraits.h>
12 #include <fpa/Filters/Image/RegionGrow.h>
13 #include <fpa/Functors/RegionGrow/BinaryThreshold.h>
14
15 namespace CTBronchi
16 {
17   /**
18    */
19   template< class _TImage, class _TLabels >
20   class MoriLabellingTraits
21     : public fpa::Filters::Image::DefaultTraits< _TImage, _TLabels, typename _TLabels::PixelType >
22   {
23   public:
24     typedef fpa::Filters::Image::DefaultTraits< _TImage, _TLabels, typename _TLabels::PixelType > Superclass;
25     typedef typename Superclass::TInternalTraits  TInternalTraits;
26     typedef typename Superclass::TMarksImage      TMarksImage;
27     typedef typename Superclass::TFilterInterface TFilterInterface;
28
29     typedef fpa::Filters::BaseMarksInterface< TInternalTraits >  TMarksInterface;
30     typedef fpa::Filters::Image::SeedsFromLabelsInterface< TInternalTraits > TSeedsInterface;
31   };
32
33   /**
34    */
35   template< class _TImage, class _TLabels, class _TScalarImage >
36   class MoriLabelling
37     : public fpa::Filters::Image::RegionGrow< _TImage, _TLabels, typename _TLabels::PixelType, CTBronchi::MoriLabellingTraits< _TImage, _TLabels > >
38   {
39   public:
40     typedef CTBronchi::MoriLabellingTraits< _TImage, _TLabels > TTraits;
41     fpaTraitsMacro( typename TTraits );
42
43     typedef fpa::Filters::Image::RegionGrow< _TImage, _TLabels, TMark, TTraits > Superclass;
44     typedef MoriLabelling Self;
45     typedef itk::SmartPointer< Self > Pointer;
46     typedef itk::SmartPointer< const Self > ConstPointer;
47
48     typedef fpa::Functors::RegionGrow::BinaryThreshold< TInputValue > TFunctor;
49
50   public:
51     itkNewMacro( Self );
52     itkTypeMacro( MoriLabelling, fpa::Filters::Image::RegionGrow );
53
54     itkGetConstMacro( VesselnessThreshold, double );
55     itkSetMacro( VesselnessThreshold, double );
56
57     itkGetConstMacro( UpperThreshold, TInputValue );
58     itkSetMacro( UpperThreshold, TInputValue );
59
60     ivqITKInputMacro( InputLabels, _TLabels );
61     ivqITKInputMacro( InputVesselness, _TScalarImage );
62
63   protected:
64     MoriLabelling( )
65       : Superclass( ),
66         m_VesselnessThreshold( 5 ),
67         m_UpperThreshold( -650 )
68       {
69         ivqITKInputConfigureMacro( InputLabels, _TLabels );
70         ivqITKInputConfigureMacro( InputVesselness, _TScalarImage );
71         this->m_Functor = TFunctor::New( );
72         this->SetPredicate( this->m_Functor );
73       }
74
75     virtual ~MoriLabelling( )
76       {
77       }
78
79     virtual const itk::DataObject* _GetReferenceInput( ) const override
80       {
81         return( this->GetInputLabels( ) );
82       }
83     virtual void _BeforeGenerateData( ) override
84       {
85         this->Superclass::_BeforeGenerateData( );
86
87         this->m_Functor->SetUpperThreshold( this->m_UpperThreshold );
88
89         typedef itk::LabelStatisticsImageFilter< _TScalarImage, _TLabels > _TStats;
90         typename _TStats::Pointer stats = _TStats::New( );
91         stats->SetInput( this->GetInputVesselness( ) );
92         stats->SetLabelInput( this->GetInputLabels( ) );
93         stats->Update( );
94
95         typedef typename _TStats::ValidLabelValuesContainerType _TValidLbl;
96         typedef typename _TStats::LabelPixelType                _TLabelPx;
97         unsigned long minCount = std::numeric_limits< unsigned long >::max( );
98         double vAvg = 0, vStd = 0;
99         typename _TValidLbl::const_iterator vIt = stats->GetValidLabelValues( ).begin( );
100         for( ; vIt != stats->GetValidLabelValues( ).end( ); ++vIt )
101         {
102           if( stats->HasLabel( *vIt ) )
103           {
104             unsigned long c = stats->GetCount( *vIt );
105             if( c < minCount )
106             {
107               minCount = c;
108               vAvg = stats->GetMean( *vIt );
109               vStd = stats->GetSigma( *vIt );
110
111             } // fi
112  
113           } // fi
114
115         } // rof
116         this->m_MinVesselness = vAvg + ( vStd * this->m_VesselnessThreshold );
117       }
118
119     virtual void _PostComputeOutputValue( TNode& n ) override
120       {
121         this->Superclass::_PostComputeOutputValue( n );
122         if( n.Value == this->GetInsideValue( ) )
123         {
124           const _TLabels* labels = this->GetInputLabels( );
125           const _TScalarImage* vesselness = this->GetInputVesselness( );
126           if( labels->GetPixel( n.Vertex ) == 0 )
127           {
128             if( this->m_MinVesselness < vesselness->GetPixel( n.Vertex ) )
129               n.Value = this->GetInsideValue( );
130             else
131               n.Value = 0;
132
133           } // fi
134
135         } // fi
136       }
137
138   private:
139     // Purposely not implemented.
140     MoriLabelling( const Self& other );
141     Self& operator=( const Self& other );
142
143   protected:
144     typename TFunctor::Pointer m_Functor;
145     double m_VesselnessThreshold;
146     double m_MinVesselness;
147     TInputValue m_UpperThreshold;
148   };
149
150 } // ecapseman
151
152 #endif // __CTBronchi__Functions__h__
153
154 // eof - $RCSfile$