예제 #1
0
def get_crossing_section_direction(classified_pixels, crossing_pixel,
                                   last_gradients, section):
    # counter gradients frequency
    cnt_gradient = Counter(last_gradients.get_list())
    # count in list
    grads = cnt_gradient.most_common()

    crossing_section_direction = []

    next = crossing_pixel
    next_value = classified_pixels[next[0], next[1]]

    # back is a edge pixel
    back = section[-2][0], section[-2][1]

    # avoid local minima
    iterations = 0
    loop_grads = CircularList(3)
    excluded_grad = None

    while next_value != 2:  # edge pixel
        aux_value = 0
        i = 0

        if iterations == 3:
            list_loop_grads = loop_grads.get_list()
            excluded_grad = list_loop_grads[1]
            crossing_section_direction[:] = []
            iterations = 0

        # blank pixel or miscellaneous and i < len
        while aux_value < 2 and i < len(grads):
            if grads[i][0] == excluded_grad:
                continue

            delta = grads[i][0]
            aux = np.add(next, delta)

            if aux[0] == back[0] and aux[1] == back[
                    1]:  # back[0] >= 0 and back[1] >= 0 and
                aux_value = 0
            else:
                aux_value = classified_pixels[aux[0], aux[1]]

            i += 1

        if aux_value < 2 and i == len(grads):
            delta = get_direction(classified_pixels, back, next, grads,
                                 excluded_grad)

            loop_grads.insert(delta)
            back = next[0], next[1]
            next = np.add(next, delta)
            next_value = classified_pixels[next[0], next[1]]
        else:
            loop_grads.insert(delta)
            back = next[0], next[1]
            next = aux
            next_value = aux_value

        crossing_section_direction.append(next)
        iterations += 1

    return crossing_section_direction