]> Creatis software - clitk.git/blob - segmentation/clitkConnectedComponentLabelingGenericFilter.txx
option to auto-detect motion mask's initial ellipse
[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   this->SetIOVerbose(mArgsInfo.verbose_flag);
61   if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
62   if (mArgsInfo.input_given)   this->AddInputFilename(mArgsInfo.input_arg);
63   if (mArgsInfo.output_given)  this->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   // Reading input
76   typename ImageType::Pointer input = this->template GetInput<ImageType>(0);
77
78   // Output image type
79   typedef itk::Image<int, ImageType::ImageDimension> OutputImageType;
80   //PrintMemory(true, "initial");
81
82   typename OutputImageType::Pointer output;
83   {
84     typename OutputImageType::Pointer temp;
85     {
86       // Create CCL filter
87       typedef itk::ConnectedComponentImageFilter<ImageType, OutputImageType> ConnectFilterType;
88       typename ConnectFilterType::Pointer connectFilter = ConnectFilterType::New();
89       // connectFilter->ReleaseDataFlagOn(); // release earlier
90       connectFilter->SetInput(input);
91       connectFilter->SetBackgroundValue(mArgsInfo.inputBG_arg);
92       connectFilter->SetFullyConnected(mArgsInfo.full_flag);
93       //          connectFilter->SetNumberOfThreads(8);
94       connectFilter->Update();
95       temp = connectFilter->GetOutput();
96       // PrintMemory(true, "after udpate");
97     }
98     // PrintMemory(true, "after CCL block");
99     // DD(input->GetReferenceCount());
100     // DD(temp->GetReferenceCount());
101     
102     // Sort by size and remove too small area.
103     typedef itk::RelabelComponentImageFilter<OutputImageType, OutputImageType> RelabelFilterType;
104     typename RelabelFilterType::Pointer relabelFilter = RelabelFilterType::New();
105     //    relabelFilter->SetInput(connectFilter->GetOutput());
106     relabelFilter->SetInput(temp);
107     relabelFilter->SetMinimumObjectSize(mArgsInfo.minSize_arg);
108     relabelFilter->Update();
109     
110     // DD(mArgsInfo.inputBG_arg);
111     // DD(mArgsInfo.full_flag);
112     // DD(mArgsInfo.minSize_arg);
113     
114     // Set information
115     const std::vector<typename RelabelFilterType::ObjectSizeType> & a 
116       = relabelFilter->GetSizeOfObjectsInPixels();
117     m_SizeOfObjectsInPixels.resize(a.size());
118     for(unsigned int i=0; i<a.size(); i++) m_SizeOfObjectsInPixels[i] = a[i];
119     m_SizeOfObjectsInPhysicalUnits = relabelFilter->GetSizeOfObjectsInPhysicalUnits();
120     m_OriginalNumberOfObjects = relabelFilter->GetOriginalNumberOfObjects();
121     // DD(m_OriginalNumberOfObjects);
122     // DD(m_SizeOfObjectsInPhysicalUnits.size());
123     
124     output = relabelFilter->GetOutput();
125   }
126   // PrintMemory(true, "after block");
127
128   // Write/Save results
129   this->template SetNextOutput<OutputImageType>(output); 
130   // PrintMemory(true, "end filter ");
131 }
132 //--------------------------------------------------------------------
133
134 #endif //#define CLITKCONNECTEDCOMPONENTLABELINGSGENERICFILTER_TXX