Trying to turn a non-cubic .nii brain scan into a cubic one using numpy - data deteriorating - doesnt look the same

51 views Asked by At

I have a .nii brain scan file converted to an array with dimensions (240, 240, 155). I need a cubic 3D array - so I am concatenating 2 arrays of shape (240, 240, 43) and (240, 240, 42) to the original array along the 2nd axis (axis=2), which results in a (240, 240, 240) array. This is great, but when I convert my array back into an .nii brain scan file and view it, the originally clear scan is surrounded by a gray mist, and the scan overall looks bright and blurred. If I'm just connecting 2 arrays full of zeroes to the front and back of my original array, why does the file view change after this process?

Here is my code:

zero_buffer_front = np.zeros((240, 240, 43))
zero_buffer_end = np.zeros((240, 240, 42))

data_dir_save = # <directory I'm saving data in>

flair_MRI = # <my complete path to file>
nii_file = nib.load(flair_MRI)
arr_MRI = np.array(nii_file.dataobj)

rescaling_data = np.concatenate((zero_buffer_front, arr_MRI, zero_buffer_end), axis=2)

nifti_file_conv = nib.Nifti1Image(rescaling_data, nii_file.affine, nii_file.header)

save_path = # <complete path I'm saving to>
nib.save(nifti_file_conv, save_path)

Does numpy's concatenate change something in the original array? When I view the original file on https://socr.umich.edu/HTML5/BrainViewer/, the scan looks normal and defined, with just the brain showing up. However, when I modify my data as shown above, a gray mist surrounds the brain scan, with the scan itself brighter and less defined. I'm using a single flair MRI scan from the data found on https://www.kaggle.com/datasets/andrewmvd/brain-tumor-segmentation-in-mri-brats-2015. Help!

1

There are 1 answers

6
LukasNeugebauer On

I'm going to guess that the outside of the brain in your original scan is not filled with zeros but something > 0. So if you add zeros, the viewer rescales the colormap to the new range which would explain your results. I.e. if there's a 7 everywhere outside the brain it might have been black before because it was the lowest value. But now there's zeros as well and in comparison the 7 is higher and is assigned a grey color. Check some elements outside the brain and pad with that value instead of zeros.