Пример #1
0
def segment_mc(pred, seg, delta):
    rag = feats.compute_rag(seg)
    edge_probs = embed.edge_probabilities_from_embeddings(
        pred, seg, rag, delta)
    edge_sizes = feats.compute_boundary_mean_and_length(rag, pred[0])[:, 1]
    costs = mc.transform_probabilities_to_costs(edge_probs,
                                                edge_sizes=edge_sizes)
    mc_seg = mc.multicut_kernighan_lin(rag, costs)
    mc_seg = feats.project_node_labels_to_pixels(rag, mc_seg)
    return mc_seg
def supervoxel_merging(mem, sv, beta=0.5, verbose=False):

    rag = feats.compute_rag(sv)
    costs = feats.compute_boundary_features(rag, mem)[:, 0]

    edge_sizes = feats.compute_boundary_mean_and_length(rag, mem)[:, 1]
    costs = mc.transform_probabilities_to_costs(costs,
                                                edge_sizes=edge_sizes,
                                                beta=beta)

    node_labels = mc.multicut_kernighan_lin(rag, costs)
    segmentation = feats.project_node_labels_to_pixels(rag, node_labels)

    return segmentation
Пример #3
0
def segment_volume_lmc_from_seg(boundary_pmaps,
                                nuclei_seg,
                                threshold=0.4,
                                sigma=2.0,
                                sp_min_size=100):
    watershed = distance_transform_watershed(boundary_pmaps,
                                             threshold,
                                             sigma,
                                             min_size=sp_min_size)[0]

    # compute the region adjacency graph
    rag = compute_rag(watershed)

    # compute the edge costs
    features = compute_boundary_mean_and_length(rag, boundary_pmaps)
    costs, sizes = features[:, 0], features[:, 1]

    # transform the edge costs from [0, 1] to  [-inf, inf], which is
    # necessary for the multicut. This is done by intepreting the values
    # as probabilities for an edge being 'true' and then taking the negative log-likelihood.
    # in addition, we weight the costs by the size of the corresponding edge

    # we choose a boundary bias smaller than 0.5 in order to
    # decrease the degree of over segmentation
    boundary_bias = .6

    costs = transform_probabilities_to_costs(costs,
                                             edge_sizes=sizes,
                                             beta=boundary_bias)
    max_cost = np.abs(np.max(costs))
    lifted_uvs, lifted_costs = lifted_problem_from_segmentation(
        rag,
        watershed,
        nuclei_seg,
        overlap_threshold=0.2,
        graph_depth=4,
        same_segment_cost=5 * max_cost,
        different_segment_cost=-5 * max_cost)

    # solve the full lifted problem using the kernighan lin approximation introduced in
    # http://openaccess.thecvf.com/content_iccv_2015/html/Keuper_Efficient_Decomposition_of_ICCV_2015_paper.html
    node_labels = lmc.lifted_multicut_kernighan_lin(rag, costs, lifted_uvs,
                                                    lifted_costs)
    lifted_segmentation = project_node_labels_to_pixels(rag, node_labels)
    return lifted_segmentation
Пример #4
0
affs = np.transpose(affs.cpu().numpy(), (1, 0, 2, 3))
gt_affs = np.transpose(gt_affs.cpu().numpy(), (1, 0, 2, 3))
seg = seg.cpu().numpy()
gt_seg = gt_seg.cpu().numpy()
boundary_input = np.mean(affs, axis=0)
gt_boundary_input = np.mean(gt_affs, axis=0)

rag = feats.compute_rag(seg)
# edges rag.uvIds() [[1, 2], ...]

costs = feats.compute_affinity_features(rag, affs, offsets)[:, 0]
gt_costs = calculate_gt_edge_costs(rag.uvIds(), seg.squeeze(),
                                   gt_seg.squeeze())

edge_sizes = feats.compute_boundary_mean_and_length(rag, boundary_input)[:, 1]
gt_edge_sizes = feats.compute_boundary_mean_and_length(rag,
                                                       gt_boundary_input)[:, 1]
