Advertisement

Python:实现感知哈希算法(附完整源码)

阅读量:

本文介绍了一种基于感知哈希算法的Python实现方法。文章主要分为以下内容:首先导入必要的库(如numpy、PIL和matplotlib),并定义了一个名为Extra_Featrue的函数用于从图像中提取特征;接着定义了一个名为MRIFindCT的函数用于利用MRI图像的特征找到候选CT图像;最后展示了如何设置数据源路径,并通过调用上述函数完成整个流程。该方法通过将图像进行二进制化处理,并计算其与目标图像的差异性来实现特征匹配。

Python:实现感知哈希算法

复制代码
    import numpy as np
    
    from PIL import Image
    
    import matplotlib.pyplot as plt
    
    # extract feature
    
    # lines: src_img path
    
    def Extra_Featrue(lines, new_rows=64, new_cols=64):
    
    for name in lines:
    
    ori_img = Image.open(name.strip())
    
    feature_img = ori_img.resize((new_rows, new_cols))
    
    feature_img = feature_img.convert('L')
    
    mean_value = np.mean(np.mean(feature_img))
    
    feature = feature_img >= mean_value
    
    feature = np.matrix(feature, np.int8)
    
    if 'features' in locals():
    
    temp = np.reshape(feature, (1, new_cols * new_rows))
    
    features = np.vstack([features, temp])
    
    else:
    
    features = np.matrix(np.reshape(feature, (1, new_cols * new_rows)))
    
    return features
    
    # use MRI image features to find candidate CT images
    
    def MRIFindCT(mri_feature, mri_lines, ct_feature, ct_lines):
    
    for i in np.arange(0, np.shape(mri_feature)[0]):
    
    dist = []
    
    mri = mri_feature[i, :]
    
    for j in np.arange(0, np.shape(ct_feature)[0]):
    
    ct = ct_feature[j, :]
    
    temp = mri != ct
    
    sum = np.sum(temp)
    
    dist.append(sum)
    
    # find minimum while ignoring the zeros on the diagonal
    
    index = np.argsort(dist)
    
    img1_path = mri_lines[i]
    
    img2_path = ct_lines[index[1]]
    
    img3_path = ct_lines[index[2]]
    
    img4_path = ct_lines[index[3]]
    
    img1 = Image.open(img1_path)
    
    img2 = Image.open(img2_path)
    
    img3 = Image.open(img3_path)
    
    img4 = Image.open(img4_path)
    
    plt.subplot(2, 2, 1)
    
    plt.imshow(img1)
    
    plt.title('MRI Image(%s)' % img1_path[img1_path.__len__() - 10:])
    
    plt.axis('off')
    
    plt.xlabel(img1_path[img1_path.__len__() - 10:])
    
    plt.subplot(2, 2, 2)
    
    plt.imshow(img2)
    
    plt.title('Candidate CT Image1(%s)' % img2_path[img2_path.__len__() - 10:])
    
    plt.axis('off')
    
    plt.xlabel(img2_path[img2_path.__len__() - 10:])
    
    plt.subplot(2, 2, 3)
    
    plt.imshow(img3)
    
    plt.title('Candidate CT Image2(%s)' % img3_path[img3_path.__len__() - 10:])
    
    plt.axis('off')
    
    plt.xlabel(img3_path[img3_path.__len__() - 10:])
    
    plt.subplot(2, 2, 4)
    
    plt.imshow(img4)
    
    plt.title('Candidate CT Image3(%s)' % img4_path[img4_path.__len__() - 10:])
    
    plt.axis('off')
    
    plt.xlabel(img4_path[img4_path.__len__() - 10:])
    
    plt.show()
    
    # set data source
    
    src_path = './1794-MR/MR.txt' # *.txt file contain MRI images
    
    dst_path = './1794-CT/CT.txt' # *.txt file contain CT images
    
    mri_lines = [line.strip() for line in open(src_path)]
    
    ct_lines = [line.strip() for line in open(dst_path)]
    
    # extract feature
    
    set_size = 8
    
    mri_feature = Extra_Featrue(mri_lines, set_size, set_size)
    
    ct_feature = Extra_Featrue(ct_lines, set_size, set_size)
    
    # use feature to find candidate img
    
    MRIFindCT(mri_feature, mri_lines, ct_feature, ct_lines)
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI助手

该博文为首发文章内容,请未经作者授权严禁任何形式的转载传播

全部评论 (0)

还没有任何评论哟~