# Reading image series

**URL:** https://discourse.itk.org/t/reading-image-series/2867
**Category:** Beginner Questions
**Created:** [March 22, 2020, 9:02pm UTC](https://discourse.itk.org/t/reading-image-series/2867 "2020-03-22T21:02:22Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![basal](https://discourse.itk.org/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@basal](https://discourse.itk.org/u/basal)
#### Post date: [March 22, 2020, 9:02pm UTC](https://discourse.itk.org/t/reading-image-series/2867/1 "2020-03-22T21:02:22Z")

</div>

Hi all, How can I read images series in ITK which are in NRRD format ?

---

<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: [March 23, 2020, 2:05pm UTC](https://discourse.itk.org/t/reading-image-series/2867/2 "2020-03-23T14:05:05Z")

</div>

Hello @basal,

In C++, you can use `itk::ImageSeriesReader`. Here is an example:

[https://itk.org/ITKExamples/src/IO/ImageBase/Creade3DFromSeriesOf2D/Documentation.html?highlight=imageseriesreader](https://itk.org/ITKExamples/src/IO/ImageBase/Creade3DFromSeriesOf2D/Documentation.html?highlight=imageseriesreader)

In Python, you can just past a list of filenames to `itk.imread`.

```auto
files = ['1.nrrd', '2.nrrd', '3.nrrd']
image= itk.imread(files)

```

HTH,  
Matt

---

<div class="post-metadata">

### Author: ![basal](https://discourse.itk.org/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@basal](https://discourse.itk.org/u/basal)
#### Post date: [March 24, 2020, 1:57pm UTC](https://discourse.itk.org/t/reading-image-series/2867/3 "2020-03-24T13:57:32Z")

</div>

Hi Matt,  
Thank you for your reply, do you know how I can define the argument values? My images are output000.nrrd till output048.nrrd, I mean I have 49 slices (2D images) and need to get 3D volume or 3D image of them. For the c++ code that you kindly sent, I inserted the argument values like this:  
output000.nrrd 0 48 3Dout.nrrd , but it doesn’t work.  
Could you please help me about defining the input for this code?

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: [March 24, 2020, 2:45pm UTC](https://discourse.itk.org/t/reading-image-series/2867/4 "2020-03-24T14:45:57Z")

</div>

Try argument using `printf`-like syntax, such as: `output%3d.nrrd 0 48 3Dout.nrrd`

---

<div class="post-metadata">

### Author: ![basal](https://discourse.itk.org/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@basal](https://discourse.itk.org/u/basal)
#### Post date: [March 24, 2020, 4:03pm UTC](https://discourse.itk.org/t/reading-image-series/2867/5 "2020-03-24T16:03:10Z")

</div>

Thanks for the reply, actually, I changed this line nameGenerator-\>SetSeriesFormat(argv[1]); to  
std::string format = argv[1];  
format += “%03d.”;  
format += argv[2]; // filename extension  
nameGenerator-\>SetSeriesFormat(format.c\_str());

and put the argument values :  
**output nrrd 0 48 3Dout.nrrd**  
and it works.  
Now, my problem is that the size of the output image in coronal and sagittal plans are different from the ones which are in main 3D image. Do you have any idea to fix it?  
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: [March 24, 2020, 4:05pm UTC](https://discourse.itk.org/t/reading-image-series/2867/6 "2020-03-24T16:05:11Z")

</div>

Select only a subset of slices, e.g. all coronal slices -\> coronal3Dimage.

---

<div class="post-metadata">

### Author: ![basal](https://discourse.itk.org/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@basal](https://discourse.itk.org/u/basal)
#### Post date: [March 24, 2020, 4:09pm UTC](https://discourse.itk.org/t/reading-image-series/2867/7 "2020-03-24T16:09:46Z")

</div>

Actually, I do not understand what you mean exactly. Can you explain more? In addition, I need to view all planes in 3D slicer.

---

<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: [March 24, 2020, 4:13pm UTC](https://discourse.itk.org/t/reading-image-series/2867/8 "2020-03-24T16:13:03Z")

</div>

No matter how you read the image, once you have a 3D image you can view all planes in 3D Slicer. If all your input slices are in same orientation (e.g. axial), there is no problem. Otherwise, slices e.g. 0…10 make an axial image, and e.g. 11…47 make coronal.

---

<div class="post-metadata">

### Author: ![basal](https://discourse.itk.org/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@basal](https://discourse.itk.org/u/basal)
#### Post date: [March 24, 2020, 4:19pm UTC](https://discourse.itk.org/t/reading-image-series/2867/9 "2020-03-24T16:19:20Z")

</div>

yes, all of my slices are in the axial plane. But still, I am getting different image sizes for sagittal and coronal plans.

---

<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: [March 24, 2020, 7:55pm UTC](https://discourse.itk.org/t/reading-image-series/2867/10 "2020-03-24T19:55:21Z")

</div>

I don’t understand what is the problem, please explain it in more detail, possibly with images/screenshots.

---

<div class="post-metadata">

### Author: ![basal](https://discourse.itk.org/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@basal](https://discourse.itk.org/u/basal)
#### Post date: [March 24, 2020, 8:07pm UTC](https://discourse.itk.org/t/reading-image-series/2867/11 "2020-03-24T20:07:37Z")

</div>

[Pictures.rar](https://discourse.itk.org/uploads/short-url/be3mcurbngq1yiv8ljqacRMpTqv.rar) (674.1 KB)

---

<div class="post-metadata">

### Author: ![basal](https://discourse.itk.org/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@basal](https://discourse.itk.org/u/basal)
#### Post date: [March 24, 2020, 8:08pm UTC](https://discourse.itk.org/t/reading-image-series/2867/12 "2020-03-24T20:08:57Z")

</div>

I sent you the images, one of them is the result of the code and other is the main image. they should be in the same size in all planes but they are in the same size just in axial view.

---

<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: [March 24, 2020, 8:34pm UTC](https://discourse.itk.org/t/reading-image-series/2867/13 "2020-03-24T20:34:19Z")

</div>

The spacing along axial direction is not 1.0, but larger. A series of `.nrrd` slices probably does not encode 3D spatial position like a series of DICOM slices, so you have to know ‘Z’ spacing and set it manually in code after reading the image, e.g. `image->GetSpacing()[2]=4.4;`.

---

<div class="post-metadata">

### Author: ![basal](https://discourse.itk.org/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@basal](https://discourse.itk.org/u/basal)
#### Post date: [March 25, 2020, 4:01am UTC](https://discourse.itk.org/t/reading-image-series/2867/14 "2020-03-25T04:01:57Z")

</div>

Thank you very much for your answer. Both origin and spacing in Z direction are different. So, I wrote the following code, but it gives me a debug error. Can you take a look at the code and kindly let me know what the problem is?

```auto
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
// Software Guide : EndCodeSnippet
#include "itkImage.h"
#include "itkVersor.h"
#include "itkChangeInformationImageFilter.h"
int main(int argc, char ** argv)
{
	// Verify the number of parameters in the command line
	if (argc < 3)
	{
		std::cerr << "Usage: " << std::endl;
		std::cerr << argv[0] << " inputImageFile outputImageFile " << std::endl;
		return EXIT_FAILURE;
	}
	using PixelType = unsigned short;
	constexpr unsigned int Dimension = 3;
	
	using ImageType = itk::Image< PixelType, Dimension >;
	using ReaderType = itk::ImageFileReader< ImageType >;
	using WriterType = itk::ImageFileWriter< ImageType >;

	ReaderType::Pointer reader = ReaderType::New();
	WriterType::Pointer writer = WriterType::New();
	const char * inputFilename = argv[1];
	const char * outputFilename = argv[2];
	using InputImageType = itk::Image<PixelType, Dimension>;
	ImageType::Pointer image = ImageType::New();
	reader->Update();
	reader->SetFileName(inputFilename);
	writer->SetFileName(outputFilename);
	ImageType::ConstPointer inputImage = reader->GetOutput();
	
	using FilterType = itk::ChangeInformationImageFilter<ImageType>;
	FilterType::Pointer filter = FilterType::New();
	filter->SetInput(reader->GetOutput());
	ImageType::PointType::VectorType spacing;
	spacing[2] = 3;
	filter->SetOutputSpacing(spacing);
	filter->ChangeSpacingOn();

	ImageType::PointType origin = inputImage->GetOrigin();
	origin[2]= -667;
	filter->SetOutputOrigin(origin);
	filter->ChangeOriginOn();
	writer->SetInput(filter->GetOutput());
	try
	{
		writer->Update();
	}
	catch (itk::ExceptionObject & err)
	{
		std::cerr << "ExceptionObject caught !" << std::endl;
		std::cerr << err << 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: [March 25, 2020, 1:53pm UTC](https://discourse.itk.org/t/reading-image-series/2867/15 "2020-03-25T13:53:33Z")

</div>

`reader->SetFileName(inputFilename);` must come before `reader->Update();`.

---

<div class="post-metadata">

### Author: ![basal](https://discourse.itk.org/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@basal](https://discourse.itk.org/u/basal)
#### Post date: [March 26, 2020, 12:26pm UTC](https://discourse.itk.org/t/reading-image-series/2867/16 "2020-03-26T12:26:44Z")

</div>

Thanks. I changed it, now the output in 3D slicer is black. Do you any idea? Is the way I used for changing origin and spacing correct?

---

<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: [March 26, 2020, 12:51pm UTC](https://discourse.itk.org/t/reading-image-series/2867/17 "2020-03-26T12:51:52Z")

</div>

You might need to change your pixel type `using PixelType = unsigned short;` to something bigger or signed.
