]> Creatis software - clitk.git/blob - itk/clitkLabelImageOverlapMeasureFilter.txx
Fix deprecated warning (Begin -> GoToBegin)
[clitk.git] / itk / clitkLabelImageOverlapMeasureFilter.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 //--------------------------------------------------------------------
20 template <class ImageType>
21 clitk::LabelImageOverlapMeasureFilter<ImageType>::
22 LabelImageOverlapMeasureFilter():
23   clitk::FilterBase(),
24   itk::ImageToImageFilter<ImageType, ImageType>()
25 {
26   this->SetNumberOfRequiredInputs(2);
27   SetLabel1(1);
28   SetLabel2(1);
29   SetBackgroundValue(0);
30 }
31 //--------------------------------------------------------------------
32
33
34 //--------------------------------------------------------------------
35 template <class ImageType>
36 void 
37 clitk::LabelImageOverlapMeasureFilter<ImageType>::
38 GenerateOutputInformation() 
39
40   // DD("GenerateOutputInformation");
41   //ImagePointer input = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
42   // ImagePointer outputImage = this->GetOutput(0);
43   // outputImage->SetRegions(outputImage->GetLargestPossibleRegion());
44 }
45 //--------------------------------------------------------------------
46
47
48 //--------------------------------------------------------------------
49 template <class ImageType>
50 void 
51 clitk::LabelImageOverlapMeasureFilter<ImageType>::
52 GenerateInputRequestedRegion() 
53 {
54   // Call default
55   itk::ImageToImageFilter<ImageType, ImageType>::GenerateInputRequestedRegion();
56   // Get input pointers and set requested region to common region
57   ImagePointer input1 = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
58   ImagePointer input2 = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(1));
59   input1->SetRequestedRegion(input1->GetLargestPossibleRegion());
60   input2->SetRequestedRegion(input2->GetLargestPossibleRegion());
61 }
62 //--------------------------------------------------------------------
63
64 //--------------------------------------------------------------------
65 template <class ImageType>
66 void 
67 clitk::LabelImageOverlapMeasureFilter<ImageType>::
68 GenerateData() 
69 {
70   // DD("GenerateData");
71
72   // Get input pointer
73   m_Input1 = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
74   m_Input2 = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(1));
75   static const unsigned int dim = ImageType::ImageDimension;
76
77   // Compute union of bounding boxes
78   typedef itk::BoundingBox<unsigned long, dim> BBType;
79   typename BBType::Pointer bb1 = BBType::New();
80   ComputeBBFromImageRegion<ImageType>(m_Input1, m_Input1->GetLargestPossibleRegion(), bb1);
81   typename BBType::Pointer bb2 = BBType::New();
82   ComputeBBFromImageRegion<ImageType>(m_Input2, m_Input2->GetLargestPossibleRegion(), bb2);
83   typename BBType::Pointer bbo = BBType::New();
84   ComputeBBUnion<dim>(bbo, bb1, bb2);
85
86   // Resize like the union
87   ImagePointer input1 = clitk::ResizeImageLike<ImageType>(m_Input1, bbo, GetBackgroundValue());
88   ImagePointer input2 = clitk::ResizeImageLike<ImageType>(m_Input2, bbo, GetBackgroundValue());
89   //DD(input1->GetLargestPossibleRegion());
90   //DD(input2->GetLargestPossibleRegion());
91
92   // Compute overlap image
93   ImagePointer image_union = clitk::Clone<ImageType>(input1);
94   ImagePointer image_intersection = clitk::Clone<ImageType>(input1);
95   clitk::Or<ImageType>(image_union, input2, GetBackgroundValue());
96   clitk::And<ImageType>(image_intersection, input2, GetBackgroundValue());
97
98   ImagePointer image_1NotIn2 = clitk::Clone<ImageType>(input1);
99   clitk::AndNot<ImageType>(image_1NotIn2, input2, GetBackgroundValue());
100   
101   ImagePointer image_2NotIn1 = clitk::Clone<ImageType>(input2);
102   clitk::AndNot<ImageType>(image_2NotIn1, input1, GetBackgroundValue());
103
104   //writeImage<ImageType>(image_union, "union.mha");
105   //writeImage<ImageType>(image_intersection, "intersection.mha");
106   //writeImage<ImageType>(image_1NotIn2, "image_1NotIn2.mha");
107   //writeImage<ImageType>(image_2NotIn1, "image_2NotIn1.mha");
108   
109   // Compute size
110   typedef itk::LabelStatisticsImageFilter<ImageType, ImageType> StatFilterType;
111   typename StatFilterType::Pointer statFilter = StatFilterType::New();
112   statFilter->SetInput(image_union);
113   statFilter->SetLabelInput(image_union);
114   statFilter->Update();
115   int u = statFilter->GetCount(GetLabel1());
116
117   statFilter->SetInput(image_intersection);
118   statFilter->SetLabelInput(image_intersection);
119   statFilter->Update();
120   int inter = statFilter->GetCount(GetLabel1());
121   
122   statFilter->SetInput(m_Input1);
123   statFilter->SetLabelInput(m_Input1);
124   statFilter->Update();
125   int in1 = statFilter->GetCount(GetLabel1());
126
127   statFilter->SetInput(m_Input2);
128   statFilter->SetLabelInput(m_Input2);
129   statFilter->Update();
130   int in2 = statFilter->GetCount(GetLabel1());
131
132   statFilter->SetInput(image_1NotIn2);
133   statFilter->SetLabelInput(image_1NotIn2);
134   statFilter->Update();
135   int l1notIn2 = statFilter->GetCount(GetLabel1());
136
137   statFilter->SetInput(image_2NotIn1);
138   statFilter->SetLabelInput(image_2NotIn1);
139   statFilter->Update();
140   int l2notIn1 = statFilter->GetCount(GetLabel1());
141
142   double dice = 2.0*(double)inter/(double)(in1+in2);
143   int width = 6;
144   std::cout << std::setw(width) << in1 << " "
145             << std::setw(width) << in2 << " "
146             << std::setw(width) << inter  << " "
147             << std::setw(width) << u  << " "
148             << std::setw(width) << l1notIn2  << " "
149             << std::setw(width) << l2notIn1  << " "
150             << std::setw(width) << dice << " "; //std::endl;
151 }
152 //--------------------------------------------------------------------
153