|
4 | 4 | from nipype.interfaces.afni.base import AFNICommandBase |
5 | 5 |
|
6 | 6 |
|
| 7 | +def getLargestCC(segmentation): |
| 8 | + |
| 9 | + from skimage.measure import label |
| 10 | + import numpy as np |
| 11 | + |
| 12 | + labels = label(segmentation) |
| 13 | + assert labels.max() != 0 # assume at least 1 CC |
| 14 | + largestCC = labels == np.argmax(np.bincount(labels.flat)[1:])+1 |
| 15 | + return largestCC, labels |
| 16 | + |
| 17 | + |
7 | 18 | def keep_gcc(nii_file): |
8 | 19 | import os |
9 | 20 | import nibabel as nib |
10 | 21 | import numpy as np |
11 | 22 | from nipype.utils.filemanip import split_filename as split_f |
12 | 23 |
|
13 | | - def getLargestCC(segmentation): |
14 | | - |
15 | | - from skimage.measure import label |
16 | | - |
17 | | - labels = label(segmentation) |
18 | | - assert labels.max() != 0 # assume at least 1 CC |
19 | | - largestCC = labels == np.argmax(np.bincount(labels.flat)[1:])+1 |
20 | | - return largestCC, labels |
| 24 | + from macapype.nodes.surface import getLargestCC |
21 | 25 |
|
22 | 26 | # nibabel (nifti -> np.array) |
23 | 27 | img = nib.load(nii_file) |
@@ -50,6 +54,44 @@ def getLargestCC(segmentation): |
50 | 54 | return gcc_nii_file |
51 | 55 |
|
52 | 56 |
|
| 57 | +def keep_gcc_by_index(nii_file): |
| 58 | + import os |
| 59 | + import nibabel as nib |
| 60 | + import numpy as np |
| 61 | + from nipype.utils.filemanip import split_filename as split_f |
| 62 | + |
| 63 | + from macapype.nodes.surface import getLargestCC |
| 64 | + |
| 65 | + # nibabel (nifti -> np.array) |
| 66 | + img = nib.load(nii_file) |
| 67 | + data = img.get_fdata().astype(np.int16) |
| 68 | + |
| 69 | + path, fname, ext = split_f(nii_file) |
| 70 | + |
| 71 | + print(np.unique(data)) |
| 72 | + |
| 73 | + new_data = np.zeros(data.shape, data.dtype) |
| 74 | + |
| 75 | + for index in np.unique(data)[1:]: |
| 76 | + data_data_bin_index = np.zeros(data.shape, data.dtype) |
| 77 | + data_data_bin_index[data == index] = 1 |
| 78 | + |
| 79 | + if np.sum(data_data_bin_index): |
| 80 | + gcc_index_data, _ = getLargestCC(data_data_bin_index) |
| 81 | + new_data[gcc_index_data] = index |
| 82 | + |
| 83 | + # nibabel (np.array -> nifti) |
| 84 | + new_img = nib.Nifti1Image(dataobj=new_data, |
| 85 | + header=img.header, |
| 86 | + affine=img.affine) |
| 87 | + |
| 88 | + gcc_nii_file = os.path.abspath(fname + "_gcc" + ext) |
| 89 | + |
| 90 | + nib.save(new_img, gcc_nii_file) |
| 91 | + |
| 92 | + return gcc_nii_file |
| 93 | + |
| 94 | + |
53 | 95 | def merge_tissues(dseg_file, keep_indexes): |
54 | 96 |
|
55 | 97 | import os |
|
0 commit comments