赞
踩
原理很简单:
1 读取图像,变为灰度图,缩放到8*9
2 每行前8个像素,分别和后面一个像素比较,大于为1,否则为0,得到8*8的dhash
3 2个图片的dhash直接做汉明距比对,得到距离
# -*- coding: utf-8 -*-
import os
import sys
import argparse
import numpy
from numpy import linalg as la
import cv2
#差异hash
def dhashFeature(pic_name):
print ("dhashFeature")
image = cv2.imread(pic_name)
res_image = cv2.resize(image,(9,8))
gray = cv2.cvtColor(res_image,cv2.COLOR_RGB2GRAY)
# 比较相邻像素
difference = [0 for i in range(64)]
for row in range(8):
for col in range(8):
if( gray[row][col] > gray[row][col+1] ):
a = 1
else:
a = 0
difference[row*8+col] = a
return (difference)
#汉明距离 按字符比较
def hammingSimilar(inA,inB):
a = 0
for i in range(len(inA)):
if(inA[i] != inB[i]):
a += 1
return a
#读取图片调用
feature1 = dhashFeature("image_0001.jpg")
#print(feature1)
feature2 = dhashFeature("image_0012.jpg")
#print(feature2)
diff = hammingSimilar(feature1,feature2)
print(diff)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。