# Z spacing in Image Series Reader

**URL:** https://discourse.itk.org/t/z-spacing-in-image-series-reader/2167
**Category:** Beginner Questions
**Created:** [August 19, 2019, 9:06am UTC](https://discourse.itk.org/t/z-spacing-in-image-series-reader/2167 "2019-08-19T09:06:25Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![alkamid](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/3bc359/32.png) [@alkamid](https://discourse.itk.org/u/alkamid)
#### Post date: [August 19, 2019, 9:06am UTC](https://discourse.itk.org/t/z-spacing-in-image-series-reader/2167/1 "2019-08-19T09:06:25Z")

</div>

How is Z spacing determined from a DICOM series, when using SimpleITK’s `ImageSeriesReader`? It looks like X and Y spacing is taken from `Pixel Spacing` (`0028|0030`) field, but I have not been able to find the code for Z spacing. Is it inherited from GDCM, or does ITK have its own algorithm to calculate 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: [August 19, 2019, 1:10pm UTC](https://discourse.itk.org/t/z-spacing-in-image-series-reader/2167/2 "2019-08-19T13:10:31Z")

</div>

Hello @alkamid

The Z spacing is determined by GDCM. This is done by reading the series and looking at the Image Position Patient tag ([0020|0032](https://dicom.innolitics.com/ciods/ct-image/image-plane/00200032)) per image and then calculating the z spacing.

---

<div class="post-metadata">

### Author: ![alkamid](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/3bc359/32.png) [@alkamid](https://discourse.itk.org/u/alkamid)
#### Post date: [August 19, 2019, 2:24pm UTC](https://discourse.itk.org/t/z-spacing-in-image-series-reader/2167/3 "2019-08-19T14:24:02Z")

</div>

Thank you @zivy for the explanation. Do you happen to know where in GDCM’s source is this calculated? The simplest method I can think of would be taking the mean of Z differences between all sorted slices — is this what’s happening? Given that this is ITK forum, treat it as a bonus question! (:

---

<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: [August 19, 2019, 2:37pm UTC](https://discourse.itk.org/t/z-spacing-in-image-series-reader/2167/4 "2019-08-19T14:37:20Z")

</div>

Hello @alkamid,

Possibly [here](https://github.com/malaterre/GDCM/blob/b91700a8084a24359a1c0e0259ebdb35894e28c7/Source/MediaStorageAndFileFormat/gdcmImageHelper.cxx#L167), but as you noted this isn’t the GDCM forum, so you should probably ask there to get the authoritative answer.

---

<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: [August 19, 2019, 3:00pm UTC](https://discourse.itk.org/t/z-spacing-in-image-series-reader/2167/5 "2019-08-19T15:00:40Z")

</div>

Z spacing is calculated in [ImageSeriesReader](https://github.com/InsightSoftwareConsortium/ITK/blob/45c1147a497585dd33493407e540afa9eb0591fe/Modules/IO/ImageBase/include/itkImageSeriesReader.hxx#L159-L224). Take a look at this [commit](https://github.com/InsightSoftwareConsortium/ITK/commit/23436a04c978d176b32775e97d2701d96d6d2dd6) which updated that calculation for ITK 5.0.

---

<div class="post-metadata">

### Author: ![alkamid](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/3bc359/32.png) [@alkamid](https://discourse.itk.org/u/alkamid)
#### Post date: [August 19, 2019, 3:06pm UTC](https://discourse.itk.org/t/z-spacing-in-image-series-reader/2167/6 "2019-08-19T15:06:16Z")

</div>

Thanks a lot @dzenanz, this is exactly what I was looking for!

---

<div class="post-metadata">

### Author: ![alkamid](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/3bc359/32.png) [@alkamid](https://discourse.itk.org/u/alkamid)
#### Post date: [August 20, 2019, 7:26am UTC](https://discourse.itk.org/t/z-spacing-in-image-series-reader/2167/7 "2019-08-20T07:26:19Z")

</div>

@dzenanz, do I read the code correctly, and there is a default value of Z spacing = 1 if it’s not specified in DICOM metadata? Why is this the case?

```auto
    if ( Math::AlmostEquals(dirNnorm, 0.0) )
      {
      spacing[this->m_NumberOfDimensionsInImage] = 1.0;
      }

```

---

<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: [August 20, 2019, 1:50pm UTC](https://discourse.itk.org/t/z-spacing-in-image-series-reader/2167/8 "2019-08-20T13:50:05Z")

</div>

If just one image is read, having its thickness be 0.0 is more wrong than having it be 1.0. A choice which was probably made long ago, and now that is backwards compatible behavior.

---

<div class="post-metadata">

### Author: ![alkamid](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/3bc359/32.png) [@alkamid](https://discourse.itk.org/u/alkamid)
#### Post date: [August 21, 2019, 12:11pm UTC](https://discourse.itk.org/t/z-spacing-in-image-series-reader/2167/9 "2019-08-21T12:11:55Z")

</div>

Is this fallback only activated when one image is read? Could we have a series with wrong Z spacing which would result in a 3D volume with 1.0 spacing?

---

<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: [August 21, 2019, 12:59pm UTC](https://discourse.itk.org/t/z-spacing-in-image-series-reader/2167/10 "2019-08-21T12:59:35Z")

</div>

That will be zero only if the last slice has the same position as the first slice:

> <https://github.com/InsightSoftwareConsortium/ITK/blob/45c1147a497585dd33493407e540afa9eb0591fe/Modules/IO/ImageBase/include/itkImageSeriesReader.hxx#L201-L205>

  
Which means there is just one slice, or the positions (image position patient tag) are junk.
