# Memory problem loading vtk and itk

**URL:** https://discourse.itk.org/t/memory-problem-loading-vtk-and-itk/1722
**Category:** Uncategorized
**Created:** [April 1, 2019, 8:04am UTC](https://discourse.itk.org/t/memory-problem-loading-vtk-and-itk/1722 "2019-04-01T08:04:29Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![zandarina](https://discourse.itk.org/letter_avatar_proxy/v4/letter/z/4af34b/32.png) [@zandarina](https://discourse.itk.org/u/zandarina)
#### Post date: [April 1, 2019, 8:04am UTC](https://discourse.itk.org/t/memory-problem-loading-vtk-and-itk/1722/1 "2019-04-01T08:04:29Z")

</div>

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

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 1, 2019, 3:39pm UTC](https://discourse.itk.org/t/memory-problem-loading-vtk-and-itk/1722/2 "2019-04-01T15:39:25Z")

</div>

Have you looked at the examples, [VTK-\>ITK](https://itk.org/ITKExamples/src/Bridge/VtkGlue/ConvertvtkImageDataToAnitkImage/Documentation.html) and [ITK-\>VTK](https://itk.org/ITKExamples/src/Bridge/VtkGlue/ConvertAnitkImageTovtkImageData/Documentation.html)? You could also look directly at [`itk::ImportImageFilter`](https://itk.org/ITKExamples/src/Core/Common/ImportPixelBufferIntoAnImage/Documentation.html).

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [April 1, 2019, 4:35pm UTC](https://discourse.itk.org/t/memory-problem-loading-vtk-and-itk/1722/3 "2019-04-01T16:35:11Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![zandarina](https://discourse.itk.org/letter_avatar_proxy/v4/letter/z/4af34b/32.png) [@zandarina](https://discourse.itk.org/u/zandarina)
#### Post date: [April 2, 2019, 8:40am UTC](https://discourse.itk.org/t/memory-problem-loading-vtk-and-itk/1722/4 "2019-04-02T08:40:20Z")

</div>

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;
}
```

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [April 2, 2019, 1:13pm UTC](https://discourse.itk.org/t/memory-problem-loading-vtk-and-itk/1722/5 "2019-04-02T13:13:34Z")

</div>

In the ITK pipeline, consider using filters like [itk::RGBToLuminanceImageFilter](https://itk.org/Doxygen/html/classitk_1_1RGBToLuminanceImageFilter.html) or [itk::VectorMagnitudeImageFilter](https://public.kitware.com/Insight/Doxygen/html/classitk_1_1VectorMagnitudeImageFilter.html) and call the [ReleaseDataFlagOn()](https://public.kitware.com/Insight/Doxygen/html/classitk_1_1ProcessObject.html#ae732e9163879ae559ccd0b5957141f97) method. This will release memory from intermediate pipeline filters.

---

<div class="post-metadata">

### Author: ![zandarina](https://discourse.itk.org/letter_avatar_proxy/v4/letter/z/4af34b/32.png) [@zandarina](https://discourse.itk.org/u/zandarina)
#### Post date: [April 2, 2019, 5:33pm UTC](https://discourse.itk.org/t/memory-problem-loading-vtk-and-itk/1722/6 "2019-04-02T17:33:20Z")

</div>

Dear all,

I have tried to include the  
[ReleaseDataFlagOn()](https://public.kitware.com/Insight/Doxygen/html/classitk_1_1ProcessObject.html#ae732e9163879ae559ccd0b5957141f97)

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

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [April 2, 2019, 8:21pm UTC](https://discourse.itk.org/t/memory-problem-loading-vtk-and-itk/1722/7 "2019-04-02T20:21:33Z")

</div>

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

[https://itk.org/ITKExamples/src/Core/Common/StreamAPipeline/Documentation.html](https://itk.org/ITKExamples/src/Core/Common/StreamAPipeline/Documentation.html)

---

<div class="post-metadata">

### Author: ![zandarina](https://discourse.itk.org/letter_avatar_proxy/v4/letter/z/4af34b/32.png) [@zandarina](https://discourse.itk.org/u/zandarina)
#### Post date: [April 3, 2019, 12:36pm UTC](https://discourse.itk.org/t/memory-problem-loading-vtk-and-itk/1722/8 "2019-04-03T12:36:26Z")

</div>

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;
}
```

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [April 3, 2019, 1:06pm UTC](https://discourse.itk.org/t/memory-problem-loading-vtk-and-itk/1722/9 "2019-04-03T13:06:57Z")

</div>

@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](https://itk.org/ITKExamples/src/Core/Common/TraceMemoryBetweenPoints/Documentation.html)

---

<div class="post-metadata">

### Author: ![zandarina](https://discourse.itk.org/letter_avatar_proxy/v4/letter/z/4af34b/32.png) [@zandarina](https://discourse.itk.org/u/zandarina)
#### Post date: [April 3, 2019, 7:51pm UTC](https://discourse.itk.org/t/memory-problem-loading-vtk-and-itk/1722/10 "2019-04-03T19:51:59Z")

</div>

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

Thanks again

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 3, 2019, 8:31pm UTC](https://discourse.itk.org/t/memory-problem-loading-vtk-and-itk/1722/11 "2019-04-03T20:31:09Z")

</div>

> [@zandarina](#):
>
> I am using 32 bits

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.

---

<div class="post-metadata">

### Author: ![zandarina](https://discourse.itk.org/letter_avatar_proxy/v4/letter/z/4af34b/32.png) [@zandarina](https://discourse.itk.org/u/zandarina)
#### Post date: [April 3, 2019, 9:18pm UTC](https://discourse.itk.org/t/memory-problem-loading-vtk-and-itk/1722/12 "2019-04-03T21:18:53Z")

</div>

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