我怎样才能让这个算法重复 dx,dy 的不同值并将这些值存储在数组中

How can I make this algorithm repeat over different values of dx,dy and store the values in an array

我想用相同的算法在 DX、DY 值范围内重复,而不是我在翻译函数中设置值。

我使用的图像是抽象形状的简单二值图像。我想要一组距离,这些距离是根据 dx,dy

的不同值计算的

我相信我正在尝试做的是某种形式的蛮力,以便找到提供最佳距离的最佳 dx,dy

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'C:\Users\mbore\Pictures\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 = 45
    DY = 75
    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)
        #np.append(data,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(data,a)
print (distance)

我不完全理解你的问题(也许你可以更笼统地问它,例如绘图部分与核心问题无关)但如果你只是想 运行 不同的代码parameters 你可以将你的函数扩展到 generall DX and DY:

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

DX_DY_list = [(1,1),(2,3),(3,4)]

for DX, DY in DX_DY_list:

    result  = translate(1, 2, DX, DY)               
    print(result)     

另一种(更先进的)方法是对您的代码进行一般化并使用 argparse,因为我发现它是一个非常有用的参数研究工具。