]> Creatis software - clitk.git/blob - segmentation/clitkConnectedComponentLabelingGenericFilter.txx
changes in license header
[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://www.centreleonberard.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 #include "clitkSegmentationUtils.h"
25
26 // itk
27 #include "itkConnectedComponentImageFilter.h"
28 #include "itkRelabelComponentImageFilter.h"
29   
30 //--------------------------------------------------------------------
31 template<class ArgsInfoType>
32 clitk::ConnectedComponentLabelingGenericFilter<ArgsInfoType>::ConnectedComponentLabelingGenericFilter():
33   ImageToImageGenericFilter<Self>("ConnectedComponentLabeling") 
34 {
35   //  InitializeImageType<2>();
36   InitializeImageType<3>();
37   //InitializeImageType<4>();
38 }
39 //--------------------------------------------------------------------
40
41
42 //--------------------------------------------------------------------
43 template<class ArgsInfoType>
44 template<unsigned int Dim>
45 void clitk::ConnectedComponentLabelingGenericFilter<ArgsInfoType>::InitializeImageType() 
46 {  
47   ADD_IMAGE_TYPE(Dim, uchar);
48   ADD_IMAGE_TYPE(Dim, short);
49   // ADD_IMAGE_TYPE(Dim, int);
50   // ADD_IMAGE_TYPE(Dim, float);
51 }
52 //--------------------------------------------------------------------
53   
54
55 //--------------------------------------------------------------------
56 template<class ArgsInfoType>
57 void clitk::ConnectedComponentLabelingGenericFilter<ArgsInfoType>::SetArgsInfo(const ArgsInfoType & a) 
58 {
59   mArgsInfo=a;
60   SetIOVerbose(mArgsInfo.verbose_flag);
61   if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
62   if (mArgsInfo.input_given)   AddInputFilename(mArgsInfo.input_arg);
63   if (mArgsInfo.output_given)  SetOutputFilename(mArgsInfo.output_arg);
64 }
65 //--------------------------------------------------------------------
66
67
68 //--------------------------------------------------------------------
69 // Update with the number of dimensions and the pixeltype
70 //--------------------------------------------------------------------
71 template<class ArgsInfoType>
72 template<class ImageType>
73 void clitk::ConnectedComponentLabelingGenericFilter<ArgsInfoType>::UpdateWithInputImageType() 
74
75   DD("UpdateWithInputImageType");
76
77   // Reading input
78   typename ImageType::Pointer input = this->template GetInput<ImageType>(0);
79
80   // Output image type
81   typedef itk::Image<int, ImageType::ImageDimension> OutputImageType;
82   PrintMemory(true, "initial");
83
84   typename OutputImageType::Pointer output;
85   {
86     typename OutputImageType::Pointer temp;
87     {
88       // Create CCL filter
89       typedef itk::ConnectedComponentImageFilter<ImageType, OutputImageType> ConnectFilterType;
90       typename ConnectFilterType::Pointer connectFilter = ConnectFilterType::New();
91       // connectFilter->ReleaseDataFlagOn(); // release earlier
92       connectFilter->SetInput(input);
93       connectFilter->SetBackgroundValue(mArgsInfo.inputBG_arg);
94       connectFilter->SetFullyConnected(mArgsInfo.full_flag);
95       //          connectFilter->SetNumberOfThreads(8);
96       connectFilter->Update();
97       temp = connectFilter->GetOutput();
98       PrintMemory(true, "after udpate");
99     }
100     PrintMemory(true, "after CCL block");
101     DD(input->GetReferenceCount());
102     DD(temp->GetReferenceCount());
103     
104     // Sort by size and remove too small area.
105     typedef itk::RelabelComponentImageFilter<OutputImageType, OutputImageType> RelabelFilterType;
106     typename RelabelFilterType::Pointer relabelFilter = RelabelFilterType::New();
107     //    relabelFilter->SetInput(connectFilter->GetOutput());
108     relabelFilter->SetInput(temp);
109     relabelFilter->SetMinimumObjectSize(mArgsInfo.minSize_arg);
110     relabelFilter->Update();
111     
112     DD(mArgsInfo.inputBG_arg);
113     DD(mArgsInfo.full_flag);
114     DD(mArgsInfo.minSize_arg);
115     
116     // Set information
117     const std::vector<typename RelabelFilterType::ObjectSizeType> & a 
118       = relabelFilter->GetSizeOfObjectsInPixels();
119     m_SizeOfObjectsInPixels.resize(a.size());
120     for(unsigned int i=0; i<a.size(); i++) m_SizeOfObjectsInPixels[i] = a[i];
121     m_SizeOfObjectsInPhysicalUnits = relabelFilter->GetSizeOfObjectsInPhysicalUnits();
122     m_OriginalNumberOfObjects = relabelFilter->GetOriginalNumberOfObjects();
123     DD(m_OriginalNumberOfObjects);
124     DD(m_SizeOfObjectsInPhysicalUnits.size());
125     
126     output = relabelFilter->GetOutput();
127   }
128   PrintMemory(true, "after block");
129
130   // Write/Save results
131   this->template SetNextOutput<OutputImageType>(output); 
132   PrintMemory(true, "end filter ");
133 }
134 //--------------------------------------------------------------------
135
136 #endif //#define CLITKCONNECTEDCOMPONENTLABELINGSGENERICFILTER_TXX