Python scipy.ndimage 模块,labeled_comprehension() 实例源码

我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用scipy.ndimage.labeled_comprehension()

项目:pyku    作者:dubvulture    | 项目源码 | 文件源码
def remove_artifacts(self, image):
        """
        Remove the connected components that are not within the parameters
        Operates in place
        :param image: sudoku's thresholded image w/o grid
        :return: None
        """
        labeled, features = label(image, structure=CROSS)
        lbls = np.arange(1, features + 1)
        areas = extract_feature(image, labeled, lbls, np.sum,
                                np.uint32, 0)
        sides = extract_feature(image, labeled, lbls, min_side,
                                np.float32, 0, True)
        diags = extract_feature(image, labeled, lbls, diagonal,
                                np.float32, 0, True)

        for index in lbls:
            area = areas[index - 1] / 255
            side = sides[index - 1]
            diag = diags[index - 1]
            if side < 5 or side > 20 \
                    or diag < 15 or diag > 25 \
                    or area < 40:
                image[labeled == index] = 0
        return None
项目:pyku    作者:dubvulture    | 项目源码 | 文件源码
def remove_artifacts(self, image):
        """
        Remove the connected components that are not within the parameters
        Operates in place
        :param image: sudoku's thresholded image w/o grid
        :return: None
        """
        labeled, features = label(image, structure=CROSS)
        lbls = np.arange(1, features + 1)
        areas = extract_feature(image, labeled, lbls, np.sum,
                                np.uint32, 0)
        sides = extract_feature(image, labeled, lbls, min_side,
                                np.float32, 0, True)
        diags = extract_feature(image, labeled, lbls, diagonal,
                                np.float32, 0, True)

        for index in lbls:
            area = areas[index - 1] / 255
            side = sides[index - 1]
            diag = diags[index - 1]
            if side < 5 or side > 20 \
                    or diag < 15 or diag > 25 \
                    or area < 40:
                image[labeled == index] = 0
        return None
项目:pyem    作者:asarnow    | 项目源码 | 文件源码
def binary_volume_opening(vol, minvol):
    lb_vol, num_objs = label(vol)
    lbs = np.arange(1, num_objs + 1)
    v = labeled_comprehension(lb_vol > 0, lb_vol, lbs, np.sum, np.int, 0)
    ix = np.isin(lb_vol, lbs[v >= minvol])
    newvol = np.zeros(vol.shape)
    newvol[ix] = vol[ix]
    return newvol
项目:gnocchi    作者:gnocchixyz    | 项目源码 | 文件源码
def quantile(self, q):
        return self._scipy_aggregate(ndimage.labeled_comprehension,
                                     func=functools.partial(
                                         numpy.percentile,
                                         q=q,
                                     ),
                                     out_dtype='float64',
                                     default=None)
项目:cnn-ms-lesion-segmentation    作者:sergivalverde    | 项目源码 | 文件源码
def post_process_segmentation(input_scan, options, save_nifti = True):
    """
    Post-process the probabilistic segmentation using parameters t_bin and l_min
    t_bin: threshold to binarize the output segmentations 
    l_min: minimum lesion volume

    Inputs: 
    - input_scan: probabilistic input image (segmentation)
    - options dictionary
    - save_nifti: save the result as nifti 

    Output:
    - output_scan: final binarized segmentation 
    """

    from scipy import ndimage

    t_bin = options['t_bin']
    l_min = options['l_min']
    output_scan = np.zeros_like(input_scan)

    # threshold input segmentation
    t_segmentation = input_scan >= t_bin

    # filter candidates by size and store those > l_min
    labels, num_labels = ndimage.label(t_segmentation)
    label_list = np.unique(labels)
    num_elements_by_lesion = ndimage.labeled_comprehension(t_segmentation, labels, label_list, np.sum, float, 0)

    for l in range(len(num_elements_by_lesion)):
        if num_elements_by_lesion[l]>l_min:
            # assign voxels to output
            current_voxels = np.stack(np.where(labels == l), axis =1)
            output_scan[current_voxels[:,0], current_voxels[:,1], current_voxels[:,2]] = 1

    #save the output segmentation as Nifti1Image
    if save_nifti:
        nifti_out = nib.Nifti1Image(output_scan, np.eye(4))
        nifti_out.to_filename(os.path.join(options['test_folder'], options['test_scan'], options['experiment'], options['test_name']))

    return output_scan
项目:cnn-ms-lesion-segmentation    作者:sergivalverde    | 项目源码 | 文件源码
def post_process_segmentation(input_scan, options, save_nifti = True):
    """
    Post-process the probabilistic segmentation using parameters t_bin and l_min
    t_bin: threshold to binarize the output segmentations 
    l_min: minimum lesion volume

    Inputs: 
    - input_scan: probabilistic input image (segmentation)
    - options dictionary
    - save_nifti: save the result as nifti 

    Output:
    - output_scan: final binarized segmentation 
    """

    from scipy import ndimage

    t_bin = options['t_bin']
    l_min = options['l_min']
    output_scan = np.zeros_like(input_scan)

    # threshold input segmentation
    t_segmentation = input_scan >= t_bin

    # filter candidates by size and store those > l_min
    labels, num_labels = ndimage.label(t_segmentation)
    label_list = np.unique(labels)
    num_elements_by_lesion = ndimage.labeled_comprehension(t_segmentation, labels, label_list, np.sum, float, 0)

    for l in range(len(num_elements_by_lesion)):
        if num_elements_by_lesion[l]>l_min:
            # assign voxels to output
            current_voxels = np.stack(np.where(labels == l), axis =1)
            output_scan[current_voxels[:,0], current_voxels[:,1], current_voxels[:,2]] = 1

    #save the output segmentation as Nifti1Image
    if save_nifti:
        nifti_out = nib.Nifti1Image(output_scan, np.eye(4))
        nifti_out.to_filename(os.path.join(options['test_folder'], options['test_scan'], options['experiment'], options['test_name']))

    return output_scan
