# How to process CT images one by one and save these images of one series in memory?

**URL:** https://discourse.itk.org/t/how-to-process-ct-images-one-by-one-and-save-these-images-of-one-series-in-memory/3489
**Category:** Uncategorized
**Tags:** registration, dicom
**Created:** [September 28, 2020, 2:02pm UTC](https://discourse.itk.org/t/how-to-process-ct-images-one-by-one-and-save-these-images-of-one-series-in-memory/3489 "2020-09-28T14:02:32Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![wulicheng](https://discourse.itk.org/user_avatar/discourse.itk.org/wulicheng/32/1390_2.png) [@wulicheng](https://discourse.itk.org/u/wulicheng)
#### Post date: [September 28, 2020, 2:02pm UTC](https://discourse.itk.org/t/how-to-process-ct-images-one-by-one-and-save-these-images-of-one-series-in-memory/3489/1 "2020-09-28T14:02:33Z")

</div>

I have a series of CT images, now I want to process these images one by one, and save these images processed in memory as a 3D data? Then , I can convert this ITK 3D data to VTK image and visual.

---

<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: [September 28, 2020, 3:13pm UTC](https://discourse.itk.org/t/how-to-process-ct-images-one-by-one-and-save-these-images-of-one-series-in-memory/3489/2 "2020-09-28T15:13:06Z")

</div>

Maybe [read your series](https://itk.org/ITKExamples/src/IO/GDCM/ReadDICOMSeriesAndWrite3DImage/Documentation.html) as a 3D image and use [SliceBySliceImageFilter](https://itk.org/Doxygen/html/classitk_1_1SliceBySliceImageFilter.html)?

Alternatively use a for loop to read and process your images, and use [paste filter](https://itk.org/ITKExamples/src/Filtering/ImageGrid/PasteImageIntoAnotherOne/Documentation.html) to assemble them into a 3D volume.

But if you have a 3D image, it is usually desirable to process it as a 3D image rather than a series of 2D slices (that is called 2.5D approach).

---

<div class="post-metadata">

### Author: ![wulicheng](https://discourse.itk.org/user_avatar/discourse.itk.org/wulicheng/32/1390_2.png) [@wulicheng](https://discourse.itk.org/u/wulicheng)
#### Post date: [September 28, 2020, 8:26pm UTC](https://discourse.itk.org/t/how-to-process-ct-images-one-by-one-and-save-these-images-of-one-series-in-memory/3489/3 "2020-09-28T20:26:16Z")

</div>

OKAY, i Will try! thanks

---

<div class="post-metadata">

### Author: ![wulicheng](https://discourse.itk.org/user_avatar/discourse.itk.org/wulicheng/32/1390_2.png) [@wulicheng](https://discourse.itk.org/u/wulicheng)
#### Post date: [September 30, 2020, 8:05am UTC](https://discourse.itk.org/t/how-to-process-ct-images-one-by-one-and-save-these-images-of-one-series-in-memory/3489/4 "2020-09-30T08:05:52Z")

</div>

Sorry, I have try the “past filter”，but failed! Then I write processed dicom image as another dicom file,But always 3k Byte losed, and I can’t reconstruct again(use RadiAnt DICOM Viewer ), I thouth the header infomation losed! I also tried the ITK demo “DicomImageReadWrite” and “DicomSeriesReadSeriesWrite”,any suggesitions please!

---

<div class="post-metadata">

### Author: ![wulicheng](https://discourse.itk.org/user_avatar/discourse.itk.org/wulicheng/32/1390_2.png) [@wulicheng](https://discourse.itk.org/u/wulicheng)
#### Post date: [September 30, 2020, 8:24am UTC](https://discourse.itk.org/t/how-to-process-ct-images-one-by-one-and-save-these-images-of-one-series-in-memory/3489/5 "2020-09-30T08:24:36Z")

</div>

Hello the code like flow:

```cpp
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkGDCMImageIO.h"

#include <list>
#include <fstream>

int main(int argc, char* argv[])
{
	typedef signed short InputPixelType;
	const unsigned int InputDimension = 2;
	typedef itk::Image< InputPixelType, InputDimension > InputImageType;

	typedef itk::ImageFileReader< InputImageType > ReaderType;
	ReaderType::Pointer reader = ReaderType::New();
	reader->SetFileName("C:/input/lung.dcm");

	typedef itk::GDCMImageIO ImageIOType;
	ImageIOType::Pointer gdcmImageIO = ImageIOType::New();
	reader->SetImageIO(gdcmImageIO);

	try
	{
		reader->Update();
	}
	catch (itk::ExceptionObject & e)
	{
		std::cerr << "exception in file reader " << std::endl;
		std::cerr << e << std::endl;
		return EXIT_FAILURE;
	}

	typedef itk::ImageFileWriter< InputImageType > Writer1Type;
	Writer1Type::Pointer writer = Writer1Type::New();
	writer->SetFileName("C:/output/lung.dcm");
	writer->SetInput(reader->GetOutput());

	try
	{
		writer->Update();
	}
	catch (itk::ExceptionObject & e)
	{
		std::cerr << "exception in file writer " << std::endl;
		std::cerr << e << std::endl;
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;
}

```

---

<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: [September 30, 2020, 1:27pm UTC](https://discourse.itk.org/t/how-to-process-ct-images-one-by-one-and-save-these-images-of-one-series-in-memory/3489/6 "2020-09-30T13:27:44Z")

</div>

You may need something like `writer->SetMetaDataDictionary(reader->GetMetaDataDictionary());`. Maybe look at this [DicomSeriesReadSeriesWrite example](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Examples/IO/DicomSeriesReadSeriesWrite.cxx#L277-L293).

Also it is desirable to use backticks for [code formatting](https://discourse.stonehearth.net/t/discourse-guide-code-formatting/30587).

---

<div class="post-metadata">

### Author: ![wulicheng](https://discourse.itk.org/user_avatar/discourse.itk.org/wulicheng/32/1390_2.png) [@wulicheng](https://discourse.itk.org/u/wulicheng)
#### Post date: [October 9, 2020, 3:45am UTC](https://discourse.itk.org/t/how-to-process-ct-images-one-by-one-and-save-these-images-of-one-series-in-memory/3489/7 "2020-10-09T03:45:13Z")

</div>

I have run the code ,It is ok, But , I can’t process Image one by one！！！ Any suggestions?

---

<div class="post-metadata">

### Author: ![wulicheng](https://discourse.itk.org/user_avatar/discourse.itk.org/wulicheng/32/1390_2.png) [@wulicheng](https://discourse.itk.org/u/wulicheng)
#### Post date: [October 9, 2020, 6:31am UTC](https://discourse.itk.org/t/how-to-process-ct-images-one-by-one-and-save-these-images-of-one-series-in-memory/3489/8 "2020-10-09T06:31:11Z")

</div>

I want to process dicom images of one frame of a series.

---

<div class="post-metadata">

### Author: ![wulicheng](https://discourse.itk.org/user_avatar/discourse.itk.org/wulicheng/32/1390_2.png) [@wulicheng](https://discourse.itk.org/u/wulicheng)
#### Post date: [October 9, 2020, 8:20am UTC](https://discourse.itk.org/t/how-to-process-ct-images-one-by-one-and-save-these-images-of-one-series-in-memory/3489/9 "2020-10-09T08:20:44Z")

</div>

![图片](https://discourse.itk.org/uploads/default/original/2X/b/b6a4c9d118b8579066234155f8fb54b46a6201e1.png)  
 ![图片](https://discourse.itk.org/uploads/default/original/2X/b/bc45ac930f2a4621a326c5214b38672fef304fb9.png)  
I found the “Study Instance UID” and “Series Instance UID” has changed!  
But I don’t know why?

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [October 9, 2020, 1:37pm UTC](https://discourse.itk.org/t/how-to-process-ct-images-one-by-one-and-save-these-images-of-one-series-in-memory/3489/10 "2020-10-09T13:37:08Z")

</div>

Hello @wulicheng,

This is actually a good thing. You processed the images and when you save them they should be to a different series, likely with the same study number.

If you still feel you want to retain the study and series numbers you will need to explicitly set the writer to use GDCMImageIO, set the series/study number to what you want in the meta-data dictionary and then configure the GDCMImageIO to use these by calling the [KeepOriginalUIDOn](https://itk.org/Doxygen/html/classitk_1_1GDCMImageIO.html#ae215297a5be5792d5119940aca174514) method.

---

<div class="post-metadata">

### Author: ![mihail.isakov](https://discourse.itk.org/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@mihail.isakov](https://discourse.itk.org/u/mihail.isakov)
#### Post date: [October 9, 2020, 11:55pm UTC](https://discourse.itk.org/t/how-to-process-ct-images-one-by-one-and-save-these-images-of-one-series-in-memory/3489/11 "2020-10-09T23:55:11Z")

</div>

Doc seems to be broken, BTW

 ![Screenshot at 2020-10-10 01-53-44](https://discourse.itk.org/uploads/default/original/2X/2/27f5e36a61701e7ab76d93bfafe769864b40d2c9.png)

---

<div class="post-metadata">

### Author: ![wulicheng](https://discourse.itk.org/user_avatar/discourse.itk.org/wulicheng/32/1390_2.png) [@wulicheng](https://discourse.itk.org/u/wulicheng)
#### Post date: [October 10, 2020, 12:39am UTC](https://discourse.itk.org/t/how-to-process-ct-images-one-by-one-and-save-these-images-of-one-series-in-memory/3489/12 "2020-10-10T00:39:43Z")

</div>

thanks very much! I have got it!

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [October 10, 2020, 9:42pm UTC](https://discourse.itk.org/t/how-to-process-ct-images-one-by-one-and-save-these-images-of-one-series-in-memory/3489/13 "2020-10-10T21:42:42Z")

</div>

Hi @mihail.isakov,

Good catch, didn’t even notice that when pointing to the doxygen.

Looks like something is wrong with the doxygen generation as the [documentation in the code](https://github.com/InsightSoftwareConsortium/ITK/blob/75149ceee513ea0b3b43547af19ce250b4991646/Modules/IO/GDCM/include/itkGDCMImageIO.h#L182) is correct.

@dzenanz, any idea what’s wrong with doxygen generation?
