我在python中创建了一个能量函数,我将其应用于png图像。但是,当我输入参数时,我没有得到返回的能量值。谁能明白为什么会这样?谢谢!
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img=mpimg.imread('Image.png')
#plt.imshow(img)
#plt.show()
im=np.array(img
def E_generator(beta, eta, h):
"""Generate energy function E and localized version of E.
E = h * \sum{x_i} - beta * \sum{x_i x_j} - eta * \sum{x_i y_i}
"""
def E(x, y):
"""Calculate energy for matrices x, y.
"""
# sum of products of neighboring paris {xi, yi}
xxm = np.zeros_like(x)
xxm[:-1, :] = x[1:, :] # down
xxm[1:, :] += x[:-1, :] # up
xxm[:, :-1] += x[:, 1:] # right
xxm[:, 1:] += x[:, :-1] # left
xx = np.sum(xxm * x)
xy = np.sum(x * y)
xsum = np.sum(x)
return h * xsum - beta * xx - eta * xy
return E
y = np.array(img)
x = np.array(y)
E_generator(0,1,1)
然后输出返回:" .E>"
答案 0 :(得分:2)
你的函数E_generator返回一个函数(E);要获得结果,您需要调用该函数,因此您可以执行以下操作:
print E_generator(0,1,1)(x, y)