1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
19 #ifndef CLITKCONNECTEDCOMPONENTLABELINGSGENERICFILTER_TXX
20 #define CLITKCONNECTEDCOMPONENTLABELINGSGENERICFILTER_TXX
23 #include "clitkImageCommon.h"
24 #include "clitkSegmentationUtils.h"
27 #include "itkConnectedComponentImageFilter.h"
28 #include "itkRelabelComponentImageFilter.h"
30 //--------------------------------------------------------------------
31 template<class ArgsInfoType>
32 clitk::ConnectedComponentLabelingGenericFilter<ArgsInfoType>::ConnectedComponentLabelingGenericFilter():
33 ImageToImageGenericFilter<Self>("ConnectedComponentLabeling")
35 InitializeImageType<2>();
36 InitializeImageType<3>();
37 //InitializeImageType<4>();
39 //--------------------------------------------------------------------
42 //--------------------------------------------------------------------
43 template<class ArgsInfoType>
44 template<unsigned int Dim>
45 void clitk::ConnectedComponentLabelingGenericFilter<ArgsInfoType>::InitializeImageType()
47 ADD_IMAGE_TYPE(Dim, uchar);
48 ADD_IMAGE_TYPE(Dim, short);
49 // ADD_IMAGE_TYPE(Dim, int);
50 ADD_IMAGE_TYPE(Dim, float);
52 //--------------------------------------------------------------------
55 //--------------------------------------------------------------------
56 template<class ArgsInfoType>
57 void clitk::ConnectedComponentLabelingGenericFilter<ArgsInfoType>::SetArgsInfo(const ArgsInfoType & 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);
65 //--------------------------------------------------------------------
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()
76 typename ImageType::Pointer input = this->template GetInput<ImageType>(0);
79 typedef itk::Image<int, ImageType::ImageDimension> OutputImageType;
80 //PrintMemory(true, "initial");
82 typename OutputImageType::Pointer output;
84 typename OutputImageType::Pointer temp;
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");
98 // PrintMemory(true, "after CCL block");
99 // DD(input->GetReferenceCount());
100 // DD(temp->GetReferenceCount());
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();
110 // DD(mArgsInfo.inputBG_arg);
111 // DD(mArgsInfo.full_flag);
112 // DD(mArgsInfo.minSize_arg);
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());
124 output = relabelFilter->GetOutput();
126 // PrintMemory(true, "after block");
128 // Write/Save results
129 this->template SetNextOutput<OutputImageType>(output);
130 // PrintMemory(true, "end filter ");
132 //--------------------------------------------------------------------
134 #endif //#define CLITKCONNECTEDCOMPONENTLABELINGSGENERICFILTER_TXX