Правильный и эффективный способ найти геометрическую длину кривой одного пикселя? - PullRequest
0 голосов
/ 17 февраля 2019

У меня есть изображение, образованное кривыми шириной в один пиксель (один элемент в соответствующем массиве numpy).

Метод, который я использую, состоит в том, чтобы вручную добавлять расстояния между пикселями без перезаписи, начиная с одного конца.

Я бы хотел, чтобы эти расстояния были эффективнее.Большое спасибо, что нашли время, чтобы прочитать это.

Извините, если мой код слишком длинный, поэтому я частично знаю, что должен быть более эффективный способ.

Результат в этом примереis => dis = 2*sqrt(2) + 1

import numpy as np

# Example image already load as a numpy array 
img = np.array(
    [[1,0,0],
     [0,1,0],
     [0,1,0],
     [0,0,1]])

# add a border of 0's to implement the logic I'm using.

base = np.zeros([len(img_aux) + 2, len(img_aux[0]) + 2])
x = 0
for row in img_aux:
    y = 0
    for element in row:
        base[x + 1][y + 1] = 1 if element else 0
        y += 1
    x += 1

#    result

#    [[0,0,0,0,0]
#     [0,1,0,0,0],
#     [0,0,1,0,0],
#     [0,0,1,0,0],
#     [0,0,0,1,0]
#     [0,0,0,0,0]]

# build a base image where the pixels tell the amount of neighbors he has
# if i dont add the border this kind of operation will give me an exception

aux = base.copy()
for x in range(len(base)):
    for y in range(len(base[0])):
        if base[x][y] == 1:
            aux[x][y] = base[x - 1][y - 1] +
                        base[x - 1][y] +
                        base[x - 1][y + 1] +
                        base[x][y - 1] +
                        base[x + 1][y - 1] +
                        base[x + 1][y] +
                        base[x + 1][y + 1] +
                        base[x][y + 1]
        else:
            pass

#    result

#    [[0,0,0,0,0,0]
#     [0,1,0,0,0,0],
#     [0,0,2,0,0,0],
#     [0,0,2,0,0,0],
#     [0,0,0,1,0,0]
#     [0,0,0,0,0,0]]

# to make this shorter i asume that i have the coords of any "1" in the array
# asume this func find the coordinate in the pixel map of a random 1

x, y = coords(aux) 


# Here come the logic that find the distance
'''

if i start in the coords [1][1]
[0,0,0,0,0,0]
[0,X,0,0,0,0]
[0,0,2,0,0,0]
[0,0,2,0,0,0]
[0,0,0,1,0,0]
[0,0,0,0,0,0]

i find where is the next pixel whit a value diferent than 0 and sum the distance of the centroids of those 2 pixels, if the pixel are in horizontal or
vertical locations i sum 1 and if the other pixel is in a diag i sum sqrt(2)
the geometrical distance, after that i clean the pixel and find other until i have only one pixel left whitout any neighbors 


'''
end = 1
while end != 0:
    up = True if image[y - 1][x] != 0 else False
    up_right = True if image[y - 1][x + 1] != 0 else False
    right = True if image[y][x + 1] != 0 else False
    down_right = True if image[y + 1][x + 1] != 0 else False
    down = True if image[y + 1][x] != 0 else False
    down_left = True if image[y + 1][x - 1] != 0 else False
    left = True if image[y][x - 1] != 0 else False
    up_left = True if image[y - 1][x - 1] != 0 else False

    end = up + 
        up_right + 
        right + 
        down_right + 
        down + 
        down_left + 
        left + 
        up_left

    if end == 0:
        return dis
    else:
        if up:
            dis = dis + 1
            image[y][x] = 0
            y = y - 1

        if down:
            dis = dis + 1
            image[y][x] = 0
            y = y + 1

        if left:
            dis = dis + 1
            image[y][x] = 0
            x = x - 1

        if right:
            dis = dis + 1
            image[y][x] = 0
            x = x + 1

        if up_left:
            dis = dis + np.sqrt(2)
            image[y][x] = 0
            x = x - 1
            y = y - 1

        if up_right:
            dis = dis + np.sqrt(2)
            image[y][x] = 0
            x = x + 1
            y = y - 1

        if down_left:
            dis = dis + np.sqrt(2)
            image[y][x] = 0
            x = x - 1
            y = y + 1

        if down_right:
            dis = dis + np.sqrt(2)
            image[y][x] = 0
            x = x + 1
            y = y + 1

print(dis)
# this prints: 3.828...
# that is: 2*sqrt(2) + 1 

...