当前位置:   article > 正文

python调用opencv做简单的dhash_python numpy dhash

python numpy dhash

原理很简单:
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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/869097
推荐阅读
相关标签
  

闽ICP备14008679号