Converting from NII to MHA involves translating the NII format into the MHA format, which can be done using libraries such as NumPy and scikit-image. Here's an example code to convert NII to MHA:
import numpy as np
from skimage import io, filters
# Load the NII file
nii = io.read_nii('sample_nii.nii')
# Define the MHA parameters
mha_shape = (128, 128, 128) # Resolution of the output MHA image
mha_ orientation = 'axial' # Orientation of the output MHA image
# Convert the NII to MHA
mha = filters.mha_from_nii(nii, mha_shape, mha_orientation)
# Save the MHA image
io.write_mha('sample_mha.mha', mha)
In this code, we first load the NII file using the io.read_nii() function from scikit-image. We then define the parameters for the MHA conversion, such as the resolution and orientation of the output image.
Next, we use the filters.mha_from_nii() function from scikit-image to convert the NII file to MHA format. This function takes the NII file, the desired resolution and orientation of the output MHA image, and returns an MHA image.
Finally, we save the resulting MHA image using the io.write_mha() function from scikit-image.
Note that this code assumes that the NII file is stored in the same directory as the Python script, and that the file name is "sample_nii.nii". You may need to modify the file path and name to match your specific use case.
Also, the filters.mha_from_nii() function can take additional parameters to customize the MHA conversion process, such as the voxel size, slice thickness, and number of slices. You can refer to the scikit-image documentation for more information on these parameters and how to use them.