ITK/Examples/Statistics/Histogram: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
(Created page with "==Histogram.cxx== <source lang="cpp"> #include "itkHistogram.h" int main() { typedef float MeasurementType ; typedef itk::Statistics::Histogram< MeasurementType, i...")
 
(Wrong signature for main)
Line 3: Line 3:
#include "itkHistogram.h"
#include "itkHistogram.h"


int main()
int main(int, char *[])
{
{
   typedef float MeasurementType ;
   typedef float MeasurementType;
   typedef itk::Statistics::Histogram< MeasurementType,
   typedef itk::Statistics::Histogram< MeasurementType,
           itk::Statistics::DenseFrequencyContainer2 > HistogramType;
           itk::Statistics::DenseFrequencyContainer2 > HistogramType;
   HistogramType::Pointer histogram = HistogramType::New() ;
   HistogramType::Pointer histogram = HistogramType::New();


   unsigned int dimension = 3;
   unsigned int dimension = 3;
Line 16: Line 16:
   size.Fill(binsPerDimension);
   size.Fill(binsPerDimension);
    
    
   HistogramType::MeasurementVectorType lowerBound ;
   HistogramType::MeasurementVectorType lowerBound;
   lowerBound.SetSize(binsPerDimension);
   lowerBound.SetSize(binsPerDimension);
   lowerBound.Fill(1.1);
   lowerBound.Fill(1.1);
    
    
   HistogramType::MeasurementVectorType upperBound ;
   HistogramType::MeasurementVectorType upperBound;
   upperBound.SetSize(binsPerDimension);
   upperBound.SetSize(binsPerDimension);
   upperBound.Fill(6.2) ;
   upperBound.Fill(6.2);
    
    
   histogram->SetMeasurementVectorSize(dimension);
   histogram->SetMeasurementVectorSize(dimension);
   histogram->Initialize(size, lowerBound, upperBound ) ;
   histogram->Initialize(size, lowerBound, upperBound );


   histogram->SetFrequency(0UL, 0.0) ;
   histogram->SetFrequency(0UL, 0.0);
   histogram->SetFrequency(1UL, 2.0) ;
   histogram->SetFrequency(1UL, 2.0);
   histogram->SetFrequency(2UL, 3.0) ;
   histogram->SetFrequency(2UL, 3.0);
   histogram->SetFrequency(3UL, 2.0) ;
   histogram->SetFrequency(3UL, 2.0);
   histogram->SetFrequency(4UL, 0.5) ;
   histogram->SetFrequency(4UL, 0.5);
   histogram->SetFrequency(5UL, 1.0) ;
   histogram->SetFrequency(5UL, 1.0);
   histogram->SetFrequency(6UL, 5.0) ;
   histogram->SetFrequency(6UL, 5.0);
   histogram->SetFrequency(7UL, 2.5) ;
   histogram->SetFrequency(7UL, 2.5);
   histogram->SetFrequency(8UL, 0.0) ;
   histogram->SetFrequency(8UL, 0.0);


   HistogramType::IndexType index(dimension) ;
   HistogramType::IndexType index(dimension);
   index[0] = 0 ;
   index[0] = 0;
   index[1] = 2 ;
   index[1] = 2;
   index[1] = 4 ;
   index[1] = 4;
   std::cout << "Frequency of the bin at index  " << index
   std::cout << "Frequency of the bin at index  " << index
             << " is " << histogram->GetFrequency(index) << std::endl ;
             << " is " << histogram->GetFrequency(index) << std::endl;


     
     
   std::cout << "Measurement vector at bin " << index << " is "
   std::cout << "Measurement vector at bin " << index << " is "
             << histogram->GetMeasurementVector(index) << std::endl ;
             << histogram->GetMeasurementVector(index) << std::endl;
     
     
   HistogramType::MeasurementVectorType mv(dimension) ;
   HistogramType::MeasurementVectorType mv(dimension);
   mv[0] = 4.1 ;
   mv[0] = 4.1;
   mv[1] = 5.6 ;
   mv[1] = 5.6;
   mv[2] = 3.6 ;
   mv[2] = 3.6;


   HistogramType::IndexType resultingIndex = histogram->GetIndex(mv);
   HistogramType::IndexType resultingIndex = histogram->GetIndex(mv);
    
    
   std::cout << "Index of the measurement vector " << mv
   std::cout << "Index of the measurement vector " << mv
             << " is " << resultingIndex << std::endl ;
             << " is " << resultingIndex << std::endl;


   index.Fill(100) ;
   index.Fill(100);
   if ( histogram->IsIndexOutOfBounds(index) )
   if ( histogram->IsIndexOutOfBounds(index) )
     {
     {
     std::cout << "Index " << index << " is out of bounds." << std::endl ;
     std::cout << "Index " << index << " is out of bounds." << std::endl;
     }
     }


   std::cout << "Number of bins = " << histogram->Size()
   std::cout << "Number of bins = " << histogram->Size()
             << " Total frequency = " << histogram->GetTotalFrequency()
             << " Total frequency = " << histogram->GetTotalFrequency()
             << " Dimension sizes = " << histogram->GetSize() << std::endl ;
             << " Dimension sizes = " << histogram->GetSize() << std::endl;


   std::cout << "50th percentile along the first dimension = "
   std::cout << "50th percentile along the first dimension = "
             << histogram->Quantile(0, 0.5) << std::endl ;
             << histogram->Quantile(0, 0.5) << std::endl;


   return 0 ;
   return EXIT_SUCCESS;
}
}
</source>
</source>


