首页 > 解决方案 > 如何在 Python 中的列表上运行算法并将结果存储在列表中?

问题描述

下面代码中显示的算法计算该ellipse函数在 Python 中绘制的椭球图像与另一张黑白图像之间的切比雪夫空间距离。

一只忙碌的猫

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,我预先定义了 myDXDYto (5,5)。我想将DX和的值更改为DY上面的值(in 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))

标签: pythonarraysfunctionnumpy

解决方案


更新以显示两种方法。第一种方法是将它放在下面显示的函数中。制作一个列表来临时存储结果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

推荐阅读