Beispiel #1
0
def watershed_distance(img, label, container, holder, ERODI=5,
                       DEBRISAREA=50, DISPLACEMENT=50, MASSTHRES=0.2):
    '''watershed existing label, meaning make a cut at the deflection.
    After the cuts, objects will be linked if they are within DISPLACEMENT and MASSTHRES.
    If two candidates are found, it will pick a closer one.

    Args:
    ERODI (int):        Erosion size element for generating watershed seeds.
                        Smaller ERODI will allow more cuts.
    DISPLACEMENT (int): The maximum distance (in pixel)
    MASSTHRES (float):  The maximum difference of total intensity changes.
                        0.2 means it allows for 20% total intensity changes.
    '''

    untr_prev, untr_curr = container.unlinked
    mask_untracked = container._label_untracked.astype(bool)
    wshed_label = watershed(mask_untracked, ERODI)
    wshed_label = skilabel(sizefilterandopen(wshed_label, DEBRISAREA, np.Inf, 0))
    newcells = CellListMaker(img, wshed_label, holder, holder.frame).make_list()
    distanceUntracked = _distance_diff(untr_prev, newcells)
    masschangeUntracked = _totalintensity_difference(untr_prev, newcells)

    withinarea = distanceUntracked < DISPLACEMENT
    withinmass = abs(masschangeUntracked) < MASSTHRES
    withinareaMass = withinarea * withinmass

    withinareaMass = pick_closer_binarycostmat(withinareaMass, distanceUntracked)
    good_curr_idx, good_prev_idx = find_one_to_one_assign(withinareaMass)

    # update the link
    for ci, pi in zip(good_curr_idx, good_prev_idx):
        untr_prev[pi].next = newcells[ci]
    # Get all linear coordinates from good newly segmented cells
    good_curr_coords = [newcells[n].prop.coords for n in good_curr_idx]
    lin_curr_coords = [convert_coords_to_linear(i, holder.img_shape) for i in flatlist(good_curr_coords)]
    # find cells in old mask (in current) that overlaps with good new cells
    old_cells_to_remove, lin_old_coords_remove = find_coords_overlap_coords(untr_curr, lin_curr_coords, holder.img_shape)
    # find cells in new mask which overlaps with the cells in old mask
    newcells_to_update = find_cells_overlap_linear_coords(newcells, lin_old_coords_remove, holder.img_shape)
    # remove old cells
    for old_cell in old_cells_to_remove:
        container.curr_cells.remove(old_cell)
    # add new cells
    container.curr_cells.extend(newcells_to_update)
    return container
Beispiel #2
0
def devide_and_label_objects(bw, REGWSHED):
    label = skilabel(bw).astype(np.int64)
    if REGWSHED > 0:
        label = watershed(label, REGWSHED)
    return label