[Neuroimaging] 2 questions about Image processing on nifti images

Matthew Brett matthew.brett at gmail.com
Wed Mar 4 09:34:46 EST 2020


Hi,

On Wed, Mar 4, 2020 at 2:30 PM PLANTIN Baptiste
<baptiste.plantin at altran.com> wrote:
>
> Hi again !
>
>
>
> OK I get a bit more why I can't do that so easily : I extracted the slices of the NIFTI image (“img_sobel_data”), filtered them, but I wasn't change the NIFTI image itself !!!
>
>
>
>     img = nib.load(im_path)
>
>     img_sobel = nib.load(im_path)
>
>
>
>     # Name the interested data
>
>     img_data = img.get_fdata()
>
>     img_sobel_data = img_sobel.get_fdata()
>
>
>
>     header = img.header
>
>     nb_img = header.get_data_shape()
>
>     nb_img_h = nb_img[2] #Hauteur
>
>
>
>     for sl in range(0,nb_img_h):
>
>             slice_h = img_data[:, :, sl]
>
>             #Sobel
>
>             sx = ndimage.sobel(slice_h, axis=0, mode='constant')
>
>             sy = ndimage.sobel(slice_h, axis=1, mode='constant')
>
>             sobel_h = np.hypot(sx, sy)
>
>
>
>             img_sobel_data[:, :, sl] = sobel_h #Change the image slice to the sobel one
>
> # Save Sobel:
>
> nib.save(img_sobel,imSobel_path)
>
>
>
> So my question now : How to change the NIFTI image get from img_sobel.get_fdata() ?

Have a look at my example in the earlier email.  The trick is to make
a new array, fill it with the data you want, and then make a new image
with that array:

filtered_image = nib.Nifti1Image(new_array, None, nb_img.header)
nib.save(filtered_image, 'my_file_name.nii')

Cheers,

Matthew


More information about the Neuroimaging mailing list