Beginner Guide to Smooth .nii Images in Python

When we are processing .nii neuro images, we may have to smooth them. In this tutorial, we will use an example to show you how to do. We will use python nibabel library to smooth.

Step 1: install nibabel

We can use pip to install.

pip install nibabel

Step 2: smooth .nii images

Here is an example code.

import nibabel as nib
import nibabel.processing

input_img = 'input.nii'
saved_img = 'saved.nii'
sigma = 3 #the higher, the smoother(blur)

img = nib.load(input_img)
fhwm = nib.processing.sigma2fwhm(sigma)
smoothed_img = nib.processing.smooth_image(img, fhwm)
nib.save(smoothed_img, saved_img)

In this code, we will use nib.processing.smooth_image() to smooth.

Beginner Guide to Smooth .nii Images in Python