costs = mc.transform_probabilities_to_costs(costs, edge_sizes=edge_sizes)
gt_costs = mc.transform_probabilities_to_costs(gt_costs, edge_sizes=edge_sizes)

node_labels = mc.multicut_kernighan_lin(rag, costs)
gt_node_labels = mc.multicut_kernighan_lin(rag, gt_costs)

segmentation = feats.project_node_labels_to_pixels(rag, node_labels)
gt_segmentation = feats.project_node_labels_to_pixels(rag, gt_node_labels)
plt.imshow(
    np.concatenate(
        (gt_segmentation.squeeze(), segmentation.squeeze(), seg.squeeze()),
        axis=1))
plt.show()
Пример #5
0
def refine_seg(raw,
               seeds,
               restrict_to_seeds=True,
               restrict_to_bb=False,
               return_intermediates=False):
    pred = get_prediction(raw, cache=False)

    n_threads = 1
    # make watershed
    ws, _ = stacked_watershed(pred,
                              threshold=.5,
                              sigma_seeds=1.,
                              n_threads=n_threads)
    rag = compute_rag(ws, n_threads=n_threads)
    edge_feats = compute_boundary_mean_and_length(rag,
                                                  pred,
                                                  n_threads=n_threads)
    edge_feats, edge_sizes = edge_feats[:, 0], edge_feats[:, 1]
    z_edges = compute_z_edge_mask(rag, ws)
    edge_costs = compute_edge_costs(edge_feats,
                                    beta=.4,
                                    weighting_scheme='xyz',
                                    edge_sizes=edge_sizes,
                                    z_edge_mask=z_edges)

    # make seeds and map them to edges
    bb = tuple(
        slice(sh // 2 - ha // 2, sh // 2 + ha // 2)
        for sh, ha in zip(pred.shape, seeds.shape))

    seeds[seeds < 0] = 0
    seeds = vigra.analysis.labelVolumeWithBackground(seeds.astype('uint32'))
    seed_ids = np.unique(seeds)
    seed_mask = binary_erosion(seeds, iterations=2)

    seeds_new = seeds.copy()
    seeds_new[~seed_mask] = 0
    seed_ids_new = np.unique(seeds_new)
    for seed_id in seed_ids:
        if seed_id in seed_ids_new:
            continue
        seeds_new[seeds == seed_id] = seed_id

    seeds_full = np.zeros(pred.shape, dtype=seeds.dtype)
    seeds_full[bb] = seeds
    seeds = seeds_full

    seed_labels = compute_maximum_label_overlap(ws, seeds, ignore_zeros=True)

    edge_ids = rag.uvIds()
    labels_u = seed_labels[edge_ids[:, 0]]
    labels_v = seed_labels[edge_ids[:, 1]]

    seed_mask = np.logical_and(labels_u != 0, labels_v != 0)
    same_seed = np.logical_and(seed_mask, labels_u == labels_v)
    diff_seed = np.logical_and(seed_mask, labels_u != labels_v)

    max_att = edge_costs.max() + .1
    max_rep = edge_costs.min() - .1
    edge_costs[same_seed] = max_att
    edge_costs[diff_seed] = max_rep

    # run multicut
    node_labels = multicut_kernighan_lin(rag, edge_costs)
    if restrict_to_seeds:
        seed_nodes = np.unique(node_labels[seed_labels > 0])
        node_labels[~np.isin(node_labels, seed_nodes)] = 0
        vigra.analysis.relabelConsecutive(node_labels, out=node_labels)

    seg = project_node_labels_to_pixels(rag, node_labels, n_threads=n_threads)

    if restrict_to_bb:
        bb_mask = np.zeros(seg.shape, dtype='bool')
        bb_mask[bb] = 1
        seg[~bb_mask] = 0

    if return_intermediates:
        return pred, ws, seeds, seg
    else:
        return seg