Skip to content

Commit 34b73b3

Browse files
author
frheault
committed
Fix incremental lesions
1 parent 07d32d1 commit 34b73b3

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

scilpy/image/labels.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,4 +610,4 @@ def harmonize_labels(original_data, min_voxel_overlap=1, max_adjacency=1e2):
610610
== label] = next_label
611611
next_label += 1
612612

613-
return relabeled_data
613+
return relabeled_data.astype(np.uint16)

scripts/scil_lesions_harmonize_labels.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
The script works iteratively, so the multiple inputs should be in chronological
1111
order (and changing the order affects the output). All images should be
1212
co-registered.
13+
14+
To obtain labels from binary mask use scil_labels_from_mask.py
15+
16+
WARNING: this script requires all files to have all lesions segmented.
17+
If your data only show new lesions at each timepoints (common in manual
18+
segmentation), use the option --incremental_lesions to merge past timepoints.
19+
T1 = T1, T2 = T1 + T2, T3 = T1 + T2 + T3
1320
"""
1421

1522
import argparse
@@ -18,7 +25,8 @@
1825
import nibabel as nib
1926
import numpy as np
2027

21-
from scilpy.image.labels import get_data_as_labels, harmonize_labels
28+
from scilpy.image.labels import (get_data_as_labels, harmonize_labels,
29+
get_labels_from_mask)
2230
from scilpy.io.utils import (add_overwrite_arg,
2331
assert_inputs_exist,
2432
assert_output_dirs_exist_and_empty,
@@ -48,6 +56,10 @@ def _build_arg_parser():
4856
help='Minimum number of overlapping voxels between '
4957
'lesions for them to be considered as the potential '
5058
'match [%(default)s].')
59+
60+
p.add_argument('--incremental_lesions', action='store_true',
61+
help='If lesions files only show new lesions at each '
62+
'timepoint, this will merge past timepoints.')
5163
p.add_argument('--debug_mode', action='store_true',
5264
help='Add a fake voxel to the corner to ensure consistent '
5365
'colors in MI-Brain.')
@@ -67,6 +79,19 @@ def main():
6779
imgs = [nib.load(filename) for filename in args.in_images]
6880
original_data = [get_data_as_labels(img) for img in imgs]
6981

82+
masks = []
83+
if args.incremental_lesions:
84+
for i, data in enumerate(original_data):
85+
mask = np.zeros_like(data)
86+
mask[data > 0] = 1
87+
masks.append(mask)
88+
if i > 0:
89+
new_data = np.sum(masks, axis=0)
90+
new_data[new_data > 0] = 1
91+
else:
92+
new_data = mask
93+
original_data[i] = get_labels_from_mask(new_data)
94+
7095
relabeled_data = harmonize_labels(original_data,
7196
args.min_voxel_overlap,
7297
max_adjacency=args.max_adjacency)

0 commit comments

Comments
 (0)