# Reading and extracting images from a large DICOM file

**URL:** <https://discourse.itk.org/t/reading-and-extracting-images-from-a-large-dicom-file/6292>\
**Category:** Beginner Questions\
**Created:** [November 8, 2023, 3:09pm UTC](https://discourse.itk.org/t/reading-and-extracting-images-from-a-large-dicom-file/6292 "2023-11-08T15:09:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Viktor\_Ustynov](https://discourse.itk.org/user_avatar/discourse.itk.org/viktor_ustynov/32/3758_2.png) [@Viktor\_Ustynov](https://discourse.itk.org/u/Viktor_Ustynov)\
**Post date:** [November 8, 2023, 3:09pm UTC](https://discourse.itk.org/t/reading-and-extracting-images-from-a-large-dicom-file/6292/1 "2023-11-08T15:09:16Z")

</div>

Hi everyone,  
I’m a beginner with ITK.  
I need to extract all the images from a very large (about 3 GB) .dcm file and save them as .bmp files.  
To do this, I’m trying to read the file as shown in the examples. However, the program is killed by the system during the  
reader-\>Update()  
operation, since this requires a very large amount of memory.  
Are there any C++ examples of how I can read such files without completely loading the entire DICOM file into the reader object ?  
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:** [November 8, 2023, 3:26pm UTC](https://discourse.itk.org/t/reading-and-extracting-images-from-a-large-dicom-file/6292/2 "2023-11-08T15:26:36Z")

</div>

Have you tried turning on virtual memory (swap file)? Maybe @mihail.isakov has some DICOM-specific suggestions?

---

<div class="post-metadata">

**Author:** ![Viktor\_Ustynov](https://discourse.itk.org/user_avatar/discourse.itk.org/viktor_ustynov/32/3758_2.png) [@Viktor\_Ustynov](https://discourse.itk.org/u/Viktor_Ustynov)\
**Post date:** [November 8, 2023, 4:47pm UTC](https://discourse.itk.org/t/reading-and-extracting-images-from-a-large-dicom-file/6292/3 "2023-11-08T16:47:58Z")

</div>

hi @dzenanz Thank you for feedback.  
My question is not how to prevent the killing of application.  
The question is how to read the data in a way that requires less memory.  
What it looks now. The more images there are inside one DICOM file, the more memory is required. The question is whether it is possible to process (to read) this data like a stream, such that previously read and converted images are removed from memory.

---

<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:** [November 8, 2023, 4:56pm UTC](https://discourse.itk.org/t/reading-and-extracting-images-from-a-large-dicom-file/6292/4 "2023-11-08T16:56:47Z")

</div>

You could take a look at [ProcessImageChunks](https://github.com/OpenImaging/demos/tree/master/ProcessImageChunks) example. I don’t think that DICOM IO in ITK supports streaming. For file formats which do, take a look at [StreamingImageIOBase](https://itk.org/Doxygen49/html/classitk_1_1StreamingImageIOBase.html)’s inheritance diagram. Additionally, MetaIO supports streaming for uncompressed images.

Conclusion: I don’t think you can partially read DICOMs using ITK. You **might** be able to do that using DICOM-specific functionality.

---

<div class="post-metadata">

**Author:** ![Stephen\_Aylward](https://discourse.itk.org/user_avatar/discourse.itk.org/stephen_aylward/32/654_2.png) [@Stephen\_Aylward](https://discourse.itk.org/u/Stephen_Aylward)\
**Post date:** [November 8, 2023, 5:10pm UTC](https://discourse.itk.org/t/reading-and-extracting-images-from-a-large-dicom-file/6292/5 "2023-11-08T17:10:24Z")

</div>

1. It might be possible to stream into an ITK filter. For example, the gdcm stream read write test:  
[https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/IO/GDCM/test/itkGDCMSeriesStreamReadImageWriteTest.cxx](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/IO/GDCM/test/itkGDCMSeriesStreamReadImageWriteTest.cxx)  
suggests that it might be possible.

Perhaps try running that test on your file. The command-line for running that test is given in the cmakelists.txt file in the test directory

> <https://github.com/InsightSoftwareConsortium/ITK/blob/f73cd98faa1bc764c12a84e2b07212edd48d8cf6/Modules/IO/GDCM/test/CMakeLists.txt#L112C1-L127C5>

If that works, perhaps look at other streaming examples.

1. Another option, as suggested by @dzenanz, is to access GDCM directly.

This post might help with that…(remove the loop if your data isn’t multi-frame)…

> <https://stackoverflow.com/questions/45193995/how-to-read-multi-frame-dicom-files-using-gdcm>

---

<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:** [November 8, 2023, 5:27pm UTC](https://discourse.itk.org/t/reading-and-extracting-images-from-a-large-dicom-file/6292/6 "2023-11-08T17:27:19Z")

</div>

If GDCM supports streaming, you could combine [ProcessImageChunks](https://github.com/OpenImaging/demos/tree/master/ProcessImageChunks) with [this example](https://examples.itk.org/src/io/imagebase/generateslicesfromvolume/documentation).

---

<div class="post-metadata">

**Author:** ![Viktor\_Ustynov](https://discourse.itk.org/user_avatar/discourse.itk.org/viktor_ustynov/32/3758_2.png) [@Viktor\_Ustynov](https://discourse.itk.org/u/Viktor_Ustynov)\
**Post date:** [November 8, 2023, 6:00pm UTC](https://discourse.itk.org/t/reading-and-extracting-images-from-a-large-dicom-file/6292/7 "2023-11-08T18:00:16Z")

</div>

Thank you guys!  
I’ll investigate your advice and examples tomorrow.
