感知哈希算法——Python实现

来源:互联网 发布:php微信抢红包源代码 编辑:程序博客网 时间:2024/06/06 00:46

1. 前言

现在手中只有一张图像需要在一个集合中去找到与之最相近的那一张,这个过程实际是一个匹配的过程,特别是在多模态医学图像中解决这样的问题是比较迫切的,今年试验了一种广泛使用的算法——感知哈希算法!具体的实验结果将在下文中给出。

2. 算法原理

step1:缩小图片尺寸
将图片缩小到8x8的尺寸, 总共64个像素. 这一步的作用是去除各种图片尺寸和图片比例的差异, 只保留结构、明暗等基本信息。
这里写图片描述
step2:转为灰度图片
将缩小后的图片, 转为64级灰度图片。
这里写图片描述
step3:计算灰度平均值
计算图片中所有像素的灰度平均值
step4:比较像素的灰度
将每个像素的灰度与平均值进行比较, 如果大于或等于平均值记为1, 小于平均值记为0。
step5:计算哈希值
将上一步的比较结果, 组合在一起, 就构成了一个64位的二进制整数, 这就是这张图片的指纹。
step6:对比图片指纹
得到图片的指纹后, 就可以对比不同的图片的指纹, 计算出64位中有多少位是不一样的. 如果不相同的数据位数不超过5, 就说明两张图片很相似, 如果大于10, 说明它们是两张不同的图片。

3. Python实现

# -*- coding=utf-8 -*-import numpy as npfrom PIL import Imageimport matplotlib.pyplot as plt# extract feature# lines: src_img pathdef 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 imagesdef 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 sourcesrc_path = './1794-MR/MR.txt'  # *.txt file contain MRI imagesdst_path = './1794-CT/CT.txt'  # *.txt file contain CT imagesmri_lines = [line.strip() for line in open(src_path)]ct_lines = [line.strip() for line in open(dst_path)]# extract featureset_size = 8mri_feature = Extra_Featrue(mri_lines, set_size, set_size)ct_feature = Extra_Featrue(ct_lines, set_size, set_size)# use feature to find candidate imgMRIFindCT(mri_feature, mri_lines, ct_feature, ct_lines)

4. 结果

上面已经将算法的原理介绍了,很简单实现起来也很容易。这里给出几幅实验的结果作为参考
case1:
1
case2:
2
从结果可以看到在一些图片中匹配的效果并不是很好,应该是抽取的特征致使匹配的结果出现偏差

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 刘婷户外抵债天台在线 天枼 天葉 秘宝之主叶天南 上门狂婿叶天 狂婿叶天 上门狂婿叶天完整免费 林天叶婉清 叶天南作品 上门狂婿叶天完整 叶站天楚灵儿 超级医生叶天南 九天神尊叶辰全文阅读 叶小天 妙手狂医叶无天 叶陵天李雨欣 修真归来叶天 网游之天行失落叶 上门狂婿叶天免费版 上门狂婿叶天免费 叶天全文免费阅读 天叶多少钱 大天叶 小天叶 天叶烟 大天叶图片 天叶多少钱一包 大天叶多少钱一条 天叶价格 天叶图片 天之叶 黄金烟 6901028169967 6901028169998 黄金枼 腹黑萌宝闹翻天叶蓁 都市修真归来叶天 妖孽弃少都市叶天 都市至强修罗叶天 都市之修真归来叶天 妖孽弃少在都市叶天