Пример #1
0
def slice_blob(dert__, mask, crit__, AveB, verbose=False, render=False):
    frame = dict(rng=1, dert__=dert__, mask=None, I=0, Dy=0, Dx=0, G=0, M=0, Dyy=0, Dyx=0, Dxy=0, Dxx=0, Ga=0, Ma=0, blob__=[])
    stack_ = deque()  # buffer of running vertical stacks of Ps
    height, width = dert__[0].shape

    if render:
        def output_path(input_path, suffix):
            return str(Path(input_path).with_suffix(suffix))
        streamer = BlobStreamer(CBlob, dert__[1],
                                record_path=output_path(arguments['image'],
                                                        suffix='.im2blobs.avi'))
    if verbose:
        start_time = time()
        print("Converting to image to blobs...")


    for y, dert_ in enumerate(zip(*dert__)):  # first and last row are discarded
        if verbose:
            print(f"\rProcessing line {y + 1}/{height}, ", end="")
            print(f"{len(frame['blob__'])} blobs converted", end="")
            sys.stdout.flush()

        P_ = form_P_(zip(*dert_), crit__[y], mask[y])  # horizontal clustering
        if render:
            render = streamer.update_blob_conversion(y, P_)
        P_ = scan_P_(P_, stack_, frame)  # vertical clustering, adds P up_connects and _P down_connect_cnt
        stack_ = form_stack_(P_, frame, y)

    while stack_:  # frame ends, last-line stacks are merged into their blobs
        form_blob(stack_.popleft(), frame)

    # evaluate P blobs
    comp_slice_blob(frame['blob__'], AveB)

    if verbose:  # print out at the end
        nblobs = len(frame['blob__'])
        print(f"\rImage has been successfully converted to "
              f"{nblobs} blob{'s' if nblobs != 1 else 0} in "
              f"{time() - start_time:.3} seconds", end="")
        blob_ids = [blob_id for blob_id in range(CBlob.instance_cnt)]
        merged_percentage = len([*filter(lambda bid: CBlob.get_instance(bid) is None, blob_ids)]) / len(blob_ids)
        print(f"\nPercentage of merged blobs: {merged_percentage}")

    if render:  # rendering mode after blob conversion
        path = output_path(arguments['image'],
                           suffix='.im2blobs.jpg')
        streamer.end_blob_conversion(y, img_out_path=path)

    return frame  # frame of blobs
Пример #2
0
def cluster_derts(dert__,
                  mask,
                  Ave,
                  fcr,
                  fig,
                  render=False):  # similar to frame_to_blobs

    if fcr:  # comp_r output;  form clustering criterion:
        if fig:
            crit__ = dert__[0] + dert__[
                6] - Ave  # eval by i + m, accum in rng; dert__[:,:,0] if not transposed
        else:
            crit__ = Ave - dert__[3]  # eval by -g, accum in rng
    else:  # comp_g output
        crit__ = dert__[
            6] - Ave  # comp_g output eval by m, or clustering is always by m?

    root_dert__ = dert__  # derts after the comps operation, which is the root_dert__
    dert__ = [*zip(*dert__)]  # transpose dert__ into shape [y, params, x]

    sub_blobs = []  # from form_blob:
    stack_ = deque()  # buffer of running vertical stacks of Ps
    stack_binder = AdjBinder(CDeepStack)
    if render:
        streamer = BlobStreamer(CDeepBlob, crit__, mask)

    if render:
        streamer = BlobStreamer(CDeepBlob, crit__, mask)
    for y, dert_ in enumerate(
            dert__
    ):  # in height, first and last row are discarded;  print(f'Processing intra line {y}...')
        # if False in mask[i]:  # [y,x,params], there is at least one dert in line
        P_binder = AdjBinder(
            CDeepP)  # binder needs data about clusters of the same level
        P_ = form_P_(zip(*dert_), crit__[y], mask[y],
                     P_binder)  # horizontal clustering, adds a row of Ps
        if render:
            render = streamer.update_blob_conversion(
                y, P_)  # if return False, stop rendering
        P_ = scan_P_(
            P_, stack_, root_dert__, sub_blobs, P_binder
        )  # vertical clustering, adds up_connects per P and down_connect_cnt per stack
        stack_ = form_stack_(P_, root_dert__, sub_blobs, y)
        stack_binder.bind_from_lower(P_binder)

    while stack_:  # frame ends, last-line stacks are merged into their blobs:
        form_blob(stack_.popleft(), root_dert__, sub_blobs)

    blob_binder = AdjBinder(CDeepBlob)
    blob_binder.bind_from_lower(stack_binder)
    assign_adjacents(blob_binder)  # add adj_blobs to each blob
    # sub_blobs = find_adjacent(sub_blobs)

    if render:  # rendering mode after blob conversion
        streamer.end_blob_conversion(y)

    return sub_blobs