]> Creatis software - clitk.git/blob - segmentation/clitkConnectedComponentLabelingGenericFilter.txx
some segmentation tools (most from jef)
[clitk.git] / segmentation / clitkConnectedComponentLabelingGenericFilter.txx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to: 
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://oncora1.lyon.fnclcc.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17   ======================================================================-====*/
18
19 #ifndef CLITKCONNECTEDCOMPONENTLABELINGSGENERICFILTER_TXX
20 #define CLITKCONNECTEDCOMPONENTLABELINGSGENERICFILTER_TXX
21
22 // clitk
23 #include "clitkImageCommon.h"
24
25 // itk
26 #include "itkConnectedComponentImageFilter.h"
27 #include "itkRelabelComponentImageFilter.h"
28   
29 //--------------------------------------------------------------------
30 template<class ArgsInfoType>
31 clitk::ConnectedComponentLabelingGenericFilter<ArgsInfoType>::ConnectedComponentLabelingGenericFilter():
32   ImageToImageGenericFilter<Self>("ConnectedComponentLabeling") 
33 {
34   //  InitializeImageType<2>();
35   InitializeImageType<3>();
36   //InitializeImageType<4>();
37 }
38 //--------------------------------------------------------------------
39
40
41 //--------------------------------------------------------------------
42 template<class ArgsInfoType>
43 template<unsigned int Dim>
44 void clitk::ConnectedComponentLabelingGenericFilter<ArgsInfoType>::InitializeImageType() 
45 {  
46   ADD_IMAGE_TYPE(Dim, uchar);
47   ADD_IMAGE_TYPE(Dim, short);
48   // ADD_IMAGE_TYPE(Dim, int);
49   // ADD_IMAGE_TYPE(Dim, float);
50 }
51 //--------------------------------------------------------------------
52   
53
54 //--------------------------------------------------------------------
55 template<class ArgsInfoType>
56 void clitk::ConnectedComponentLabelingGenericFilter<ArgsInfoType>::SetArgsInfo(const ArgsInfoType & a) 
57 {
58   mArgsInfo=a;
59   SetIOVerbose(mArgsInfo.verbose_flag);
60   if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
61   if (mArgsInfo.input_given)   AddInputFilename(mArgsInfo.input_arg);
62   if (mArgsInfo.output_given)  SetOutputFilename(mArgsInfo.output_arg);
63 }
64 //--------------------------------------------------------------------
65
66
67 //--------------------------------------------------------------------
68 // Update with the number of dimensions and the pixeltype
69 //--------------------------------------------------------------------
70 template<class ArgsInfoType>
71 template<class ImageType>
72 void clitk::ConnectedComponentLabelingGenericFilter<ArgsInfoType>::UpdateWithInputImageType() 
73
74   DD("UpdateWithInputImageType");
75
76   // Reading input
77   typename ImageType::Pointer input = this->template GetInput<ImageType>(0);
78
79   // Output image type
80   typedef itk::Image<int, ImageType::ImageDimension> OutputImageType;
81
82   // Create CCL filter
83   DD("CCL");
84   typedef itk::ConnectedComponentImageFilter<ImageType, OutputImageType> ConnectFilterType;
85   typename ConnectFilterType::Pointer connectFilter = ConnectFilterType::New();
86   connectFilter->SetInput(input);
87   connectFilter->SetBackgroundValue(mArgsInfo.inputBG_arg);
88   connectFilter->SetFullyConnected(mArgsInfo.full_flag);
89
90   // TODO SetBackgroud to zero forr relabel ?
91
92
93   // Sort by size and remove too small area.
94   typedef itk::RelabelComponentImageFilter<OutputImageType, OutputImageType> RelabelFilterType;
95   typename RelabelFilterType::Pointer relabelFilter = RelabelFilterType::New();
96   //  relabelFilter->InPlaceOn();
97   relabelFilter->SetInput(connectFilter->GetOutput());
98   relabelFilter->SetMinimumObjectSize(mArgsInfo.minSize_arg);
99   relabelFilter->Update();
100
101   DD(mArgsInfo.inputBG_arg);
102   DD(mArgsInfo.full_flag);
103   DD(mArgsInfo.minSize_arg);
104   
105   // Set information
106   const std::vector<typename RelabelFilterType::ObjectSizeType> & a = relabelFilter->GetSizeOfObjectsInPixels();
107   m_SizeOfObjectsInPixels.resize(a.size());
108   for(unsigned int i=0; i<a.size(); i++) m_SizeOfObjectsInPixels[i] = a[i];
109   m_SizeOfObjectsInPhysicalUnits = relabelFilter->GetSizeOfObjectsInPhysicalUnits();
110   m_OriginalNumberOfObjects = relabelFilter->GetOriginalNumberOfObjects();
111   DD(m_OriginalNumberOfObjects);
112   DD(m_SizeOfObjectsInPhysicalUnits.size());
113
114   // Write/Save results
115   typename OutputImageType::Pointer output = relabelFilter->GetOutput();
116   this->template SetNextOutput<OutputImageType>(output); 
117 }
118 //--------------------------------------------------------------------
119
120 #endif //#define CLITKCONNECTEDCOMPONENTLABELINGSGENERICFILTER_TXX