Memory problem loading vtk and itk

Dear all,

I have a volume 549x549x700. I loaded in vtk and when i want to perform a segmentation, i export the vtk volume into a itk image but i get a memry problem.

Is there any ay to share the object and not to cerate a new one?
thanks

Have you looked at the examples, VTK->ITK and ITK->VTK? You could also look directly at itk::ImportImageFilter.

1 Like

Note that the ITKVtkGlue filters that @dzenanz points out do not require a memory copy when moving from an ITK to VTK or VTK to ITK pipeline.

Dear all,

Thanks for your quick answer. I send you the code, when i update the filter it gives me a bad allocation memory if i use all the dicoms if i only load half of them, i have no problem.

Thanks

typedef itk::Image<signed short, 3> ImageType3d;

typedef itk::VTKImageToImageFilter<ImageType3d> VTKImageToImageType;

VTKImageToImageType::Pointer vtkImageToImageFilter = VTKImageToImageType::New();
vtkImageToImageFilter->SetInput(dcm_reader_->GetOutput());
dcm_reader_->SetDataScalarTypeToShort();
dcm_reader_->Update();

vtkSmartPointer< vtkImageMagnitude > magnitude =
    vtkSmartPointer< vtkImageMagnitude >::New();
magnitude->SetInputConnection(dcm_reader_->GetOutputPort());
magnitude->Update();

using FilterType3d = itk::VTKImageToImageFilter< ImageType3d >;
FilterType3d::Pointer filter = FilterType3d::New();
filter->SetInput(magnitude->GetOutput());

try
{
    filter->Update();
}
catch (itk::ExceptionObject & error)
{
    std::cerr << "Error: " << error << std::endl;
    return EXIT_FAILURE;
}

In the ITK pipeline, consider using filters like itk::RGBToLuminanceImageFilter or itk::VectorMagnitudeImageFilter and call the ReleaseDataFlagOn() method. This will release memory from intermediate pipeline filters.

Dear all,

I have tried to include the
ReleaseDataFlagOn()

in all the filters, but when i reach the thresholding it throws an exception with a bad allocation memory. It only works if i do not load all the dicoms.

Thank you all

using FilterType3d = itk::VTKImageToImageFilter< ImageType3d >;
FilterType3d::Pointer filter = FilterType3d::New();
filter->SetInput(dcm_reader_->GetOutput());
filter->ReleaseDataFlagOn();

try
{
    filter->Update();
}
catch (itk::ExceptionObject & error)
{
    std::cerr << "Error: " << error << std::endl;
    return EXIT_FAILURE;
}

///////////////////////////////////////////////////////////////////////////////////////
//Giramos y, z para poner la misma orientacion que itk pq vtk te lo cambina          //
// TODO RTC: check if this is true for other dicom orientation matrix                //
///////////////////////////////////////////////////////////////////////////////////////

//ImageType3d::ConstPointer myitkImage = filter->GetOutput();

itk::FixedArray<bool, 3> flipAxes;
flipAxes[0] = false;
flipAxes[1] = true;
flipAxes[2] = true;

typedef itk::FlipImageFilter <ImageType3d>
    FlipImageFilterType;

FlipImageFilterType::Pointer flipFilter
    = FlipImageFilterType::New();
flipFilter->SetInput(filter->GetOutput());
flipFilter->SetFlipAxes(flipAxes);
flipFilter->ReleaseDataFlagOn();

////////////////////////////////
/// hacemos un thresholding  ///
////////////////////////////////
//Obligatorio inside 255 y fuera 0 o no funciona los connected components
using FilterTypeThresh = itk::BinaryThresholdImageFilter< ImageType3d, ImageType3d >;
FilterTypeThresh::Pointer thresholding = FilterTypeThresh::New();
thresholding->SetInput(flipFilter->GetOutput());
thresholding->SetLowerThreshold(0);
thresholding->SetUpperThreshold(500);
thresholding->SetOutsideValue(0);
thresholding->SetInsideValue(255);
thresholding->ReleaseDataFlagOn();
thresholding->Update();

Another mechanism to reduce memory usage is with streaming. Here is an example:

https://itk.org/ITKExamples/src/Core/Common/StreamAPipeline/Documentation.html

1 Like

Dear all,

I have tried the StreamingImageFilter but i still have this error when i apply a threshold:

Description: Failed to allocate memory for image.

Am i using it well? I have tried SetNumberOfStreamDivisionsfrom 10 to 1000

Thanks

//Obligatorio inside 255 y fuera 0 o no funciona los connected components
using FilterTypeThresh = itk::BinaryThresholdImageFilter< ImageType3d, ImageType3d >;
FilterTypeThresh::Pointer thresholding = FilterTypeThresh::New();
thresholding->SetInput(flipFilter->GetOutput());
thresholding->SetLowerThreshold(0);
thresholding->SetUpperThreshold(500);
thresholding->SetOutsideValue(0);
thresholding->SetInsideValue(255);
//thresholding->Update();

using StreamingFilterType = itk::StreamingImageFilter< ImageType3d, ImageType3d >;
StreamingFilterType::Pointer streamingFilter = StreamingFilterType::New();
streamingFilter->SetInput(thresholding->GetOutput());
streamingFilter->SetNumberOfStreamDivisions(10000);

try
{
    streamingFilter->Update();
}
catch (itk::ExceptionObject & error)
{
    std::cerr << "Error: " << error << std::endl;
    return EXIT_FAILURE;
}

@zandarina yes, what you shared looks correct – the Update() call should be removed from intermediate steps.

The itk::MemoryProbe can be used to help debug memory usage:

https://itk.org/ITKExamples/src/Core/Common/TraceMemoryBetweenPoints/Documentation.html

Thank you i will debug it with memoryprobe. I am using 32 bits. I will also try it in 64.

Thanks again

That is your problem! Your image has 200M pixels, and with a few copies around you are exhausting 32-bit user address space (about 1.2 GB).

Also note that it is hard to allocate a single big (relative to address space size) chunk of memory at once. If I remember correctly, the most I was able to allocate at once on Windows was maybe 700-800 MB. This is due to fragmentation of free memory space.

1 Like

Hello. I will change to 64, then. Thank you very much !!!