]> Creatis software - clitk.git/blobdiff - tools/clitkImageStatisticsGenericFilter.txx
cosmetic for .ggo
[clitk.git] / tools / clitkImageStatisticsGenericFilter.txx
index 81f93be1105ff3b162aee89c57530e0f1895744b..82a11ab5355a86134a3a6cc666ff9136b04dc423 100644 (file)
@@ -1,7 +1,7 @@
 /*=========================================================================
   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
 
-  Authors belong to: 
+  Authors belong to:
   - University of LYON              http://www.universite-lyon.fr/
   - Léon Bérard cancer center       http://www.centreleonberard.fr
   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
@@ -20,6 +20,7 @@
 
 #include "itkNthElementImageAdaptor.h"
 #include "itkJoinSeriesImageFilter.h"
+#include "itkImageRegionConstIterator.h"
 
 #include "clitkImageStatisticsGenericFilter.h"
 #include "clitkCropLikeImageFilter.h"
@@ -32,29 +33,29 @@ namespace clitk
   // Update with the number of dimensions
   //-------------------------------------------------------------------
   template<unsigned int Dimension, unsigned int Components>
-  void 
+  void
   ImageStatisticsGenericFilter::UpdateWithDim(std::string PixelType)
   {
     if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<" with " << Components << " channel(s)..."<<std::endl;
 
-    if(PixelType == "short"){  
+    if(PixelType == "short"){
       if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed short..." << std::endl;
-      UpdateWithDimAndPixelType<Dimension, signed short, Components>(); 
+      UpdateWithDimAndPixelType<Dimension, signed short, Components>();
     }
-    //    else if(PixelType == "unsigned_short"){  
-    //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl;
-    //       UpdateWithDimAndPixelType<Dimension, unsigned short>(); 
-    //     }
-    
-    else if (PixelType == "unsigned_char"){ 
+    else if(PixelType == "unsigned_short"){
+      if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl;
+      UpdateWithDimAndPixelType<Dimension, unsigned short, Components>();
+    }
+
+    else if (PixelType == "unsigned_char"){
       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl;
       UpdateWithDimAndPixelType<Dimension, unsigned char, Components>();
     }
-    
-    //     else if (PixelType == "char"){ 
-    //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl;
-    //       UpdateWithDimAndPixelType<Dimension, signed char>();
-    //     }
+
+    else if(PixelType == "double"){
+      if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and double..." << std::endl;
+      UpdateWithDimAndPixelType<Dimension, double, Components>();
+    }
     else {
       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
       UpdateWithDimAndPixelType<Dimension, float, Components>();
@@ -65,8 +66,8 @@ namespace clitk
   //-------------------------------------------------------------------
   // Update with the number of dimensions and the pixeltype
   //-------------------------------------------------------------------
-  template <unsigned int Dimension, class  PixelType, unsigned int Components> 
-  void 
+  template <unsigned int Dimension, class  PixelType, unsigned int Components>
+  void
   ImageStatisticsGenericFilter::UpdateWithDimAndPixelType()
   {
 
@@ -74,20 +75,20 @@ namespace clitk
     typedef unsigned char LabelPixelType;
     typedef itk::Image<itk::Vector<PixelType, Components>, Dimension> InputImageType;
     typedef itk::Image<LabelPixelType, Dimension> LabelImageType;
-    
+
     // Read the input
     typedef itk::ImageFileReader<InputImageType> InputReaderType;
     typename InputReaderType::Pointer reader = InputReaderType::New();
     reader->SetFileName( m_InputFileName);
     reader->Update();
     typename InputImageType::Pointer input= reader->GetOutput();
-    
+
     typedef itk::NthElementImageAdaptor<InputImageType, PixelType> InputImageAdaptorType;
     typedef itk::Image<PixelType, Dimension> OutputImageType;
 
     typename InputImageAdaptorType::Pointer input_adaptor = InputImageAdaptorType::New();
     input_adaptor->SetImage(input);
-    
+
     // Filter
     typedef itk::LabelStatisticsImageFilter<InputImageAdaptorType, LabelImageType> StatisticsImageFilterType;
     typename StatisticsImageFilterType::Pointer statisticsFilter=StatisticsImageFilterType::New();
@@ -104,13 +105,13 @@ namespace clitk
         // Due to a limitation of filter itk::LabelStatisticsImageFilter, InputImageType and LabelImageType
         // must have the same image dimension. However, we want to support label images with Dl = Di - 1,
         // so we need to replicate the label image as many times as the size along dimension Di.
-        if (m_Verbose) 
+        if (m_Verbose)
           std::cout << "Replicating label image to match input image's dimension... " << std::endl;
-        
+
         typedef itk::Image<LabelPixelType, Dimension - 1> ReducedLabelImageType;
         typedef itk::ImageFileReader<ReducedLabelImageType> LabelImageReaderType;
         typedef itk::JoinSeriesImageFilter<ReducedLabelImageType, LabelImageType> JoinImageFilterType;
-        
+
         typename LabelImageReaderType::Pointer labelImageReader=LabelImageReaderType::New();
         labelImageReader->SetFileName(m_ArgsInfo.mask_arg);
         labelImageReader->Update();
@@ -119,7 +120,7 @@ namespace clitk
         typename InputImageType::SizeType size = input->GetLargestPossibleRegion().GetSize();
         for (unsigned int i = 0; i < size[Dimension - 1]; i++)
           joinFilter->PushBackInput(labelImageReader->GetOutput());
-        
+
         joinFilter->Update();
         labelImage = joinFilter->GetOutput();
       }
@@ -140,15 +141,23 @@ namespace clitk
             typename ResamplerType::Pointer resampler = ResamplerType::New();
             resampler->SetInput(labelImage);
             resampler->SetOutputSpacing(input->GetSpacing());
+            resampler->SetOutputOrigin(labelImage->GetOrigin());
+            resampler->SetGaussianFilteringEnabled(false);
             resampler->Update();
             labelImage = resampler->GetOutput();
+            //writeImage<LabelImageType>(labelImage, "test1.mha");
+
+            typedef clitk::CropLikeImageFilter<LabelImageType> FilterType;
+            typename FilterType::Pointer crop = FilterType::New();
+            crop->SetInput(labelImage);
+            crop->SetCropLikeImage(input);
+            crop->Update();
+            labelImage = crop->GetOutput();
+            //writeImage<LabelImageType>(labelImage, "test2.mha");
 
-            typename itk::ImageBase<LabelImageType::ImageDimension>::RegionType reg 
-              = input->GetLargestPossibleRegion();
-            labelImage = ResizeImageLike<LabelImageType>(labelImage, &reg, 0);
           }
           else {
-            std::cerr << "Mask image has a different size/spacing than input. Abort" << std::endl;
+            std::cerr << "Mask image has a different size/spacing than input. Abort. (Use option to resize)" << std::endl;
             exit(-1);
           }
         }
@@ -156,16 +165,27 @@ namespace clitk
       }
 
     }
-    else { 
+    else {
       labelImage=LabelImageType::New();
+      labelImage->SetDirection(input->GetDirection());
       labelImage->SetRegions(input->GetLargestPossibleRegion());
       labelImage->SetOrigin(input->GetOrigin());
       labelImage->SetSpacing(input->GetSpacing());
+      labelImage->SetDirection(input->GetDirection());
       labelImage->Allocate();
       labelImage->FillBuffer(m_ArgsInfo.label_arg[0]);
     }
     statisticsFilter->SetLabelInput(labelImage);
 
+    // Check/compute spacing
+    const typename LabelImageType::SpacingType& spacing = input->GetSpacing();
+    double spacing_cc = (spacing[0]*spacing[1]*spacing[2])/1000;
+    // std::cout<<"Spacing x : " << spacing[0]<<std::endl;
+    // std::cout<<"Spacing y :  " << spacing[1]<<std::endl;
+    // std::cout<<"Spacing z :  " << spacing[2]<<std::endl;
+    // std::cout <<"spacing_cc : " << spacing_cc << std::endl;
+
+
     // For each Label
     typename LabelImageType::PixelType label;
     unsigned int numberOfLabels;
@@ -176,26 +196,26 @@ namespace clitk
 
     unsigned int firstComponent = 0, lastComponent = 0;
     if (m_ArgsInfo.channel_arg == -1) {
-      firstComponent = 0; 
+      firstComponent = 0;
       lastComponent = Components - 1;
     }
     else {
       firstComponent = m_ArgsInfo.channel_arg;
       lastComponent = m_ArgsInfo.channel_arg;
     }
-    
+
     for (unsigned int c=firstComponent; c<=lastComponent; c++) {
       if (m_Verbose) std::cout << std::endl << "Processing channel " << c << std::endl;
-      
+
       input_adaptor->SelectNthElement(c);
       input_adaptor->Update();
-      
+
       for (unsigned int k=0; k< numberOfLabels; k++) {
         label=m_ArgsInfo.label_arg[k];
 
         std::cout<<std::endl;
         if (m_Verbose) std::cout<<"-------------"<<std::endl;
-        if (m_Verbose) std::cout<<"| Label: "<<label<<"  |"<<std::endl;
+        if (m_Verbose) std::cout<<"| Label: "<< (int) label<<"  |"<<std::endl;
         if (m_Verbose) std::cout<<"-------------"<<std::endl;
 
         // Histograms
@@ -203,34 +223,57 @@ namespace clitk
           statisticsFilter->SetUseHistograms(true);
           statisticsFilter->SetHistogramParameters(m_ArgsInfo.bins_arg, m_ArgsInfo.lower_arg, m_ArgsInfo.upper_arg);
         }
+
+        // DVH
+        if(m_ArgsInfo.dvhistogram_given)
+        {
+          statisticsFilter->SetUseHistograms(true);
+          statisticsFilter->SetHistogramParameters(m_ArgsInfo.bins_arg, m_ArgsInfo.lower_arg, m_ArgsInfo.upper_arg);
+        }
+
         statisticsFilter->Update();
 
+        //find localization for max and min (the last pixel found)
+        typename InputImageType::IndexType minIndex, maxIndex;
+        if (m_Verbose && m_Localize) {
+          itk::ImageRegionConstIterator<InputImageAdaptorType> imageIterator(input_adaptor,input_adaptor->GetLargestPossibleRegion());
+          while(!imageIterator.IsAtEnd()) {
+            if (imageIterator.Get() == statisticsFilter->GetMinimum(label))
+              minIndex = imageIterator.GetIndex();
+            if (imageIterator.Get() == statisticsFilter->GetMaximum(label))
+              maxIndex = imageIterator.GetIndex();
+            ++imageIterator;
+          }
+        }
+
         // Output
         if (m_Verbose) std::cout<<"N° of pixels: ";
-          std::cout<<statisticsFilter->GetCount(label)<<std::endl;
-
+        std::cout<<statisticsFilter->GetCount(label)<<std::endl;
         if (m_Verbose) std::cout<<"Mean: ";
-          std::cout<<statisticsFilter->GetMean(label)<<std::endl;
-
+        std::cout<<statisticsFilter->GetMean(label)<<std::endl;
         if (m_Verbose) std::cout<<"SD: ";
-          std::cout<<statisticsFilter->GetSigma(label)<<std::endl;
-
+        std::cout<<statisticsFilter->GetSigma(label)<<std::endl;
         if (m_Verbose) std::cout<<"Variance: ";
-          std::cout<<statisticsFilter->GetVariance(label)<<std::endl;
-
+        std::cout<<statisticsFilter->GetVariance(label)<<std::endl;
         if (m_Verbose) std::cout<<"Min: ";
-          std::cout<<statisticsFilter->GetMinimum(label)<<std::endl;
-
+        std::cout<<statisticsFilter->GetMinimum(label)<<std::endl;
+        if (m_Verbose && m_Localize) {
+          std::cout<<"        in voxel of index: ";
+          std::cout<<minIndex<<std::endl;
+        }
         if (m_Verbose) std::cout<<"Max: ";
-          std::cout<<statisticsFilter->GetMaximum(label)<<std::endl;
-
+        std::cout<<statisticsFilter->GetMaximum(label)<<std::endl;
+        if (m_Verbose && m_Localize) {
+          std::cout<<"        in voxel of index: ";
+          std::cout<<maxIndex<<std::endl;
+        }
         if (m_Verbose) std::cout<<"Sum: ";
-          std::cout<<statisticsFilter->GetSum(label)<<std::endl;
-
+        std::cout<<statisticsFilter->GetSum(label)<<std::endl;
+        if (m_Verbose) std::cout<<"Volume (cc): ";
+        std::cout<<statisticsFilter->GetCount(label)*spacing_cc<<std::endl;
         if (m_Verbose) std::cout<<"Bounding box: ";
-        
         for(unsigned int i =0; i <statisticsFilter->GetBoundingBox(label).size(); i++)
-          std::cout<<statisticsFilter->GetBoundingBox(label)[i]<<" ";
+            std::cout<<statisticsFilter->GetBoundingBox(label)[i]<<" ";
         std::cout<<std::endl;
 
         // Histogram
@@ -254,14 +297,77 @@ namespace clitk
           for( int i =0; i <m_ArgsInfo.bins_arg; i++)
             histogramFile<<histogram->GetBinMin(0,i)<<"\t"<<histogram->GetMeasurement(i,0)<<"\t"<<histogram->GetBinMax(0,i)<<"\t"<<histogram->GetFrequency(i)<<std::endl;
         }
+
+        // DVH
+        if(m_ArgsInfo.dvhistogram_given)
+        {
+            typename StatisticsImageFilterType::HistogramPointer dvhistogram = statisticsFilter->GetHistogram(label);
+            double totalVolumeCC = ((statisticsFilter->GetCount(label))*spacing_cc);
+            double totalVolume = statisticsFilter->GetCount(label);
+
+            // Screen
+            std::cout<<"# Total volume : ";
+            std::cout<<totalVolume<<" [No. of voxels]"<<std::endl;
+            std::cout<<"# Total volume : ";
+            std::cout<<totalVolumeCC<<" [cc]"<<std::endl;
+            std::cout<<"# Dose mean: ";
+            std::cout<<statisticsFilter->GetMean(label)<<" [Gy]"<<std::endl;
+            std::cout<<"# Dose min: ";
+            std::cout<<statisticsFilter->GetMinimum(label)<<" [Gy]"<<std::endl;
+            std::cout<<"# Dose max: ";
+            std::cout<<statisticsFilter->GetMaximum(label)<<" [Gy]"<<std::endl;
+            std::cout<<" "<<std::endl;
+            std::cout<<"#Dose_diff[Gy] Volume_diff[No. of voxels] Volume_diff[%] Volume_diff[cc] Volume_cumul[No. of voxels] Volume_cumul[%] Volume_cumul[cc]"<<std::endl;
+            for( int i =0; i <m_ArgsInfo.bins_arg; i++)
+            {
+              double popCumulativeVolume = 0;
+              for(int j=0; j<i; j++)
+              {
+                 popCumulativeVolume+=(dvhistogram->GetFrequency(j));
+              }
+              double cumulativeVolume = (totalVolume - (popCumulativeVolume + (dvhistogram->GetFrequency(i))));
+              double percentCumulativeVolume =(cumulativeVolume*100)/(statisticsFilter->GetCount(label)) ;
+              double ccCumulativeVolume = (totalVolumeCC -((popCumulativeVolume + (dvhistogram->GetFrequency(i)))*spacing_cc));
+              double percentDiffVolume = dvhistogram->GetFrequency(i)*100/(statisticsFilter->GetCount(label));
+              std::cout<<dvhistogram->GetBinMax(0,i)<<"\t  "<<dvhistogram->GetFrequency(i)<<"\t  "<<percentDiffVolume<<"\t "<<((dvhistogram->GetFrequency(i))*spacing_cc)<<"\t "<<"\t "<<cumulativeVolume<<"\t  "<<percentCumulativeVolume<<"\t  "<<ccCumulativeVolume<<"\t "<<std::endl;
+            }
+
+            // Add to the file
+            std::ofstream dvhistogramFile(m_ArgsInfo.dvhistogram_arg);
+            dvhistogramFile<<"# Total volume : ";
+            dvhistogramFile<<statisticsFilter->GetCount(label)<<" [No. of voxels]"<<std::endl;
+            dvhistogramFile<<"# Total volume : ";
+            dvhistogramFile<<totalVolumeCC<<" [cc]"<<std::endl;
+            dvhistogramFile<<"# Dose mean: ";
+            dvhistogramFile<<statisticsFilter->GetMean(label)<<" [Gy]"<<std::endl;
+            dvhistogramFile<<"# Dose min: ";
+            dvhistogramFile<<statisticsFilter->GetMinimum(label)<<" [Gy]"<<std::endl;
+            dvhistogramFile<<"# Dose max: ";
+            dvhistogramFile<<statisticsFilter->GetMaximum(label)<<" [Gy]"<<std::endl;
+            dvhistogramFile<<"  "<<std::endl;
+            dvhistogramFile<<"#Dose_diff[Gy] Volume_diff[No. of voxels] Volume_diff[%] Volume_diff[cc] Volume_cumul[No. of voxels] Volume_cumul[%] Volume_cumul[cc]"<<std::endl;
+            for( int i =0; i <m_ArgsInfo.bins_arg; i++)
+            {
+               double popCumulativeVolume = 0;
+               for(int j=0; j<i; j++)
+               {
+                 popCumulativeVolume+=(dvhistogram->GetFrequency(j));
+               }
+               double cumulativeVolume = (totalVolume - (popCumulativeVolume + (dvhistogram->GetFrequency(i))));
+               double percentCumulativeVolume =(cumulativeVolume*100)/(statisticsFilter->GetCount(label));
+               double ccCumulativeVolume = (totalVolumeCC -((popCumulativeVolume + (dvhistogram->GetFrequency(i)))*spacing_cc));
+               double percentDiffVolume = ((dvhistogram->GetFrequency(i))*100)/(statisticsFilter->GetCount(label));
+               dvhistogramFile<<dvhistogram->GetBinMax(0,i)<<"\t  "<<dvhistogram->GetFrequency(i)<<"\t  "<<percentDiffVolume<<"\t "<<((dvhistogram->GetFrequency(i))*spacing_cc)<<"\t "<<cumulativeVolume<<"\t "<<percentCumulativeVolume<<"\t  "<<ccCumulativeVolume<<std::endl;
+           }
+        }
       }
     }
 
     return;
 
   }
-  
-  
+
+
 }//end clitk
 
 #endif //#define clitkImageStatisticsGenericFilter_txx