0

我在 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>”

4

1 回答 1

2

您的函数 E_generator 返回一个函数 (E);要获得结果,您需要调用该函数,因此您可以执行以下操作:

print E_generator(0,1,1)(x, y)
于 2015-05-18T08:26:15.860 回答