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()
{
  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>;

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

  using SampleType = itk::Statistics::ImageToListSampleAdaptor<ArrayImageType>;
  auto 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#

template<typename TImage>
class ImageToListSampleAdaptor : public itk::Statistics::ListSample<MeasurementVectorPixelTraits<TImage::PixelType>::MeasurementVectorType>

This class provides ListSample interface to ITK Image.

After calling SetImage( const Image * ) method to plug in the image object, users can use Sample interfaces to access Image data. The resulting data are a list of measurement vectors.

The measurement vector type is determined from the image pixel type. This class handles images with scalar, fixed array or variable length vector pixel types.

See

Sample, ListSample

ITK Sphinx Examples:

See itk::Statistics::ImageToListSampleAdaptor for additional documentation.