如何在Python中的列表上运行算法并将结果存储在列表中?

时间:2019-07-17 12:23:13

标签: python arrays function numpy

以下代码中所示的算法计算了ellipse函数在Python中绘制的椭圆图像与另一幅黑白图像之间的切比雪夫空间距离。

a busy cat

translate是将椭圆形的每个点移动一定距离的功能。我想观察一下切比雪夫距离如何随不同的平移值而变化。我有一个列表,其中包含我想尝试的成对平移坐标:

translation_points = 
[ (5, 6), (5, 7), (5, 8), (5, 9), (5, 10), (5, 11), (5, 12), (5, 13), (5, 14), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (6, 10), (6, 11), (6, 12), (6, 13), (6, 14), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (7, 10), (7, 11), (7, 12), (7, 13), (7, 14), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (8, 10), (8, 11), (8, 12), (8, 13), (8, 14), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9), (9, 10), (9, 11), (9, 12), (9, 13), (9, 14), (10, 5), (10, 6), (10, 7), (10, 8), (10, 9), (10, 10), (10, 11), (10, 12), (10, 13), (10, 14), (11, 5), (11, 6), (11, 7), (11, 8), (11, 9), (11, 10), (11, 11), (11, 12), (11, 13), (11, 14), (12, 5), (12, 6), (12, 7), (12, 8), (12, 9), (12, 10), (12, 11), (12, 12), (12, 13), (12, 14), (13, 5), (13, 6), (13, 7), (13, 8), (13, 9), (13, 10), (13, 11), (13, 12), (13, 13), (13, 14), (14, 5), (14, 6), (14, 7), (14, 8), (14, 9), (14, 10), (14, 11), (14, 12), (14, 13), (14, 14)]

从下面的translate中可以看出,我将DXDY预定义为(5,5)。我想将DXDY的值更改为上面的值(在translation_points中),并将得到的空间距离存储在列表中。如何更新代码以将列表传递给算法并输出列表?

这是我现有的算法代码:

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from scipy.spatial import distance
import scipy.misc
im = scipy.misc.imread(r'Downloads/irregular1.png', flatten=False, mode='L')


def ellipse(x, y):
    value = (x*x) + (y*y)/3
    if (value >= 600):
        return 0
    else:
        return 1

def translate(x, y):
    DX = 5
    DY = 5
    return (x- DX, y - DY)

def rotate(x, y):
    theta = np.radians(45)
    matrix = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]])
    return np.dot(matrix, (x,y))

data = np.zeros((100,100))

for i in range(0, 100):
    for j in range(0, 100):
        (x, y) = translate(i,j)
        (x, y) = rotate(x, y)
        data[i,j] = ellipse(x, y)

plt.imshow(data, cmap="gray")
#plt.show()


plt.imshow(im)
#plt.show()

counter = 0 #tracking white
counter1 = 0 #tracking black 

#getting the dimensions of the image -> y
yDim = im.shape[0]

#getting the dimensions of the image -> x
xDim = im.shape[1]

for i in range(yDim):
    for j in range (xDim): 
        if np.any(im[i,j]) == 0:
            counter += 1
        else: 
            counter1 += 1

#initialize empty array this array will receive all the white pixels 
a = np.empty([100,100])

for i in range(yDim):
    for j in range (xDim): 
        if np.any(im[i,j]) == 0:
            np.append(a,im[i,j],axis=None)

#spatial distance 
a = a.flatten()
data = data.flatten()

distance = distance.hamming(a,data))

1 个答案:

答案 0 :(得分:1)

已更新以显示两种方法。第一种方法是将其包含在下面显示的功能中。创建一个列表以临时存储结果temp,循环遍历函数中定义的列表translation_points,减去差并将其添加到我们的临时列表temp中。一旦循环遍历列表translation_points,便返回存储所有新点的温度。

def translate(x, y):
    translation_points = [ (5, 6), (5, 7), (5, 8), (5, 9), (5, 10), (5, 11), (5, 12), (5, 13), (5, 14), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (6, 10), (6, 11), (6, 12), (6, 13), (6, 14), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (7, 10), (7, 11), (7, 12), (7, 13), (7, 14), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (8, 10), (8, 11), (8, 12), (8, 13), (8, 14), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9), (9, 10), (9, 11), (9, 12), (9, 13), (9, 14), (10, 5), (10, 6), (10, 7), (10, 8), (10, 9), (10, 10), (10, 11), (10, 12), (10, 13), (10, 14), (11, 5), (11, 6), (11, 7), (11, 8), (11, 9), (11, 10), (11, 11), (11, 12), (11, 13), (11, 14), (12, 5), (12, 6), (12, 7), (12, 8), (12, 9), (12, 10), (12, 11), (12, 12), (12, 13), (12, 14), (13, 5), (13, 6), (13, 7), (13, 8), (13, 9), (13, 10), (13, 11), (13, 12), (13, 13), (13, 14), (14, 5), (14, 6), (14, 7), (14, 8), (14, 9), (14, 10), (14, 11), (14, 12), (14, 13), (14, 14)]
    temp = []
    for i in translation_points:
        temp.append((x-i[0],y-i[1]))
    return temp

print(translate(5,5))

另一种方法是将元组列表作为参数输入并执行相同的过程。可以通过更改translation_points来提供更多功能。

translation_points = [ (5, 6), (5, 7), (5, 8), (5, 9), (5, 10), (5, 11), (5, 12), (5, 13), (5, 14), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (6, 10), (6, 11), (6, 12), (6, 13), (6, 14), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (7, 10), (7, 11), (7, 12), (7, 13), (7, 14), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (8, 10), (8, 11), (8, 12), (8, 13), (8, 14), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9), (9, 10), (9, 11), (9, 12), (9, 13), (9, 14), (10, 5), (10, 6), (10, 7), (10, 8), (10, 9), (10, 10), (10, 11), (10, 12), (10, 13), (10, 14), (11, 5), (11, 6), (11, 7), (11, 8), (11, 9), (11, 10), (11, 11), (11, 12), (11, 13), (11, 14), (12, 5), (12, 6), (12, 7), (12, 8), (12, 9), (12, 10), (12, 11), (12, 12), (12, 13), (12, 14), (13, 5), (13, 6), (13, 7), (13, 8), (13, 9), (13, 10), (13, 11), (13, 12), (13, 13), (13, 14), (14, 5), (14, 6), (14, 7), (14, 8), (14, 9), (14, 10), (14, 11), (14, 12), (14, 13), (14, 14)]

def translate(x,y,l): 
    temp = []
    for i in l:
        temp.append((x-i[0],y-i[1]))
    return temp