# Slope and intercept when reading Nifti images

**URL:** <https://discourse.itk.org/t/slope-and-intercept-when-reading-nifti-images/2122>\
**Category:** Uncategorized\
**Created:** [August 5, 2019, 2:41pm UTC](https://discourse.itk.org/t/slope-and-intercept-when-reading-nifti-images/2122 "2019-08-05T14:41:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elli](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/58f4c7/32.png) [@Elli](https://discourse.itk.org/u/Elli)\
**Post date:** [August 5, 2019, 2:41pm UTC](https://discourse.itk.org/t/slope-and-intercept-when-reading-nifti-images/2122/1 "2019-08-05T14:41:38Z")

</div>

Dear all,

I have a very basic question: I am reading in nifti images using:

ImageType::Pointer readImage(string imageName) {  
typename ReaderType::Pointer reader = ReaderType::New();  
reader-\>SetFileName(imageName);  
try {  
reader-\>Update();  
}  
catch (itk::ExceptionObject &excp) {  
std::cerr \<\< excp \<\< std::endl;  
}  
ImageType::Pointer image = reader-\>GetOutput();  
return reader-\>GetOutput();  
}

Am I correct that the ITK image reader is applying the slope and intercept automatically? Or do I have to manually multiply by the slope and add the intercept to get the correct values?

I am asking because I have some images where the results are reasonable, but I have one image where the image values are not making sense after reading it. When I apply then slope and intercept, the values are making sense… (this file can be downloaded here: [https://wetransfer.com/downloads/3aa534c3139492a4687df079bd1bde0120190805143844/4ff1ff5b3944d7bfa2b2c40c798ae40820190805143844/b82e99](https://wetransfer.com/downloads/3aa534c3139492a4687df079bd1bde0120190805143844/4ff1ff5b3944d7bfa2b2c40c798ae40820190805143844/b82e99))

Thanks a lot for your answer.

Elli

---

<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:** [August 9, 2019, 3:46am UTC](https://discourse.itk.org/t/slope-and-intercept-when-reading-nifti-images/2122/2 "2019-08-09T03:46:43Z")

</div>

Hi Elli,

Yes, rescale / slope and intercept is applied with NIFTI files per:

> <https://github.com/InsightSoftwareConsortium/ITK/blob/f162460efc347025d063634153b4dce4db6c3937/Modules/IO/NIFTI/src/itkNiftiImageIO.cxx#L714-L725>

HTH,  
Matt

---

<div class="post-metadata">

**Author:** ![Elli](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/58f4c7/32.png) [@Elli](https://discourse.itk.org/u/Elli)\
**Post date:** [August 10, 2019, 9:40am UTC](https://discourse.itk.org/t/slope-and-intercept-when-reading-nifti-images/2122/3 "2019-08-10T09:40:35Z")

</div>

Dear Matt,

thanks for your answer…

But why is it not working for this specific image ? (you can find it here:  
[https://we.tl/t-ZKbnxnTWwr](https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwe.tl%2Ft-ZKbnxnTWwr&data=02%7C01%7Ce.a.g.pfaehler%40umcg.nl%7Cf8b1b140279a4baafbd808d719b2c70f%7C335122f9d4f44d67a2fccd6dc20dde70%7C0%7C1%7C637006127943388320&sdata=EfNj0nlzHduslPzDbPeMZ9UEbzAvZCMRbQ8V7X8CEwM%3D&reserved=0)

)

All other softwares I used for comparison (Matlab, nibabel in python, etc) can read the image correctly. But when I read it with ITK, i get as maximum intensity 32767 as if the image would not have been scaled. When I scale it then manually by mutliplying with slope, I get the correct values. How can that be?

Thanks a lot for your help,

Elli

---

<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 13, 2019, 3:55pm UTC](https://discourse.itk.org/t/slope-and-intercept-when-reading-nifti-images/2122/4 "2019-08-13T15:55:02Z")

</div>

When I try to download the offending image, I get “Transfer deleted”. Has the the link expired? Can you refresh it?

---

<div class="post-metadata">

**Author:** ![Elli](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/58f4c7/32.png) [@Elli](https://discourse.itk.org/u/Elli)\
**Post date:** [August 13, 2019, 4:15pm UTC](https://discourse.itk.org/t/slope-and-intercept-when-reading-nifti-images/2122/5 "2019-08-13T16:15:25Z")

</div>

Sure, sorry. Here is a new link: [https://wetransfer.com/downloads/603bc96030ba5dfea33032524743e07b20190813161307/2586576f53a7554bfd45b001747119a320190813161307/e6f5d9](https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwetransfer.com%2Fdownloads%2F603bc96030ba5dfea33032524743e07b20190813161307%2F2586576f53a7554bfd45b001747119a320190813161307%2Fe6f5d9&data=02%7C01%7Ce.a.g.pfaehler%40umcg.nl%7C2fa53949e53e4c2ea54008d720093bc6%7C335122f9d4f44d67a2fccd6dc20dde70%7C0%7C0%7C637013096341550524&sdata=Cfwi86xcU3yOYtXjE7RavbRYcBXWjMV1axT%2Fis4TOMw%3D&reserved=0)

thanks in advance for looking into this!

---

<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 14, 2019, 2:30pm UTC](https://discourse.itk.org/t/slope-and-intercept-when-reading-nifti-images/2122/6 "2019-08-14T14:30:35Z")

</div>

The slope in this image is ignored because both SForm and QForm codes are zero:

> <https://github.com/InsightSoftwareConsortium/ITK/blob/f162460efc347025d063634153b4dce4db6c3937/Modules/IO/NIFTI/src/itkNiftiImageIO.cxx#L1210-L1224>

---

<div class="post-metadata">

**Author:** ![Elli](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/58f4c7/32.png) [@Elli](https://discourse.itk.org/u/Elli)\
**Post date:** [August 15, 2019, 11:48am UTC](https://discourse.itk.org/t/slope-and-intercept-when-reading-nifti-images/2122/7 "2019-08-15T11:48:25Z")

</div>

Hey Dzenan,

thanks! But officially that is not correct, right? Because I read that the scaling with slope and intercept should always be applied also when sform and qform are 0…

Anyhow, I can make a workaround in my code. So thanks a lot for the information!

Regards

Elli

---

<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 15, 2019, 12:45pm UTC](https://discourse.itk.org/t/slope-and-intercept-when-reading-nifti-images/2122/8 "2019-08-15T12:45:39Z")

</div>

> [@Elli](#):
>
> But officially that is not correct

Where is the official answer to this question? Also, you could dig a little through git history of that file to see when changes were made, and maybe also why.

---

<div class="post-metadata">

**Author:** ![Elli](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/58f4c7/32.png) [@Elli](https://discourse.itk.org/u/Elli)\
**Post date:** [August 15, 2019, 2:38pm UTC](https://discourse.itk.org/t/slope-and-intercept-when-reading-nifti-images/2122/9 "2019-08-15T14:38:43Z")

</div>

Thanks, thats a good idea.

Well, officially is a bit difficult to define. We just had a big discussion in our group whats the right way to handle nifti images… And what we found online in the header descriptions was that scaling should always be applied. Anyhow, I will check the history! Thank you!
