forked from metrics-lab/SLCN_challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding conversion file image type .mha .nii
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on Wed Apr 20 22:14:40 2022 | ||
@author: lw19 | ||
""" | ||
|
||
|
||
import SimpleITK as sitk | ||
import numpy as np | ||
import nibabel as nb | ||
|
||
|
||
#### Load gifti image using nibabel #### | ||
#image = nb.load('/data/Data/data_release_gDL_benchmarking/Data/Regression/Native_Space/regression_native_space_features/sub-CC00051XX02_ses-7702_L.shape.gii') | ||
image = nb.load('/home/sd20/data/dHCP/merged_metrics/ico6_template/original/CC01215XX11_146331_right_merged.shape.gii') | ||
|
||
#### Define number of channels in gifti #### | ||
channels=4 | ||
|
||
img_array=[] | ||
|
||
|
||
#### Iterate over each channel, extract array and append to img_array object/list #### | ||
for i in range(channels): | ||
array = image.darrays[i].data | ||
img_array.append(array) | ||
|
||
|
||
#### Swap axes so that img_array dim is 40962,4 #### | ||
#img_array = np.swapaxes(img_array, 0,1) | ||
|
||
|
||
#### Create .mha image using sitk #### | ||
mha_img = sitk.GetImageFromArray(img_array) | ||
|
||
#### Write out .mha image #### | ||
sitk.WriteImage(mha_img,'/home/sd20/workspace/SLCN_algorithm/test/sub-CC01215XX111_ses-14633_L.mha') | ||
|
||
#### Read in .mha image again #### | ||
mha_img1 = sitk.ReadImage('/home/sd20/workspace/SLCN_algorithm/test/sub-CC01215XX111_ses-14633_L.mha') | ||
|
||
|
||
#### Get array from image using sitk #### | ||
img1_array = sitk.GetArrayFromImage(mha_img1) |