]> Creatis software - clitk.git/blob - segmentation/clitkConnectedComponentLabelingGenericFilter.txx
Debug RTStruct conversion with empty struc
[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, ushort);
50   // ADD_IMAGE_TYPE(Dim, int);
51   ADD_IMAGE_TYPE(Dim, float);
52 }
53 //--------------------------------------------------------------------
54   
55
56 //--------------------------------------------------------------------
57 template<class ArgsInfoType>
58 void clitk::ConnectedComponentLabelingGenericFilter<ArgsInfoType>::SetArgsInfo(const ArgsInfoType & a) 
59 {
60   mArgsInfo=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);
65 }
66 //--------------------------------------------------------------------
67
68
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() 
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   //PrintMemory(true, "initial");
82
83   typename OutputImageType::Pointer output;
84   {
85     typename OutputImageType::Pointer temp;
86     {
87       // Create CCL filter
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");
98     }
99     // PrintMemory(true, "after CCL block");
100     // DD(input->GetReferenceCount());
101     // DD(temp->GetReferenceCount());
102     
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();
110     
111     // DD(mArgsInfo.inputBG_arg);
112     // DD(mArgsInfo.full_flag);
113     // DD(mArgsInfo.minSize_arg);
114     
115     // Set information
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());
124     
125     output = relabelFilter->GetOutput();
126   }
127   // PrintMemory(true, "after block");
128
129   // Write/Save results
130   this->template SetNextOutput<OutputImageType>(output); 
131   // PrintMemory(true, "end filter ");
132 }
133 //--------------------------------------------------------------------
134
135 #endif //#define CLITKCONNECTEDCOMPONENTLABELINGSGENERICFILTER_TXX