我在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> "
函数E_generator返回一个函数(E);要获得结果,需要调用该函数,因此可以这样做:
print E_generator(0,1,1)(x, y)