如何在python中计算二维图像的互信息

2024-05-28 18:22:43 发布

您现在位置:Python中文网/ 问答频道 /正文

图像存储在images_values

例如,276个具有列x行的图像

images_values.shape = (276, 1080, 1920)

如何正确地将其传递给以下函数以计算两个图像之间的互信息?i、 e.images_values[0,:,:]images_values[1,:,:]

from scipy import ndimage

EPS = np.finfo(float).eps

 def mutual_information_2d(x, y, sigma=1, normalized=False):
    """
    Computes (normalized) mutual information between two 1D variate from a
    joint histogram.
    Parameters
    ----------
    x : 1D array
        first variable
    y : 1D array
        second variable
    sigma: float
        sigma for Gaussian smoothing of the joint histogram
    Returns
    -------
    nmi: float
        the computed similariy measure
    """
    bins = (256, 256)



    jh = np.histogram2d(x, y, bins=bins)[0]

    # smooth the jh with a gaussian filter of given sigma
    ndimage.gaussian_filter(jh, sigma=sigma, mode='constant',
                                 output=jh)

    # compute marginal histograms
    jh = jh + EPS
    sh = np.sum(jh)
    jh = jh / sh
    s1 = np.sum(jh, axis=0).reshape((-1, jh.shape[0]))
    s2 = np.sum(jh, axis=1).reshape((jh.shape[1], -1))

    # Normalised Mutual Information of:
    # Studholme,  jhill & jhawkes (1998).
    # "A normalized entropy measure of 3-D medical image alignment".
    # in Proc. Medical Imaging 1998, vol. 3338, San Diego, CA, pp. 132-143.
    if normalized:
        mi = ((np.sum(s1 * np.log(s1)) + np.sum(s2 * np.log(s2)))
                / np.sum(jh * np.log(jh))) - 1
    else:
        mi = ( np.sum(jh * np.log(jh)) - np.sum(s1 * np.log(s1))
               - np.sum(s2 * np.log(s2)))

    return mi

Tags: ofthe图像lognpfloatsigmavalues
1条回答
网友
1楼 · 发布于 2024-05-28 18:22:43

你可以把这个当作

mi=mutual_information_2d(images_values[0,:,:].ravel(), images_values[1,:,:].ravel())

ravel将按照mutual_information_2d函数的预期,将2d数组重塑为1d

我正确理解你的问题了吗

相关问题 更多 >

    热门问题