lama/segmentation_plugins/secondary_seg_lateral_ventricle.py
def segment_lateral_ventricles(image_to_segment_path: Path,
                               initial_segmentation_path: Path,
                               surrounding_labels: List[int],
                               target_label: int,
                               outpath=None) -> np.ndarray:
    """
    Parameters
    ----------
    image_to_segment_path:
        The image to be segmented
    initial_segmentation_path
        The inital LAMA segmentation
    surrounding_labels
        a list of label numbers that surround the target label
    target_label
        The label we are trying to improve

    Returns
    -------
    A new segmentation with the same dimensions as 'inital_segmentation' containing only the newly segmented
    'target_label'

    """

    image_to_segment = sitk.GetArrayFromImage(sitk.ReadImage(str(image_to_segment_path)))
    initial_segmentation = sitk.GetArrayFromImage(sitk.ReadImage(str(initial_segmentation_path)))

    # Remove all label that are not of interest here
    initial_segmentation[~np.isin(initial_segmentation, surrounding_labels + [target_label])] = 0

    # Set all the surrouding labels to 1
    initial_segmentation[np.isin(initial_segmentation, surrounding_labels)] = 1

    # write(lab, outdir / 'test_surroud.nrrd')

    props = measure.regionprops(initial_segmentation)

    for x in props:
        if x.label == 1:

            # Get the ROI defined by the surrounding labels
            b = x.bbox
            image_to_segment *= -1
            image_roi = image_to_segment[b[0]:b[3], b[1]: b[4], b[2]: b[5]]
            label_roi = initial_segmentation[b[0]:b[3], b[1]: b[4], b[2]: b[5]]

            # Do otsu thresholding on the roi
            ventr_area_itk = sitk.GetImageFromArray(image_roi)
            thresh = sitk.OtsuThreshold(ventr_area_itk)
            thresh_arr = sitk.GetArrayFromImage(thresh)
            thresh_arr = np.invert(thresh_arr)

            # Get connected component labels from the thresholding
            threshold_labels = measure.label(thresh_arr)
            new_segmentation = initial_segmentation.copy().astype(np.short)
            new_segmentation[:] = 0

            # Find the threshold label with the largest overlap with the target label
            largest_overlap = 0
            largest_overlap_label = 0
            thresh_props = [x for x in measure.regionprops(threshold_labels) if x.area > 500]

            for pr in thresh_props:
                ol = overalap(label_roi, target_label, threshold_labels, pr.label)
                if ol > largest_overlap:
                    largest_overlap = ol
                    largest_overlap_label = pr.label

            # Wipe all candidate segmentations except largest overlap
            threshold_labels[threshold_labels != largest_overlap_label] = 0

            # fill small holes
            threshold_labels = morphology.closing(threshold_labels, morphology.ball(2))

            # Insert the threshold label condidates back into the label map
            new_segmentation[b[0]:b[3], b[1]: b[4], b[2]: b[5]] = threshold_labels

            # new_segmentation[new_segmentation != largest_overlap_label] = 0
            new_segmentation[new_segmentation == largest_overlap_label] = target_label

            if outpath:
                write(new_segmentation.astype(np.uint8), outpath)

            return new_segmentation.astype(np.uint8)

    Parameters
    ----------
    image_to_segment_path:
        The image to be segmented
    initial_segmentation_path
        The inital LAMA segmentation
    surrounding_labels
        a list of label numbers that surround the target label
    target_label
        The label we are trying to improve

    Returns
    -------
    A new segmentation with the same dimensions as 'inital_segmentation' containing only the newly segmented
    'target_label'

    
Parameters
image_to_segment_path:
The image to be segmented
initial_segmentation_path
The inital LAMA segmentation
surrounding_labels
a list of label numbers that surround the target label
target_label
The label we are trying to improve

Returns

A new segmentation with the same dimensions as 'inital_segmentation' containing only the newly segmented
'target_label'
Parameters image_to_segment_path : The image to be segmented initial_segmentation_path The inital LAMA segmentation surrounding_labels a list of label numbers that surround the target label target_label The label we are trying to improve Returns A new segmentation with the same dimensions as ' inital_segmentation ' containing only the newly segmented ' target_label '
def segment_lateral_ventricles(image_to_segment_path: Path,
                               initial_segmentation_path: Path,
                               surrounding_labels: List[int],
                               target_label: int,
                               outpath=None) -> np.ndarray:
    image_to_segment = sitk.GetArrayFromImage(sitk.ReadImage(str(image_to_segment_path)))
    initial_segmentation = sitk.GetArrayFromImage(sitk.ReadImage(str(initial_segmentation_path)))
    initial_segmentation[~np.isin(initial_segmentation, surrounding_labels + [target_label])] = 0
    initial_segmentation[np.isin(initial_segmentation, surrounding_labels)] = 1
    props = measure.regionprops(initial_segmentation)
    for x in props:
        if x.label == 1:
            b = x.bbox
            image_to_segment *= -1
            image_roi = image_to_segment[b[0]:b[3], b[1]: b[4], b[2]: b[5]]
            label_roi = initial_segmentation[b[0]:b[3], b[1]: b[4], b[2]: b[5]]
            ventr_area_itk = sitk.GetImageFromArray(image_roi)
            thresh = sitk.OtsuThreshold(ventr_area_itk)
            thresh_arr = sitk.GetArrayFromImage(thresh)
            thresh_arr = np.invert(thresh_arr)
            threshold_labels = measure.label(thresh_arr)
            new_segmentation = initial_segmentation.copy().astype(np.short)
            new_segmentation[:] = 0
            largest_overlap = 0
            largest_overlap_label = 0
            thresh_props = [x for x in measure.regionprops(threshold_labels) if x.area > 500]
            for pr in thresh_props:
                ol = overalap(label_roi, target_label, threshold_labels, pr.label)
                if ol > largest_overlap:
                    largest_overlap = ol
                    largest_overlap_label = pr.label
            threshold_labels[threshold_labels != largest_overlap_label] = 0
            threshold_labels = morphology.closing(threshold_labels, morphology.ball(2))
            new_segmentation[b[0]:b[3], b[1]: b[4], b[2]: b[5]] = threshold_labels
            new_segmentation[new_segmentation == largest_overlap_label] = target_label
            if outpath:
                write(new_segmentation.astype(np.uint8), outpath)
            return new_segmentation.astype(np.uint8)
