0

I work on deep learning with medical images and use nibabel package for reading .nii files in python. My code is shown below.

import numpy as np
from pylab import *
import nibabel as nib

img_path=r"C:\\Users\\Umit Kilic\\Komodomyfiles\\umit\\myfile.nii"

nii_obj=nib.load(img_path)
img=nii_obj.get_fdata()
slice1=(img[60,:,:])
slice2=(img[:,80,:])
slice3=(img[:,:,60])

Then I select a slice and want to see in the figure.

figure()
imshow(slice2)
show()

The output of the above code snippet is this: Output of code

After that, I want to apply some operation such as:

 slice2[:, :, 0] -= 123.68
 slice2[:, :, 1] -= 116.779
 slice2[:, :, 2] -= 103.939

But I can not. Cause when I run that code:

print(slice2.shape)

The output is

(121,121)

Normally there should be a third parameter that says the number of channels. There are two parameters. So, is my slice2 grayscale or RGB? How can I apply the operation that I mentioned above?

Community
  • 1
  • 1
umitkilic
  • 327
  • 1
  • 4
  • 17
  • 1
    Why should the `shape` of `slice2` be different? You are modifying the `img` array by changing its content, not its dimension. In the definition of `slice2`, you are dropping one of the dimensions, so of course it's 2 dimensional. By the way, after you modify `img`, you have to again run `slice2 = (img[:,80,:])`, because every time you slice an array, the values are copied. – darksky Feb 21 '19 at 08:29
  • @darksky the code starts with img[:,:,0]-=123.68 belongs to another code. I want to do same opeartion. For avoiding misunderstanding, I changed the code with slice2[] instead of img[]. I want to subtract related value from related channel of slices2 as in the code snipped. – umitkilic Feb 21 '19 at 08:36
  • @darksky when I added img[:,:,:,0]-=123.60 code, this error is shown: IndexError: too many indices for array – umitkilic Feb 21 '19 at 08:40
  • Are you trying to convert RGB to BGR? I'm looking at https://github.com/KaimingHe/deep-residual-networks/issues/5, does that code snippet help you? – darksky Feb 21 '19 at 08:48
  • @darksky I want to subtract the values from RGB channels, but probably it is not RGB and that's why I can not. – umitkilic Feb 21 '19 at 09:16
  • that's a good point, and is entirely possible. Can you share your `myfile.nii` so that I can test it? – darksky Feb 21 '19 at 09:28

0 Answers0