转自:https://blog.csdn.net/m_buddy/article/details/78887248

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/m_buddy/article/details/78887248
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 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)

4. 结果
上面已经将算法的原理介绍了,很简单实现起来也很容易。这里给出几幅实验的结果作为参考
case1:

case2:

从结果可以看到在一些图片中匹配的效果并不是很好,应该是抽取的特征致使匹配的结果出现偏差
————————————————
版权声明:本文为CSDN博主「m_buddy」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m_buddy/article/details/78887248

感知哈希算法——Python实现【转】的更多相关文章

  1. 感知哈希算法 python 3.4

    #!/usr/bin/python # -*- coding: UTF-8 -*- #Less than 10 add to list and sort import glob import os i ...

  2. 感知哈希算法的java实现

    一.原理讲解      实现这种功能的关键技术叫做"感知哈希算法"(Perceptual Hash Algorithm), 意思是为图片生成一个指纹(字符串格式), 两张图片的指纹 ...

  3. 谷歌百度以图搜图 "感知哈希算法" C#简单实现

    /// <summary> /// 感知哈希算法 /// </summary> public class ImageComparer { /// <summary> ...

  4. Iconfinder 如何杜绝盗版,哈希算法检测图像重复

    原地址:http://blog.jobbole.com/65914/ 本文由 伯乐在线 - 小鱼 翻译自 Silviu Tantos.欢迎加入技术翻译小组.转载请参见文章末尾处的要求. [伯乐在线导读 ...

  5. 压缩感知重构算法之IRLS算法python实现

    压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...

  6. 压缩感知重构算法之OLS算法python实现

    压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...

  7. 压缩感知重构算法之CoSaMP算法python实现

    压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...

  8. 压缩感知重构算法之IHT算法python实现

    压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...

  9. 压缩感知重构算法之SP算法python实现

    压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...

随机推荐

  1. LR11录制手机/pad App脚本多种方法介绍(Mobile App补丁包)

    总体来说,通过LR录制手机脚本的方式有三种:1)通过代理方式录制,保证手机电脑在同一个网段:2)通过抓包录制,在手机上安装Mobile Recorder:3)通过安卓模拟器录制,本地安装android ...

  2. 九、Swift对象存储服务(双节点搭建)

    九.Swift对象存储服务(双节点搭建) 要求:Controoler节点需要2块空盘 Compute节点需要再加2块空盘 本次搭建采用Controller 和 Compute双节点节点做swift组件 ...

  3. 6、zabbix自定义监控

    一.概述 为什么需要自定义监控呢? 虽然zabbix已经给我们准备好了很多的模板,但是有的东西还是无法监控,这时候就要我们自定义监控了. 自定义监控的思路? 比如我们现在想要监控这个值,如下所示,模板 ...

  4. tensorflow的MNIST教程

    (ps:根据自己的理解,提炼了一下官方文档的内容,错误的地方希望大佬们多多指正.....) 0x01:数据集的获取和表示 数据集的获取,可以通过代码自动下载.这里的数据就是各种手写数字图片和图片对应的 ...

  5. 配置Ngnix1.15.11+php5.4出现502 Bad Gateway问题

    今天在调试Ngnix1.15.11+php5.4网站时候,因为网站数据和并发过大,出现502 Bad Gateway问题,所以记下笔记. 只需要修改php-fpm.conf的request_termi ...

  6. centos7虚拟机端口命令

    cat /etc/redhat-release  #  查看centos 版本 Centos7端口常见命令 虚拟机新开了5005端口,系统内部是显示开了的,但是外部不能访问端口. 一些需要用到的命令: ...

  7. vmvare虚拟机篇

    新建虚拟机-典型-稍后安装-Linux-管理-从磁盘删除-虚拟机名称-位置- 安装Tools-用于虚拟机和本地文件共享和传送 网络适配器桥接模式-桥接本地网卡 NAT模式-再重新连接本地网卡 仅主机模 ...

  8. 第02组Beta版本演示

    组长博客 组名:十一个憨比 本组组员: 学号 姓名 分工 贡献比例 181700413 黄智 写Beta冲刺的四次博客,写评审表,写word,统筹规划 9% 131700309 林闽沪 代码实现,答辩 ...

  9. Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) C. p-binary 水题

    C. p-binary Vasya will fancy any number as long as it is an integer power of two. Petya, on the othe ...

  10. git pull --rebase的理解

    在使用git的过程中经常需要使用到git pull命令,在更新远端代码的同时如果与本地代码产生冲突了, 那么冲突的文件中就出现了需要手动合并的部分,而git pull --rebase不同的地方则是当 ...