ITK/Examples/Statistics/Histogram: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
No edit summary
m (Fixing bug (double assignment))
Line 40: Line 40:
   index[0] = 0;
   index[0] = 0;
   index[1] = 2;
   index[1] = 2;
   index[1] = 4;
   index[2] = 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;

Revision as of 16:37, 30 August 2017

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[2] = 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, resultingIndex);
 
 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.