计算三维图像的互信息值

2024-06-28 11:05:58 发布

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

我想问一下,如何使用Python计算两幅三维图像的互信息值和标准化互信息值?任何帮助都将不胜感激。 我曾尝试使用sklearn中的函数来计算互信息值a,但我发现在旋转或平移图像后再次计算互信息时,会获得值B。a和B之间的差异非常小,所以我个人认为这种方法不适用于图像。我说的对吗?这是我在sklearn软件包中使用的代码

def NMI(img1,img2):
    img1_ = sitk.GetArrayFromImage(img1)
    img2_ = sitk.GetArrayFromImage(img2)
    img2_ = np.reshape(img2_, -1)
    img1_ = np.reshape(img1_, -1)
    normalized_mutual_infor = mr.normalized_mutual_info_score(img1_, img2_)
    nmi = normalized_mutual_infor 
    print(nmi)

fixed_image = sitk.ReadImage(r"D:\Lung CT\RIDER Lung CT\001_1.mha", sitk.sitkFloat32)
moving_image = sitk.ReadImage(r"D:\Lung CT\RIDER Lung CT\001_2.mha", sitk.sitkFloat32)
tfm1 = sitk.ReadTransform(r'D:\6freedom\1_text2.tfm')
x = tfm1.GetParameters()[3]
y = tfm1.GetParameters()[4]
z = tfm1.GetParameters()[5]

transform1 = sitk.Euler3DTransform(tfm1)
transform1.SetParameters((0, 0, 0, x, y, z))
resample = sitk.Resample(moving_image, fixed_image, transform1, sitk.sitkLinear, 0.0, moving_image.GetPixelID())
NMI(fixed_image, resample)
#When the parameter is (0,0,0, x, y, z), the result is 0.524628297588729
#When the parameter is (1,0,0, x, y, z), the result is 0.4657578384754303
#The unit of rotation is radians, so the image has been rotated a lot, 
#but the difference between the two results is very small.

Tags: the图像imageisfixedimg1img2ct
1条回答
网友
1楼 · 发布于 2024-06-28 11:05:58

从这个漂亮的notebook,似乎可以使用输入图像的联合直方图,例如

import numpy as np
def mutual_information(hgram):
     # Mutual information for joint histogram
     # Convert bins counts to probability values
     pxy = hgram / float(np.sum(hgram))
     px = np.sum(pxy, axis=1) # marginal for x over y
     py = np.sum(pxy, axis=0) # marginal for y over x
     px_py = px[:, None] * py[None, :] # Broadcast to multiply marginals
     # Now we can do the calculation using the pxy, px_py 2D arrays
     nzs = pxy > 0 # Only non-zero pxy values contribute to the sum
     return np.sum(pxy[nzs] * np.log(pxy[nzs] / px_py[nzs]))
hist_2d, x_edges, y_edges = np.histogram2d(img1.ravel(),img2.ravel(),bins=20)
mi = mutual_information(hist_2d)
print(mi)

相关问题 更多 >