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 //--------------------------------------------------------------------
20 template <class ImageType>
21 clitk::LabelImageOverlapMeasureFilter<ImageType>::
22 LabelImageOverlapMeasureFilter():
24 itk::ImageToImageFilter<ImageType, ImageType>()
26 this->SetNumberOfRequiredInputs(2);
29 SetBackgroundValue(0);
31 //--------------------------------------------------------------------
34 //--------------------------------------------------------------------
35 template <class ImageType>
37 clitk::LabelImageOverlapMeasureFilter<ImageType>::
38 GenerateOutputInformation()
40 // DD("GenerateOutputInformation");
41 //ImagePointer input = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
42 // ImagePointer outputImage = this->GetOutput(0);
43 // outputImage->SetRegions(outputImage->GetLargestPossibleRegion());
45 //--------------------------------------------------------------------
48 //--------------------------------------------------------------------
49 template <class ImageType>
51 clitk::LabelImageOverlapMeasureFilter<ImageType>::
52 GenerateInputRequestedRegion()
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());
62 //--------------------------------------------------------------------
64 //--------------------------------------------------------------------
65 template <class ImageType>
67 clitk::LabelImageOverlapMeasureFilter<ImageType>::
70 // DD("GenerateData");
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;
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);
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());
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());
98 ImagePointer image_1NotIn2 = clitk::Clone<ImageType>(input1);
99 clitk::AndNot<ImageType>(image_1NotIn2, input2, GetBackgroundValue());
101 ImagePointer image_2NotIn1 = clitk::Clone<ImageType>(input2);
102 clitk::AndNot<ImageType>(image_2NotIn1, input1, GetBackgroundValue());
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");
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());
117 statFilter->SetInput(image_intersection);
118 statFilter->SetLabelInput(image_intersection);
119 statFilter->Update();
120 int inter = statFilter->GetCount(GetLabel1());
122 statFilter->SetInput(m_Input1);
123 statFilter->SetLabelInput(m_Input1);
124 statFilter->Update();
125 int in1 = statFilter->GetCount(GetLabel1());
127 statFilter->SetInput(m_Input2);
128 statFilter->SetLabelInput(m_Input2);
129 statFilter->Update();
130 int in2 = statFilter->GetCount(GetLabel1());
132 statFilter->SetInput(image_1NotIn2);
133 statFilter->SetLabelInput(image_1NotIn2);
134 statFilter->Update();
135 int l1notIn2 = statFilter->GetCount(GetLabel1());
137 statFilter->SetInput(image_2NotIn1);
138 statFilter->SetLabelInput(image_2NotIn1);
139 statFilter->Update();
140 int l2notIn1 = statFilter->GetCount(GetLabel1());
142 double dice = 2.0*(double)inter/(double)(in1+in2);
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;
152 //--------------------------------------------------------------------