-4

I'm trying to strip MRI images but I can't understand this part of the code. Specifically:

for i in range(nii_images.shape[2]):
    data = nii_images[:,:,i]
    print(i)

an MRI image stripped.

Dave
  • 5,108
  • 16
  • 30
  • 40
Bellaaa90
  • 1
  • 1
  • 2
    Where is `nii_images` defined? – DirtyBit Feb 18 '19 at 09:59
  • If you mean what does each variable refer to, we have no idea; you'd need to check the rest of the code or ask its author for that. If you mean what does the loop do, it loops through the given object, assigns the name `data` to something in that object (possibly a numpy array, given the tuple indexing), and then prints the loop counter. – TigerhawkT3 Feb 18 '19 at 10:00
  • @user5173426 nii_images = img.get_data() – Bellaaa90 Feb 18 '19 at 10:00
  • And what is `img`? –  Feb 18 '19 at 10:00
  • It extracts the 2d slices of the 3d array along its third dimension. For an image these tend to be the color component channels. – Dan D. Feb 18 '19 at 10:03
  • I guess you are using the library nibabel? Anyway, you are slicing a numpy array composed of voxels. – Mathieu Feb 18 '19 at 10:26

1 Answers1

0

First, we need to understand what the loop is looping on:

for i in range(nii_images.shape[2]):

nii.images appears to be a class, and then .shape is perhaps an array in that class. It seems to be an array of arrays, because we then get the third element ([2]) and we loop on that.

.

Next, I think it is the slicing that you are having trouble with.

data = nii_images[:,:,i]

Are you using numpy? Because this looks like another post. Or here. Or over here

It seems to select a column of a 2d data set. It seems like an odd way of doing it though. As Dan D. says,

It extracts the 2d slices of the 3d array along its third dimension. For an image these tend to be the color component channels.

Some output would be really helpful here to understand the data further. Also, is there any reason why you need to understand the code? Could you look at the documentation?

PrinceOfCreation
  • 389
  • 1
  • 12