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, ushort);
50 // ADD_IMAGE_TYPE(Dim, int);
51 ADD_IMAGE_TYPE(Dim, float);
53 //--------------------------------------------------------------------
56 //--------------------------------------------------------------------
57 template<class ArgsInfoType>
58 void clitk::ConnectedComponentLabelingGenericFilter<ArgsInfoType>::SetArgsInfo(const ArgsInfoType & a)
61 this->SetIOVerbose(mArgsInfo.verbose_flag);
62 if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
63 if (mArgsInfo.input_given) this->AddInputFilename(mArgsInfo.input_arg);
64 if (mArgsInfo.output_given) this->SetOutputFilename(mArgsInfo.output_arg);
66 //--------------------------------------------------------------------
69 //--------------------------------------------------------------------
70 // Update with the number of dimensions and the pixeltype
71 //--------------------------------------------------------------------
72 template<class ArgsInfoType>
73 template<class ImageType>
74 void clitk::ConnectedComponentLabelingGenericFilter<ArgsInfoType>::UpdateWithInputImageType()
77 typename ImageType::Pointer input = this->template GetInput<ImageType>(0);
80 typedef itk::Image<int, ImageType::ImageDimension> OutputImageType;
81 //PrintMemory(true, "initial");
83 typename OutputImageType::Pointer output;
85 typename OutputImageType::Pointer temp;
88 typedef itk::ConnectedComponentImageFilter<ImageType, OutputImageType> ConnectFilterType;
89 typename ConnectFilterType::Pointer connectFilter = ConnectFilterType::New();
90 // connectFilter->ReleaseDataFlagOn(); // release earlier
91 connectFilter->SetInput(input);
92 connectFilter->SetBackgroundValue(mArgsInfo.inputBG_arg);
93 connectFilter->SetFullyConnected(mArgsInfo.full_flag);
94 // connectFilter->SetNumberOfThreads(8);
95 connectFilter->Update();
96 temp = connectFilter->GetOutput();
97 // PrintMemory(true, "after udpate");
99 // PrintMemory(true, "after CCL block");
100 // DD(input->GetReferenceCount());
101 // DD(temp->GetReferenceCount());
103 // Sort by size and remove too small area.
104 typedef itk::RelabelComponentImageFilter<OutputImageType, OutputImageType> RelabelFilterType;
105 typename RelabelFilterType::Pointer relabelFilter = RelabelFilterType::New();
106 // relabelFilter->SetInput(connectFilter->GetOutput());
107 relabelFilter->SetInput(temp);
108 relabelFilter->SetMinimumObjectSize(mArgsInfo.minSize_arg);
109 relabelFilter->Update();
111 // DD(mArgsInfo.inputBG_arg);
112 // DD(mArgsInfo.full_flag);
113 // DD(mArgsInfo.minSize_arg);
116 const std::vector<typename RelabelFilterType::ObjectSizeType> & a
117 = relabelFilter->GetSizeOfObjectsInPixels();
118 m_SizeOfObjectsInPixels.resize(a.size());
119 for(unsigned int i=0; i<a.size(); i++) m_SizeOfObjectsInPixels[i] = a[i];
120 m_SizeOfObjectsInPhysicalUnits = relabelFilter->GetSizeOfObjectsInPhysicalUnits();
121 m_OriginalNumberOfObjects = relabelFilter->GetOriginalNumberOfObjects();
122 // DD(m_OriginalNumberOfObjects);
123 // DD(m_SizeOfObjectsInPhysicalUnits.size());
125 output = relabelFilter->GetOutput();
127 // PrintMemory(true, "after block");
129 // Write/Save results
130 this->template SetNextOutput<OutputImageType>(output);
131 // PrintMemory(true, "end filter ");
133 //--------------------------------------------------------------------
135 #endif //#define CLITKCONNECTEDCOMPONENTLABELINGSGENERICFILTER_TXX