]> Creatis software - clitk.git/blob - itk/clitkLabelImageOverlapMeasureFilter.txx
motion masks with and without bands
[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   // DD("GenerateInputRequestedRegion");
55   // Call default
56   itk::ImageToImageFilter<ImageType, ImageType>::GenerateInputRequestedRegion();
57   // Get input pointers and set requested region to common region
58   ImagePointer input1 = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
59   ImagePointer input2 = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(1));
60   input1->SetRequestedRegion(input1->GetLargestPossibleRegion());
61   input2->SetRequestedRegion(input2->GetLargestPossibleRegion());
62 }
63 //--------------------------------------------------------------------
64
65 //--------------------------------------------------------------------
66 template <class ImageType>
67 void 
68 clitk::LabelImageOverlapMeasureFilter<ImageType>::
69 GenerateData() 
70 {
71   // DD("GenerateData");
72
73   // Get input pointer
74   m_Input1 = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
75   m_Input2 = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(1));
76   static const unsigned int dim = ImageType::ImageDimension;
77
78   // Compute union of bounding boxes
79   typedef itk::BoundingBox<unsigned long, dim> BBType;
80   typename BBType::Pointer bb1 = BBType::New();
81   ComputeBBFromImageRegion<ImageType>(m_Input1, m_Input1->GetLargestPossibleRegion(), bb1);
82   typename BBType::Pointer bb2 = BBType::New();
83   ComputeBBFromImageRegion<ImageType>(m_Input2, m_Input2->GetLargestPossibleRegion(), bb2);
84   typename BBType::Pointer bbo = BBType::New();
85   ComputeBBUnion<dim>(bbo, bb1, bb2);
86
87   // Resize like the union
88   ImagePointer input1 = clitk::ResizeImageLike<ImageType>(m_Input1, bbo, GetBackgroundValue());
89   ImagePointer input2 = clitk::ResizeImageLike<ImageType>(m_Input2, bbo, GetBackgroundValue());
90
91   // Compute overlap image
92   ImagePointer image_union = clitk::Clone<ImageType>(input1);
93   ImagePointer image_intersection = clitk::Clone<ImageType>(input1);
94   clitk::Or<ImageType>(image_union, input2, GetBackgroundValue());
95   clitk::And<ImageType>(image_intersection, input2, GetBackgroundValue());
96   
97   writeImage<ImageType>(image_union, "union.mha");
98   writeImage<ImageType>(image_intersection, "intersection.mha");
99   
100   // Compute size
101   typedef itk::LabelStatisticsImageFilter<ImageType, ImageType> StatFilterType;
102   typename StatFilterType::Pointer statFilter = StatFilterType::New();
103   statFilter->SetInput(image_union);
104   statFilter->SetLabelInput(image_union);
105   statFilter->Update();
106   int u = statFilter->GetCount(GetLabel1());
107
108   statFilter->SetInput(image_intersection);
109   statFilter->SetLabelInput(image_intersection);
110   statFilter->Update();
111   int inter = statFilter->GetCount(GetLabel1());
112   
113   statFilter->SetInput(m_Input1);
114   statFilter->SetLabelInput(m_Input1);
115   statFilter->Update();
116   int in1 = statFilter->GetCount(GetLabel1());
117
118   statFilter->SetInput(m_Input2);
119   statFilter->SetLabelInput(m_Input2);
120   statFilter->Update();
121   int in2 = statFilter->GetCount(GetLabel1());
122
123   std::cout << in1 << " " << in2 << " " << inter << " " << u << " " << (double)inter/(double)u << std::endl;
124 }
125 //--------------------------------------------------------------------
126