]> Creatis software - clitk.git/blob - itk/clitkLabelImageOverlapMeasureFilter.txx
Add clitkImage2Dicom tool
[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   m_BackgroundValue = 0;
30   SetVerboseFlag(false);
31   SetLongFlag(false);
32 }
33 //--------------------------------------------------------------------
34
35
36 //--------------------------------------------------------------------
37 template <class ImageType>
38 void
39 clitk::LabelImageOverlapMeasureFilter<ImageType>::
40 GenerateOutputInformation()
41 {
42   // DD("GenerateOutputInformation");
43   //ImagePointer input = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
44   // ImagePointer outputImage = this->GetOutput(0);
45   // outputImage->SetRegions(outputImage->GetLargestPossibleRegion());
46 }
47 //--------------------------------------------------------------------
48
49
50 //--------------------------------------------------------------------
51 template <class ImageType>
52 void
53 clitk::LabelImageOverlapMeasureFilter<ImageType>::
54 GenerateInputRequestedRegion()
55 {
56   // Call default
57   itk::ImageToImageFilter<ImageType, ImageType>::GenerateInputRequestedRegion();
58   // Get input pointers and set requested region to common region
59   ImagePointer input1 = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
60   ImagePointer input2 = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(1));
61   input1->SetRequestedRegion(input1->GetLargestPossibleRegion());
62   input2->SetRequestedRegion(input2->GetLargestPossibleRegion());
63 }
64 //--------------------------------------------------------------------
65
66 //--------------------------------------------------------------------
67 template <class ImageType>
68 void
69 clitk::LabelImageOverlapMeasureFilter<ImageType>::
70 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   /*
90     if (GetVerboseFlag()) {
91     std::cout << "Resize images to the union of bounding boxes: "
92     <<  input1->GetLargestPossibleRegion().GetSize() << std::endl;
93     }
94   */
95
96   /*
97   int width = 6;
98   std::cout << std::setw(width) << in1 << " "
99             << std::setw(width) << in2 << " "
100             << std::setw(width) << inter  << " "
101             << std::setw(width) << u  << " "
102             << std::setw(width) << l1notIn2  << " "
103             << std::setw(width) << l2notIn1  << " "
104             << std::setw(width) << dice << " "; //std::endl;
105   */
106
107   // Compute overlap, dice
108   typedef itk::LabelOverlapMeasuresImageFilter<ImageType> FilterType;
109   typename FilterType::Pointer diceFilter = FilterType::New();
110   diceFilter->SetSourceImage(input1);
111   diceFilter->SetTargetImage(input2);
112   diceFilter->Update();
113
114   // Display information
115   if (!GetLongFlag()) {
116     if (GetVerboseFlag()) {
117       std::cout << "Dice : ";
118     }
119     std::cout << diceFilter->GetDiceCoefficient() << std::endl;
120   }
121   else { // long options
122     if (GetVerboseFlag()) {
123       std::cout << "Dice     Jaccard   Source Target Inter  Union   SrComp TarComp" << std::endl;
124     }
125     typename FilterType::MapType m = diceFilter->GetLabelSetMeasures();
126     int width = 6;
127     std::cout << std::setw(width) << diceFilter->GetDiceCoefficient() << " "
128               << std::setw(width) << diceFilter->GetJaccardCoefficient() << " "
129               << std::setw(width) << m[m_Label1].m_Source << " "
130               << std::setw(width) << m[m_Label2].m_Target << " "
131               << std::setw(width) << m[m_Label1].m_Intersection << " "
132               << std::setw(width) << m[m_Label1].m_Union << " "
133               << std::setw(width) << m[m_Label1].m_SourceComplement << " "
134               << std::setw(width) << m[m_Label2].m_TargetComplement << " "
135               << std::endl;
136   }
137
138 }
139 //--------------------------------------------------------------------