Create List Of Samples From Image Without Duplication

Synopsis

Create a list of samples from an image without duplicating the data.

Results

Output:

[96.6165]
[833.994]
[935.002]
[571.885]
[663.087]
[496.426]
[429.681]
[649.809]
[333.22]
[425.118]
[965.227]
[568.819]
[133.191]
[547.472]
[361.405]
[136.599]
[823.276]
[802.849]
[475.275]
[947.74]
[96.6244]
[965.532]
[690.606]
[13.2403]
[529.497]
[258.332]
[780.933]
[135.776]
[985.543]
[23.581]
[325.735]
[623.222]
[485.055]
...

Code

C++

#include "itkImageToListSampleAdaptor.h"
#include "itkImage.h"
#include "itkRandomImageSource.h"
#include "itkComposeImageFilter.h"

int
main(int, char *[])
{
  using FloatImage2DType = itk::Image<float, 2>;

  itk::RandomImageSource<FloatImage2DType>::Pointer random;
  random = itk::RandomImageSource<FloatImage2DType>::New();

  random->SetMin(0.0);
  random->SetMax(1000.0);

  using SpacingValueType = FloatImage2DType::SpacingValueType;
  using SizeValueType = FloatImage2DType::SizeValueType;
  using PointValueType = FloatImage2DType::PointValueType;

  SizeValueType size[2] = { 20, 20 };
  random->SetSize(size);

  SpacingValueType spacing[2] = { 0.7, 2.1 };
  random->SetSpacing(spacing);

  PointValueType origin[2] = { 15, 400 };
  random->SetOrigin(origin);

  using MeasurementVectorType = itk::FixedArray<float, 1>;
  using ArrayImageType = itk::Image<MeasurementVectorType, 2>;
  using CasterType = itk::ComposeImageFilter<FloatImage2DType, ArrayImageType>;

  CasterType::Pointer caster = CasterType::New();
  caster->SetInput(random->GetOutput());
  caster->Update();

  using SampleType = itk::Statistics::ImageToListSampleAdaptor<ArrayImageType>;
  SampleType::Pointer sample = SampleType::New();

  sample->SetImage(caster->GetOutput());

  SampleType::Iterator iter = sample->Begin();

  while (iter != sample->End())
  {
    std::cout << iter.GetMeasurementVector() << std::endl;
    ++iter;
  }

  return EXIT_SUCCESS;
}

Classes demonstrated

Warning

doxygenclass: Cannot find class “itk::ImageToListSampleAdaptor” in doxygen xml output for project “ITK” from directory: /home/kitware/Dashboards/Tests/ITKExamples-doc-build/ITKEx-build/ITKDoxygenXML

See itk::ImageToListSampleAdaptor for additional documentation.