예제 #1
0
def get_basic_section(start, classified_pixels, start_back=None):
    # 'gradient' vector
    delta = np.array([0, 0])

    last_gradients = CircularList()

    section = []
    section.append(start)

    x, y = start
    next, next_value = get_max_neighbor(classified_pixels, x, y, start_back)
    delta = np.subtract(next, start)

    while next_value == 2:  # edge pixel
        last_gradients.insert((delta[0], delta[1]))
        section.append(next)

        next = np.add(next, delta)
        next_value = classified_pixels[next[0], next[1]]

        if next_value < 2:  # blank pixel or miscellaneous pixel
            last = section[-1]  # get last element added in section
            x, y = last
            back = np.subtract(last, delta)

            # get max value in the neighborhood, unless the 'back'
            next, next_value = get_max_neighbor(classified_pixels, x, y, back)

            delta = np.subtract(next, last)

    last_gradients.insert((delta[0], delta[1]))
    section.append(next)
    last_element = next

    return section, last_gradients, last_element
예제 #2
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