## -*- coding: utf-8 -*-
# !/usr/bin/env python
# 余弦相似度计算
import time

import cv2
from PIL import Image
from numpy import average, dot, linalg
# 对图片进行统一化处理
def get_thum(image, size=(64, 64), greyscale=False):
    # 利用image对图像大小重新设置, Image.ANTIALIAS为高质量的
    image = image.resize(size, Image.ANTIALIAS)
    if greyscale:
        # 将图片转换为L模式，其为灰度图，其每个像素用8个bit表示
        image = image.convert('L')
    return image
# 计算图片的余弦距离
def image_similarity_vectors_via_numpy(image1, image2):
    image1 = get_thum(image1)
    image2 = get_thum(image2)
    images = [image1, image2]
    vectors = []
    norms = []
    for image in images:
        vector = []
        for pixel_tuple in image.getdata():
            vector.append(average(pixel_tuple))
        vectors.append(vector)
        # linalg=linear（线性）+algebra（代数），norm则表示范数
        # 求图片的范数
        norms.append(linalg.norm(vector, 2))
    a, b = vectors
    a_norm, b_norm = norms
    # dot返回的是点积，对二维数组（矩阵）进行计算
    res = dot(a / a_norm, b / b_norm)
    return res


#差值感知算法
def dHash(image):
    #缩放9*8
    image=cv2.resize(image,(9,8),interpolation=cv2.INTER_CUBIC)
    #转换灰度图
    image=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    #     print(image.shape)
    hash=[]
    #每行前一个像素大于后一个像素为1，相反为0，生成哈希
    for i in range(8):
        for j in range(8):
            if image[i,j]>image[i,j+1]:
                hash.append(1)
            else:
                hash.append(0)
    return hash

#计算汉明距离
def Hamming_distance(hash1,hash2):
    num = 0
    for index in range(len(hash1)):
        if hash1[index] != hash2[index]:
            num += 1
    return num

if __name__ == '__main__':
    # 把图片表示成一个向量，通过计算向量之间的余弦距离来表征两张图片的相似度 0.9760
    image1 = cv2.imread('../image/1.jpg')
    image2 = cv2.imread('../image/2.jpg')
    # image3 = cv2.imread('image/AI2.jpg')
    # image4 = cv2.imread('image/AI3.jpg')
    # image5 = cv2.imread('image/AI4.jpg')
    # image6 = cv2.imread('a.jpg')
    # image7 = cv2.imread('AI.jpg')
    hash1 = dHash(image1)
    hash2 = dHash(image2)
    # hash3 = dHash(image3)
    # hash4 = dHash(image4)
    # hash5 = dHash(image5)
    # hash6 = dHash(image6)
    # hash7 = dHash(image7)
    start = time.time()
    dist = Hamming_distance(hash1, hash2)
    #将距离转化为相似度
    similarity = 1 - dist * 1.0 / 64
    # print(dist)
    print(similarity, time.time() - start)
    # cosin = image_similarity_vectors_via_numpy(image1, image2)
    # print('图片余弦相似度', cosin)
    # cosin1 = image_similarity_vectors_via_numpy(image1, image3)
    # print('图片余弦相似度', cosin1)
    # cosin2 = image_similarity_vectors_via_numpy(image1, image4)
    # print('图片余弦相似度', cosin2)
    # cosin3 = image_similarity_vectors_via_numpy(image1, image5)
    # print('图片余弦相似度', cosin3)
    # cosin4 = image_similarity_vectors_via_numpy(image5, image6)
    # print('图片余弦相似度', cosin4)
    # cosin5 = image_similarity_vectors_via_numpy(image1, image6)
    # print('图片余弦相似度', cosin5)