def segment_lateral_ventricles ( image_to_segment_path : Path , initial_segmentation_path : Path , surrounding_labels : List [ int ] , target_label : int , outpath = None ) -> np . ndarray : image_to_segment = sitk . GetArrayFromImage ( sitk . ReadImage ( str ( image_to_segment_path ) ) ) initial_segmentation = sitk . GetArrayFromImage ( sitk . ReadImage ( str ( initial_segmentation_path ) ) ) initial_segmentation [ ~ np . isin ( initial_segmentation , surrounding_labels + [ target_label ] ) ] = 0 initial_segmentation [ np . isin ( initial_segmentation , surrounding_labels ) ] = 1 props = measure . regionprops ( initial_segmentation ) for x in props : if x . label == 1 : b = x . bbox image_to_segment *= - 1 image_roi = image_to_segment [ b [ 0 ] : b [ 3 ] , b [ 1 ] : b [ 4 ] , b [ 2 ] : b [ 5 ] ] label_roi = initial_segmentation [ b [ 0 ] : b [ 3 ] , b [ 1 ] : b [ 4 ] , b [ 2 ] : b [ 5 ] ] ventr_area_itk = sitk . GetImageFromArray ( image_roi ) thresh = sitk . OtsuThreshold ( ventr_area_itk ) thresh_arr = sitk . GetArrayFromImage ( thresh ) thresh_arr = np . invert ( thresh_arr ) threshold_labels = measure . label ( thresh_arr ) new_segmentation = initial_segmentation . copy ( ) . astype ( np . short ) new_segmentation [ : ] = 0 largest_overlap = 0 largest_overlap_label = 0 thresh_props = [ x for x in measure . regionprops ( threshold_labels ) if x . area > 500 ] for pr in thresh_props : ol = overalap ( label_roi , target_label , threshold_labels , pr . label ) if ol > largest_overlap : largest_overlap = ol largest_overlap_label = pr . label threshold_labels [ threshold_labels != largest_overlap_label ] = 0 threshold_labels = morphology . closing ( threshold_labels , morphology . ball ( 2 ) ) new_segmentation [ b [ 0 ] : b [ 3 ] , b [ 1 ] : b [ 4 ] , b [ 2 ] : b [ 5 ] ] = threshold_labels new_segmentation [ new_segmentation == largest_overlap_label ] = target_label if outpath : write ( new_segmentation . astype ( np . uint8 ) , outpath ) return new_segmentation . astype ( np . uint8 )
Parameters
image_to_segment_path:
The image to be segmented
initial_segmentation_path
The inital LAMA segmentation
surrounding_labels
a list of label numbers that surround the target label
target_label
The label we are trying to improve
Parameters image_to_segment_path : The image to be segmented initial_segmentation_path The inital LAMA segmentation surrounding_labels a list of label numbers that surround the target label target_label The label we are trying to improve
"""
    Parameters
    ----------
    image_to_segment_path:
        The image to be segmented
    initial_segmentation_path
        The inital LAMA segmentation
    surrounding_labels
        a list of label numbers that surround the target label
    target_label
        The label we are trying to improve

    Returns
    -------
    A new segmentation with the same dimensions as 'inital_segmentation' containing only the newly segmented
    'target_label'

    """ # Remove all label that are not of interest here # Set all the surrouding labels to 1 # write(lab, outdir / 'test_surroud.nrrd') # Get the ROI defined by the surrounding labels # Do otsu thresholding on the roi # Get connected component labels from the thresholding # Find the threshold label with the largest overlap with the target label # Wipe all candidate segmentations except largest overlap # fill small holes # Insert the threshold label condidates back into the label map # new_segmentation[new_segmentation != largest_overlap_label] = 0
image_to_segment_path
Path
initial_segmentation_path
Path
surrounding_labels
List[int]
target_label
int
outpath