I'm actually work on MRI images using Python. The image format is the NIFTI format I get how to visualise slices on x, y or z axe, but now, I want tu use Sobel filtering on each of them and create a new NIFTI image with those slices.
For that:
- I load the main .nii.gz image (img = nib.load(im_path))
- I load again the main .nii.gz image with a new name "img_sobel" (img_sobel = nib.load(im_path))
- Create a loop for each slice
- Sobel filtering the slice
- Replace this slice on the corresponding slice of img_sobel ("img_sobel_data[:, :, sl] == np.hypot(sx, sy)")
- After the loop, save the img_sobel with the name "image_XXX_Sobel"
Using subplot, I see the sobel filtering work on each slice, but it seems the line "img_sobel_data[:, :, sl] == np.hypot(sx, sy)" don't work, why ?
Here's the lopp section :
# 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)
What's wrong ? Can't we replace an image slice from another one in Python ? Is there a trick to solve this problem ?
Thank you !
EDIT : OK I get a bit more why I can't do taht so easily : I extracted the slices of the NIFTI image, filtered them, but I wasn't change the NIFTI image itself !!! So my question now : How to change the NIFTI image get from img_sobel.get_fdata() ?