| import SimpleITK as sitk |
| import numpy as np |
| import scipy.ndimage as ndimage |
| from scipy.ndimage import distance_transform_edt |
| from scipy.ndimage import label, find_objects |
|
|
|
|
| def split_connected_components(labels, label_value, offset, min_volume=400, top_n=6): |
| """ |
| Split the region with the specified label_value into multiple connected components and reassign labels. |
| |
| Parameters: |
| labels (np.ndarray): Input label array |
| label_value (int): The label value to split |
| offset (int): Offset used to generate new label values |
| min_volume (int): Minimum volume to retain connected components |
| top_n (int): Retain the top-n connected components by volume |
| |
| Returns: |
| np.ndarray: Relabeled array |
| """ |
| |
| binary_mask = (labels == label_value) |
|
|
| structure = np.array([[[0, 0, 0], |
| [0, 1, 0], |
| [0, 0, 0]], |
| [[0, 1, 0], |
| [1, 1, 1], |
| [0, 1, 0]], |
| [[0, 0, 0], |
| [0, 1, 0], |
| [0, 0, 0]]], dtype=int) |
|
|
| |
| labeled_array, num_features = label(binary_mask, structure=structure) |
|
|
| |
| new_labels = labels.copy() |
|
|
| |
| volumes = [np.sum(labeled_array == i) for i in range(1, num_features + 1)] |
|
|
| |
| top_n_indices = np.argsort(volumes)[-top_n:][::-1] |
| top_n_volumes_labels = [(volumes[i], i + 1) for i in top_n_indices] |
|
|
| |
| current_label = offset |
| for volume, i in top_n_volumes_labels: |
| region_mask = (labeled_array == i) |
| if volume >= min_volume: |
| new_labels[region_mask] = current_label |
| current_label += 1 |
| else: |
| new_labels[region_mask] = 0 |
|
|
| return new_labels |
|
|
|
|
| def remove_small_connected_components(prediction, min_volume, label_values): |
| """ |
| Remove small connected components and set them as background. |
| |
| Parameters: |
| prediction (np.ndarray): Model output predictions |
| min_volume (int): Minimum volume to retain connected components |
| label_values (list): List of label values to process |
| |
| Returns: |
| np.ndarray: Processed prediction array |
| """ |
| new_prediction = prediction.copy() |
|
|
| |
| structure = np.array([[[0, 0, 0], |
| [0, 1, 0], |
| [0, 0, 0]], |
| [[0, 1, 0], |
| [1, 1, 1], |
| [0, 1, 0]], |
| [[0, 0, 0], |
| [0, 1, 0], |
| [0, 0, 0]]], dtype=int) |
|
|
| for index, label_value in enumerate(label_values): |
| print(f"Processing label {label_value}:") |
| |
| binary_mask = (prediction == label_value) |
| minimum = min_volume[index] |
|
|
| labeled_array, num_features = label(binary_mask, structure=structure) |
|
|
| |
| slices = find_objects(labeled_array) |
|
|
| retained_sizes = [] |
| removed_sizes = [] |
|
|
| |
| for i, slice_ in enumerate(slices): |
| region_size = np.sum(labeled_array[slice_] == (i + 1)) |
| if region_size <= minimum: |
| removed_sizes.append(region_size) |
| new_prediction[labeled_array == (i + 1)] = 0 |
| else: |
| retained_sizes.append(region_size) |
|
|
| |
| if retained_sizes: |
| print(f" Retained regions sizes: {retained_sizes}") |
| if removed_sizes: |
| print(f" Removed regions sizes: {removed_sizes}") |
|
|
| return new_prediction |
|
|
|
|
|
|
| def calculate_iou(label1, label2): |
| intersection = np.logical_and(label1, label2).sum() |
| union = np.logical_or(label1, label2).sum() |
| return intersection / union |
|
|
|
|
| def relabel_connected_components(segmentation): |
| """ |
| Handle partial confusion between left and right hip bones in stage0 segmentation. |
| |
| Parameters: |
| segmentation (np.ndarray): The segmentation labels from stage0. |
| |
| Returns: |
| np.ndarray: Relabeled segmentation with confusion resolved. |
| """ |
| |
| label_2, num_features_2 = ndimage.label(segmentation == 2) |
| label_3, num_features_3 = ndimage.label(segmentation == 3) |
|
|
| |
| size_2 = np.bincount(label_2.ravel()) |
| size_3 = np.bincount(label_3.ravel()) |
|
|
| |
| new_segmentation = np.copy(segmentation) |
|
|
| |
| struct = ndimage.generate_binary_structure(3, 1) |
|
|
| |
| for label in range(1, num_features_2 + 1): |
| current_region = (label_2 == label) |
| neighbors = ndimage.binary_dilation(current_region, structure=struct) & (segmentation == 3) |
|
|
| if neighbors.any(): |
| touching_labels_3 = np.unique(label_3[neighbors]) |
| for lbl_3 in touching_labels_3: |
| if lbl_3 > 0: |
| if 5 * size_2[label] < size_3[lbl_3]: |
| print(f"Change: class_2 (size: {size_2[label]}) -> class_3 (size: {size_3[lbl_3]})") |
| new_segmentation[current_region] = 3 |
| elif 5 * size_3[lbl_3] < size_2[label]: |
| print(f"Change: class_3 (size: {size_3[lbl_3]}) -> class_2 (size: {size_2[label]})") |
| new_segmentation[label_3 == lbl_3] = 2 |
|
|
| return new_segmentation |
|
|
|
|
| def refine_labels(label1, label2, threshold=0.99): |
| """ |
| Refine label2 based on reference from label1 if IoU is below the threshold. |
| |
| Parameters: |
| label1 (np.ndarray): The reference label. |
| label2 (np.ndarray): The label to be refined. |
| threshold (float): IoU threshold for refinement. Default is 0.99. |
| |
| Returns: |
| np.ndarray: Refined label. |
| """ |
| iou = calculate_iou(label1 > 0, label2 > 0) |
| if iou >= threshold: |
| return label2 |
|
|
| print('Refining label...') |
| fixed_label2 = label2.copy() |
|
|
| |
| structure = np.array([[[0, 0, 0], |
| [0, 1, 0], |
| [0, 0, 0]], |
| [[0, 1, 0], |
| [1, 1, 1], |
| [0, 1, 0]], |
| [[0, 0, 0], |
| [0, 1, 0], |
| [0, 0, 0]]], dtype=int) |
| labeled_a, num_features_a = label(label2, structure=structure) |
|
|
| |
| for component_id in range(1, num_features_a + 1): |
| component_mask = (labeled_a == component_id) |
| if not np.any(component_mask & (label1 > 0)): |
| |
| fixed_label2[component_mask] = 0 |
|
|
| |
| fg_to_bg_mask = (label1 > 0) & (label2 == 0) |
|
|
| |
| if fg_to_bg_mask.any(): |
| distance, indices = distance_transform_edt(fixed_label2 == 0, return_indices=True) |
| nearest_foreground = label2[tuple(indices)] |
| fixed_label2[fg_to_bg_mask] = nearest_foreground[fg_to_bg_mask] |
|
|
| return fixed_label2 |
|
|
|
|
| def process_final_label(segmentation): |
| """ |
| Process the final segmentation labels by refining the connected components of specific labels. |
| |
| Parameters: |
| segmentation (np.ndarray): The final segmentation labels. |
| |
| Returns: |
| np.ndarray: Refined segmentation with certain connected components removed or relabeled. |
| """ |
| |
| new_segmentation = np.copy(segmentation) |
|
|
| |
| segmentation = np.where((segmentation >= 1) & (segmentation <= 10), 0, segmentation) |
|
|
| |
| label_11, num_features_11 = ndimage.label(segmentation == 11) |
| label_21, num_features_21 = ndimage.label(segmentation == 21) |
|
|
| |
| size_11 = np.bincount(label_11.ravel()) |
| size_21 = np.bincount(label_21.ravel()) |
|
|
| |
| largest_label_11_index = np.argmax(size_11[1:]) + 1 |
| largest_label_21_index = np.argmax(size_21[1:]) + 1 |
|
|
| assert num_features_11 > 0 and num_features_21 > 0, "label 11 and label 21 have no connected components!!" |
|
|
| |
| label_11[label_11 == largest_label_11_index] = 0 |
| num_features_11 -= 1 |
|
|
| label_21[label_21 == largest_label_21_index] = 0 |
| num_features_21 -= 1 |
|
|
| |
| struct = ndimage.generate_binary_structure(3, 1) |
|
|
| |
| def process_label(label, segment_label, num_features): |
| if num_features < 1: |
| return |
|
|
| for lbl in range(1, num_features + 1): |
| current_region = (label == lbl) |
| neighbors = ndimage.binary_dilation(current_region, structure=struct) & (segmentation != segment_label) |
|
|
| if neighbors.any(): |
| |
| touching_labels = np.unique(segmentation[neighbors]) |
| touching_labels = touching_labels[touching_labels != 0] |
| touching_labels = touching_labels[touching_labels != segment_label] |
|
|
| if touching_labels.size > 0: |
| |
| touching_label_sizes = {label: np.sum(segmentation == label) for label in touching_labels} |
|
|
| |
| max_touching_label = max(touching_label_sizes, key=touching_label_sizes.get) |
| print(f"Changing segment {lbl} from {segment_label} to {max_touching_label}") |
| new_segmentation[current_region] = max_touching_label |
|
|
| |
| process_label(label_11, 11, num_features_11) |
|
|
| |
| process_label(label_21, 21, num_features_21) |
|
|
| return new_segmentation |
|
|
|
|
| if __name__ == "__main__": |
| labels = sitk.ReadImage( |
| "/home/ypy/Code/PENGWIN-example-algorithm-main/PENGWIN-challenge-packages/preliminary-development-phase-ct/stage1_label_after_remove_101_1.nii.gz") |
| spacing = labels.GetSpacing() |
| direction = labels.GetDirection() |
| origin = labels.GetOrigin() |
| labels = sitk.GetArrayFromImage(labels) |
| |
| |
| |
| |
| |
| new_labels = relabel_connected_components(labels) |
| save_label = sitk.GetImageFromArray(new_labels.astype(np.int8)) |
| save_label.SetSpacing(spacing) |
| save_label.SetDirection(direction) |
| save_label.SetOrigin(origin) |
| sitk.WriteImage(save_label, "stage1_label_after.nii.gz", useCompression=True) |
|
|