# coding: utf-8 import operator from PIL import Image import numpy as np import cv2 """圖片處理: 圖片截取、圖片相似度比對、哈希算法比對""" def cmp_pic(pic1, pic2): """ 比對圖片相似度 @param pic1: @param pic2: @return: """ a = Image.open(pic1) b = Image.open(pic2) return operator.eq(a, b) def image_interception(image): """ 圖片截取 @param image: 目標圖片 @return: """ img = cv2.imread(image) print('圖片{}高度、寬度、通道數為:{}'.format(image, img.shape)) # (1792, 828, 3) 高度、寬度、通道數 cropped = img[170:650, 0:900] # 裁剪坐標為[y0:y1, x0:x1] cv2.imwrite(image, cropped) return image def aHash(img): """ 均值哈希算法 @param img: @return: """ # 縮放為8*8 img = cv2.resize(cv2.imread(img), (8, 8)) # 轉換為灰度圖 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # s為像素和初值為0,hash_str為hash值初值為'' s = 0 hash_str = '' # 遍歷累加求像素和 for i in range(8): for j in range(8): s = s + gray[i, j] # 求平均灰度 avg = s / 64 # 灰度大於平均值為1相反為0生成圖片的hash值 for i in range(8): for j in range(8): if gray[i, j] > avg: hash_str = hash_str + '1' else: hash_str = hash_str + '0' return hash_str def dHash(img): """ 差值感知算法 @param img: @return: """ # 縮放8*8 img = cv2.resize(cv2.imread(img), (9, 8)) # 轉換灰度圖 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) hash_str = '' # 每行前一個像素大於后一個像素為1,相反為0,生成哈希 for i in range(8): for j in range(8): if gray[i, j] > gray[i, j + 1]: hash_str = hash_str + '1' else: hash_str = hash_str + '0' return hash_str def pHash(img): """ 感知哈希算法(pHash) @param img: @return: """ # 縮放32*32 img = cv2.resize(cv2.imread(img), (32, 32)) # , interpolation=cv2.INTER_CUBIC # 轉換為灰度圖 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 將灰度圖轉為浮點型,再進行dct變換 dct = cv2.dct(np.float32(gray)) # opencv實現的掩碼操作 dct_roi = dct[0:8, 0:8] hash = [] avreage = np.mean(dct_roi) for i in range(dct_roi.shape[0]): for j in range(dct_roi.shape[1]): if dct_roi[i, j] > avreage: hash.append(1) else: hash.append(0) return hash def classify_hist_with_split(image1, image2, size=(256, 256)): """ 通過得到RGB每個通道的直方圖來計算相似度 @param image1: @param image2: @param size: @return: """ # 將圖像resize后,分離為RGB三個通道,再計算每個通道的相似值 image1 = cv2.resize(cv2.imread(image1), size) image2 = cv2.resize(cv2.imread(image2), size) sub_image1 = cv2.split(image1) sub_image2 = cv2.split(image2) sub_data = 0 for im1, im2 in zip(sub_image1, sub_image2): sub_data += calculate(im1, im2) sub_data = sub_data / 3 # print(sub_data) return sub_data def calculate(image1, image2): """ 計算單通道的直方圖的相似值 @param image1: @param image2: @return: """ hist1 = cv2.calcHist([image1], [0], None, [256], [0.0, 255.0]) hist2 = cv2.calcHist([image2], [0], None, [256], [0.0, 255.0]) # 計算直方圖的重合度 degree = 0 for i in range(len(hist1)): if hist1[i] != hist2[i]: degree = degree + (1 - abs(hist1[i] - hist2[i]) / max(hist1[i], hist2[i])) else: degree = degree + 1 degree = degree / len(hist1) return degree def cmpHash(hash1, hash2): """ Hash值對比 @param hash1: @param hash2: @return: """ n = 0 # hash長度不同則返回-1代表傳參出錯 if len(hash1) != len(hash2): return -1 # 遍歷判斷 for i in range(len(hash1)): # 不相等則n計數+1,n最終為相似度 if hash1[i] != hash2[i]: n = n + 1 return n image_interception('11.png') image_interception('11.png') img1 = '1.png' img2 = '2.png' hash1 = aHash(img1) hash2 = aHash(img2) n = cmpHash(hash1, hash2) print('均值哈希算法相似度:', n) hash1 = dHash(img1) hash2 = dHash(img2) n = cmpHash(hash1, hash2) print('差值哈希算法相似度:', n) hash1 = pHash(img1) hash2 = pHash(img2) n = cmpHash(hash1, hash2) print('感知哈希算法相似度:', n) n = classify_hist_with_split(img1, img2) print('三直方圖算法相似度:', n)