]> Creatis software - clitk.git/blob - tools/clitkRTStructStatisticsGenericFilter.txx
d83b25d9caeff24194d67af207d5a4bda60d1f8d
[clitk.git] / tools / clitkRTStructStatisticsGenericFilter.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 #ifndef CLITKRTSTRUCTSTATISTICSGENERICFILTER_TXX
19 #define CLITKRTSTRUCTSTATISTICSGENERICFILTER_TXX
20
21 #include "clitkImageCommon.h"
22
23 #include "itkConnectedComponentImageFilter.h"
24 #include "itkLabelImageToShapeLabelMapFilter.h"
25
26 namespace clitk
27 {
28
29 //--------------------------------------------------------------------
30 template<class args_info_type>
31 RTStructStatisticsGenericFilter<args_info_type>::RTStructStatisticsGenericFilter()
32   :ImageToImageGenericFilter<Self>("RTStructStatisticsGenericFilter")
33 {
34   InitializeImageType<2>();
35   InitializeImageType<3>();
36   InitializeImageType<4>();
37 }
38 //--------------------------------------------------------------------
39
40
41 //--------------------------------------------------------------------
42 template<class args_info_type>
43 template<unsigned int Dim>
44 void RTStructStatisticsGenericFilter<args_info_type>::InitializeImageType()
45 {
46   ADD_DEFAULT_IMAGE_TYPES(Dim);
47 }
48 //--------------------------------------------------------------------
49
50
51 //--------------------------------------------------------------------
52 template<class args_info_type>
53 void RTStructStatisticsGenericFilter<args_info_type>::SetArgsInfo(const args_info_type & a)
54 {
55   mArgsInfo=a;
56
57   // Set value
58   this->SetIOVerbose(mArgsInfo.verbose_flag);
59
60   if (mArgsInfo.input_given) this->AddInputFilename(mArgsInfo.input_arg);
61
62   }
63 //--------------------------------------------------------------------
64
65
66 //--------------------------------------------------------------------
67 template<class args_info_type>
68 template<class ImageType>
69 void RTStructStatisticsGenericFilter<args_info_type>::UpdateWithInputImageType()
70 {
71   // Read mask input
72   typedef itk::Image<unsigned char, ImageType::ImageDimension> MaskInputImageType;
73   typename MaskInputImageType::Pointer mask;
74   mask = this->template GetInput<MaskInputImageType>(0);
75
76   //Create the Shape Label Map from the mask
77   typedef itk::Image< unsigned char, ImageType::ImageDimension > OutputImageType;
78   typedef itk::ShapeLabelObject< unsigned char, ImageType::ImageDimension > ShapeLabelObjectType;
79   typedef itk::LabelMap< ShapeLabelObjectType > LabelMapType;
80   typedef itk::ConnectedComponentImageFilter <MaskInputImageType, OutputImageType > ConnectedComponentImageFilterType;
81   typedef itk::LabelImageToShapeLabelMapFilter< OutputImageType, LabelMapType> I2LType;
82
83   typename ConnectedComponentImageFilterType::Pointer connected = ConnectedComponentImageFilterType::New ();
84   connected->SetInput(mask);
85   connected->FullyConnectedOn();
86   connected->Update();
87
88   //Create a map to contain all connectedComponent (even a little pixel)
89   typename I2LType::Pointer i2l = I2LType::New();
90   i2l->SetInput( connected->GetOutput() );
91   i2l->SetComputePerimeter(true);
92   i2l->Update();
93
94   // Retrieve the biggest component
95   LabelMapType *labelMap = i2l->GetOutput();
96   int largestComponent(0);
97   int nbPixel(0);
98   for (unsigned int n = 0; n < labelMap->GetNumberOfLabelObjects(); ++n)
99   {
100     ShapeLabelObjectType *labelObject = labelMap->GetNthLabelObject(n);
101     if (labelObject->GetNumberOfPixels() > nbPixel)
102     {
103         nbPixel = labelObject->GetNumberOfPixels();
104         largestComponent = n;
105     }
106   }
107
108   //Write statitistics on the largest component
109   ShapeLabelObjectType *labelObject = labelMap->GetNthLabelObject(largestComponent);
110   std::cout << " Centroid: " << std::endl;
111   std::cout << labelObject->GetCentroid()[0] << std::endl;
112   std::cout << labelObject->GetCentroid()[1] << std::endl;
113   std::cout << labelObject->GetCentroid()[2] << std::endl;
114   std::cout << " Roundness: " << std::endl;
115   std::cout << labelObject->GetRoundness() << std::endl;
116
117 }
118 //--------------------------------------------------------------------
119
120
121
122 } // end namespace
123
124 #endif  //#define CLITKRTSTRUCTSTATISTICSGENERICFILTER_TXX