{{ITKCMakeLists|Histogram}}
{{ITKCMakeLists|Histogram}}

Revision as of 11:57, 19 September 2011

Histogram.cxx

<source lang="cpp">

  1. include "itkHistogram.h"

int main(int, char *[]) {

 typedef float MeasurementType;
 typedef itk::Statistics::Histogram< MeasurementType,
         itk::Statistics::DenseFrequencyContainer2 > HistogramType;
 HistogramType::Pointer histogram = HistogramType::New();
 unsigned int dimension = 3;
 HistogramType::SizeType size(dimension);
 
 unsigned int binsPerDimension = 30;
 size.Fill(binsPerDimension);
 
 HistogramType::MeasurementVectorType lowerBound;
 lowerBound.SetSize(binsPerDimension);
 lowerBound.Fill(1.1);
 
 HistogramType::MeasurementVectorType upperBound;
 upperBound.SetSize(binsPerDimension);
 upperBound.Fill(6.2);
 
 histogram->SetMeasurementVectorSize(dimension);
 histogram->Initialize(size, lowerBound, upperBound );
 histogram->SetFrequency(0UL, 0.0);
 histogram->SetFrequency(1UL, 2.0);
 histogram->SetFrequency(2UL, 3.0);
 histogram->SetFrequency(3UL, 2.0);
 histogram->SetFrequency(4UL, 0.5);
 histogram->SetFrequency(5UL, 1.0);
 histogram->SetFrequency(6UL, 5.0);
 histogram->SetFrequency(7UL, 2.5);
 histogram->SetFrequency(8UL, 0.0);
 HistogramType::IndexType index(dimension);
 index[0] = 0;
 index[1] = 2;
 index[1] = 4;
 std::cout << "Frequency of the bin at index  " << index
           << " is " << histogram->GetFrequency(index) << std::endl;


 std::cout << "Measurement vector at bin " << index << " is "
           << histogram->GetMeasurementVector(index) << std::endl;
 HistogramType::MeasurementVectorType mv(dimension);
 mv[0] = 4.1;
 mv[1] = 5.6;
 mv[2] = 3.6;
 HistogramType::IndexType resultingIndex = histogram->GetIndex(mv);
 
 std::cout << "Index of the measurement vector " << mv
           << " is " << resultingIndex << std::endl;
 index.Fill(100);
 if ( histogram->IsIndexOutOfBounds(index) )
   {
   std::cout << "Index " << index << " is out of bounds." << std::endl;
   }
 std::cout << "Number of bins = " << histogram->Size()
           << " Total frequency = " << histogram->GetTotalFrequency()
           << " Dimension sizes = " << histogram->GetSize() << std::endl;
 std::cout << "50th percentile along the first dimension = "
           << histogram->Quantile(0, 0.5) << std::endl;
 return EXIT_SUCCESS;

} </source>

CMakeLists.txt

<syntaxhighlight lang="cmake"> cmake_minimum_required(VERSION 3.9.5)

project(Histogram)

find_package(ITK REQUIRED) include(${ITK_USE_FILE}) if (ITKVtkGlue_LOADED)

 find_package(VTK REQUIRED)
 include(${VTK_USE_FILE})

endif()

add_executable(Histogram MACOSX_BUNDLE Histogram.cxx)

if( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(Histogram ITKReview ${ITK_LIBRARIES})

else( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(Histogram ${ITK_LIBRARIES})

endif( "${ITK_VERSION_MAJOR}" LESS 4 )

</syntaxhighlight>

Download and Build Histogram

Click here to download Histogram and its CMakeLists.txt file. Once the tarball Histogram.tar has been downloaded and extracted,

cd Histogram/build
  • If ITK is installed:
cmake ..
  • If ITK is not installed but compiled on your system, you will need to specify the path to your ITK build:
cmake -DITK_DIR:PATH=/home/me/itk_build ..

Build the project:

make

and run it:

./Histogram

WINDOWS USERS PLEASE NOTE: Be sure to add the ITK bin directory to your path. This will resolve the ITK dll's at run time.

Building All of the Examples

Many of the examples in the ITK Wiki Examples Collection require VTK. You can build all of the the examples by following these instructions. If you are a new VTK user, you may want to try the Superbuild which will build a proper ITK and VTK.

ItkVtkGlue

ITK >= 4

For examples that use QuickView (which depends on VTK), you must have built ITK with Module_ITKVtkGlue=ON.

ITK < 4

Some of the ITK Examples require VTK to display the images. If you download the entire ITK Wiki Examples Collection, the ItkVtkGlue directory will be included and configured. If you wish to just build a few examples, then you will need to download ItkVtkGlue and build it. When you run cmake it will ask you to specify the location of the ItkVtkGlue binary directory.