项目:DrainageNetworkExtraction    作者:MiladHooshyar    | 项目源码 | 文件源码
def segment_size_curvature(curve_Array):
    min_c = np.nanmin(curve_Array)
    max_c = np.nanmax(curve_Array)
    max_c = max(max_c , -min_c)

    max_num_label_valley = 0
    max_num_label_ridge = 0

    d_curve = max_c / (5 * 100)
    for i in range (0 , 100):
        k_tresh_valley = 1 * ( float(i) * d_curve)
        k_tresh_ridge = -1 * k_tresh_valley

        Val_Array = np.where(curve_Array >= k_tresh_valley, 1, 0)
        Rid_Array = np.where(curve_Array <= k_tresh_ridge, 1, 0)

        Lab_Val_Array, num_label_val = ndimage.label(Val_Array , structure = np.ones((3 , 3)))
        Lab_Rid_Array, num_label_rid = ndimage.label(Rid_Array , structure = np.ones((3 , 3)))

        labels_val = np.arange(1, num_label_val + 1)
        labels_rid = np.arange(1, num_label_rid + 1)

        try:
            area_label_val = ndimage.labeled_comprehension(Val_Array, Lab_Val_Array, labels_val, np.sum, int, 0)
            num_sig_label_valley = np.sum(area_label_val >= 1)
        except ValueError:
            num_sig_label_valley = 0

        try:
            area_label_rid = ndimage.labeled_comprehension(Rid_Array, Lab_Rid_Array, labels_rid, np.sum, int, 0)
            num_sig_label_ridge = np.sum(area_label_rid >= 1)
        except ValueError:
            num_sig_label_ridge = 0


        if num_sig_label_valley >= max_num_label_valley:
            max_num_label_valley = num_sig_label_valley
            valley_thresh = k_tresh_valley
        if num_sig_label_ridge >= max_num_label_ridge:
            max_num_label_ridge = num_sig_label_ridge
            ridge_thresh = k_tresh_ridge

    return valley_thresh , ridge_thresh
项目:DrainageNetworkExtraction    作者:MiladHooshyar    | 项目源码 | 文件源码
def neg_bond(curvature_file, valley_bond_file , ridge_bond_file , covergent_sig_file , divergent_sig_file):

    curve_Array = arcpy.RasterToNumPyArray(curvature_file  , nodata_to_value=0)
    corner = arcpy.Point(arcpy.Describe(curvature_file).Extent.XMin,arcpy.Describe(curvature_file).Extent.YMin)
    dx = arcpy.Describe(curvature_file).meanCellWidth

    valley_thresh , ridge_thresh = segment_size_curvature(curve_Array)


    valley = np.where(curve_Array > valley_thresh , 1 , 0)
    ridge = np.where(curve_Array < ridge_thresh , 1 , 0)

    Lab_Val_Array, num_label_val = ndimage.label(valley , structure = np.ones((3 , 3)))
    Lab_Rid_Array, num_label_rid = ndimage.label(ridge , structure = np.ones((3 , 3)))

    perc90_valley = ndimage.labeled_comprehension(curve_Array, Lab_Val_Array, np.arange(1, num_label_val + 1) , percentile, float, 0)
    perc90_ridge = ndimage.labeled_comprehension(-1 * curve_Array, Lab_Rid_Array, np.arange(1, num_label_rid + 1) , percentile, float, 0)

    area_valley = ndimage.labeled_comprehension(valley, Lab_Val_Array, np.arange(1, num_label_val + 1) , np.sum, float, 0)
    area_ridge = ndimage.labeled_comprehension(ridge, Lab_Rid_Array, np.arange(1, num_label_rid + 1) , np.sum, float, 0)

    main_patch_val = perc90_valley[area_valley == np.max(area_valley)][0]
    main_patch_ridge = perc90_ridge[area_ridge == np.max(area_ridge)][0]


    sig_valley_thresh = otsu(perc90_valley[perc90_valley < main_patch_val])
    sig_ridge_thresh = otsu(perc90_ridge[perc90_ridge < main_patch_ridge])

    perc90_valley = perc90_valley[Lab_Val_Array - 1]
    perc90_valley = np.where(valley == 1 , perc90_valley , 0)

    perc90_ridge = perc90_ridge[Lab_Rid_Array - 1]
    perc90_ridge = np.where(ridge == 1 , perc90_ridge , 0)

    sig_valley_patch = np.where(perc90_valley > sig_valley_thresh , 1 , 0)
    sig_ridge_patch = np.where(perc90_ridge > sig_ridge_thresh , 1 , 0)

    arcpy.NumPyArrayToRaster(perc90_valley , corner,dx,dx , value_to_nodata=0).save(covergent_sig_file)
    arcpy.NumPyArrayToRaster(perc90_ridge , corner,dx,dx , value_to_nodata=0).save(divergent_sig_file)

    arcpy.NumPyArrayToRaster(sig_valley_patch , corner,dx,dx , value_to_nodata=0).save(valley_bond_file)
    arcpy.NumPyArrayToRaster(sig_ridge_patch , corner,dx,dx , value_to_nodata=0).save(ridge_bond_file)

    return valley_thresh