圖像哈希:均值哈希、感知哈希、差值哈希
1、均值哈希
long aHash(const Mat &srcMat) {
Mat dstMat;
resize(srcMat, dstMat, Size(8, 8));
// cvtColor(dstMat, dstMat, COLOR_BGR2GRAY);
double iAvg = mean(dstMat)[0];
// printf("均值哈希: %lf \n", iAvg);
int p = 1;
long value = 0;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
p <<= 1;
if (dstMat.at<uchar>(i, j) >= iAvg) {
value |= p;
}
}
}
// printf("%ld ;\n", value);
dstMat.release();
return value;
}
2、余玄感知哈希算法;
/**
* 感知哈希算法
* @param img
*/
void pHash32(cv::Mat img, bool gray,unsigned char **result) {
cv::Mat dstMat;
cv::resize(img, dstMat, cv::Size(32, 32));
if (gray)
cv::cvtColor(dstMat, dstMat, cv::COLOR_BGR2GRAY);
dstMat.convertTo(dstMat, CV_32F);
cv::Mat srcDCT;
cv::dct(dstMat, srcDCT);
srcDCT = cv::abs(srcDCT);
cv::Mat finalMat;
cv::resize(srcDCT,finalMat,cv::Size(8,8));
double average = cv::mean(finalMat)[0];
// printf("感知哈希2 %lf \n", average);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (srcDCT.at<float>(i, j) >= average) {
result[i][j] = 1;
} else {
result[i][j] = 0;
}
}
}
// printf("%ld ;\n", value);
dstMat.release();
srcDCT.release();
}
延伸:三種感知算法:
# ---------------------------------------------------------------------------------------------------------------------
# 測試:為什么要縮放DCT?DCT縮放方式有哪些?不同DCT縮放方式有何不同?不進(jìn)行DCT縮放效果會(huì)怎么樣?
# ---------------------------------------------------------------------------------------------------------------------
import cv2
import time
import numpy as np
import matplotlib.pyplot as plt
# DCT變換后:無特征頻率區(qū)域縮放,使用整個(gè)32x32圖像塊的頻率分布,計(jì)算整個(gè)DCT系數(shù)的均值,并根據(jù)這個(gè)均值生成哈希值。
def get_pHash1(img_path):
img = cv2.cvtColor(cv2.imread(img_path), cv2.COLOR_BGR2RGB)
# plt.imshow(img, cmap='gray')
# plt.show()
img = cv2.resize(img, (32, 32), cv2.INTER_CUBIC)
# plt.imshow(img, cmap='gray')
# plt.show()
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# plt.imshow(img_gray, cmap='gray')
# plt.show()
img_dct = cv2.dct(np.float32(img_gray))
# 顯示DCT系數(shù)的圖像
# dct_scaled = cv2.normalize(img_dct, None, 0, 255, cv2.NORM_MINMAX)
# img_dct_scaled = dct_scaled.astype(np.uint8)
# plt.imshow(img_dct_scaled, cmap='gray')
# plt.show()
img_avg = np.mean(img_dct)
# print(f"DCT變換后圖像塊的均值={img_avg}")
img_hash_str = get_img_hash_binary(img_dct, img_avg)
# print(f"圖像的二進(jìn)制哈希值={img_hash_str}")
img_hash = get_img_hash(img_hash_str)
return img_hash
# DCT變換后:將DCT系數(shù)的大小顯式地調(diào)整為8x8,使用8x8的DCT系數(shù)塊的頻率分布,計(jì)算調(diào)整后的DCT系數(shù)的均值,并生成哈希值。
def get_pHash2(img_path):
img = cv2.cvtColor(cv2.imread(img_path), cv2.COLOR_BGR2RGB)
# plt.imshow(img, cmap='gray')
# plt.show()
img = cv2.resize(img, (32, 32), cv2.INTER_CUBIC)
# plt.imshow(img, cmap='gray')
# plt.show()
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# plt.imshow(img_gray, cmap='gray')
# plt.show()
img_dct = cv2.dct(np.float32(img_gray))
img_dct.resize(8, 8)
# 顯示DCT系數(shù)的圖像
# dct_scaled = cv2.normalize(img_dct, None, 0, 255, cv2.NORM_MINMAX)
# img_dct_scaled = dct_scaled.astype(np.uint8)
# plt.imshow(img_dct_scaled, cmap='gray')
# plt.show()
img_avg = np.mean(img_dct)
# print(f"DCT變換后圖像塊的均值={img_avg}")
img_hash_str = get_img_hash_binary(img_dct, img_avg)
# print(f"圖像的二進(jìn)制哈希值={img_hash_str}")
img_hash = get_img_hash(img_hash_str)
return img_hash
# DCT變換后:只提取DCT系數(shù)的左上角8x8塊的信息,然后計(jì)算這個(gè)塊的均值。此法只考慮圖像一小部分的頻率分布,并生成哈希值。
def get_pHash3(img_path):
img = cv2.cvtColor(cv2.imread(img_path), cv2.COLOR_BGR2RGB)
# plt.imshow(img, cmap='gray')
# plt.show()
img = cv2.resize(img, (32, 32), cv2.INTER_CUBIC)
# plt.imshow(img, cmap='gray')
# plt.show()
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# plt.imshow(img_gray, cmap='gray')
# plt.show()
img_dct = cv2.dct(np.float32(img_gray))
dct_roi = img_dct[0:8, 0:8]
# 顯示DCT系數(shù)的圖像
# dct_scaled = cv2.normalize(dct_roi, None, 0, 255, cv2.NORM_MINMAX)
# img_dct_scaled = dct_scaled.astype(np.uint8)
# plt.imshow(img_dct_scaled, cmap='gray')
# plt.show()
img_avg = np.mean(dct_roi)
# print(f"DCT變換后圖像塊的均值={img_avg}")
img_hash_str = get_img_hash_binary(dct_roi, img_avg)
# print(f"圖像的二進(jìn)制哈希值={img_hash_str}")
img_hash = get_img_hash(img_hash_str)
return img_hash
def get_img_hash_binary(img_dct, img_avg):
img_hash_str = ''
for i in range(8):
img_hash_str += ''.join(map(lambda i: '0' if i < img_avg else '1', img_dct[i]))
# print(f"圖像的二進(jìn)制哈希值={img_hash_str}")
return img_hash_str
def get_img_hash(img_hash_str):
img_hash = ''.join(map(lambda x:'%x' % int(img_hash_str[x : x + 4], 2), range(0, 64, 4)))
# print(f"圖像可識別的哈希值={img_hash}")
return img_hash
# 漢明距離:計(jì)算兩個(gè)等長字符串(通常是二進(jìn)制字符串或位字符串)之間的漢明距離。用于確定兩個(gè)等長字符串在相同位置上不同字符的數(shù)量。
def hamming_distance(s1, s2):
# 檢查這兩個(gè)字符串的長度是否相同。如果長度不同,它會(huì)引發(fā) ValueError 異常,因?yàn)闈h明距離只適用于等長的字符串
if len(s1) != len(s2):
raise ValueError("Input strings must have the same length")
distance = 0
for i in range(len(s1)):
# 遍歷兩個(gè)字符串的每個(gè)字符,比較它們在相同位置上的值。如果發(fā)現(xiàn)不同的字符,將 distance 的值增加 1
if s1[i] != s2[i]:
distance += 1
return distance
# ======================================== 測試場景一 ========================================
# img = 'img_test/apple-01.jpg'
# img_hash1 = get_phash1(img)
# img_hash2 = get_phash2(img)
# img_hash3 = get_phash3(img)
# print(f"方式一:DCT變換后,無DCT特征頻率區(qū)域縮放,獲得圖像的二進(jìn)制哈希值={img_hash1}")
# print(f"方式二:DCT變換后,將DCT系數(shù)顯式調(diào)整為8x8,獲得圖像的二進(jìn)制哈希值={img_hash2}")
# print(f"方式三:DCT變換后,只提取DCT系數(shù)左上角8x8像素,獲得圖像的二進(jìn)制哈希值={img_hash3}")
# ======================================== 測試場景二 ========================================
time_start = time.time()
img_1 = 'img_test/apple-01.jpg'
img_2 = 'img_test/apple-02.jpg'
img_3 = 'img_test/apple-03.jpg'
img_4 = 'img_test/apple-04.jpg'
img_5 = 'img_test/apple-05.jpg'
img_6 = 'img_test/apple-06.jpg'
img_7 = 'img_test/apple-07.jpg'
img_8 = 'img_test/apple-08.jpg'
img_9 = 'img_test/apple-09.jpg'
img_10 = 'img_test/pear-001.jpg'
# ------------------------------------- 測試場景二:方式一 --------------------------------------
# img_hash1 = get_pHash1(img_1)
# img_hash2 = get_pHash1(img_2)
# img_hash3 = get_pHash1(img_3)
# img_hash4 = get_pHash1(img_4)
# img_hash5 = get_pHash1(img_5)
# img_hash6 = get_pHash1(img_6)
# img_hash7 = get_pHash1(img_7)
# img_hash8 = get_pHash1(img_8)
# img_hash9 = get_pHash1(img_9)
# img_hash10 = get_pHash1(img_10)
# ------------------------------------- 測試場景二:方式二 --------------------------------------
img_hash1 = get_pHash2(img_1)
img_hash2 = get_pHash2(img_2)
img_hash3 = get_pHash2(img_3)
img_hash4 = get_pHash2(img_4)
img_hash5 = get_pHash2(img_5)
img_hash6 = get_pHash2(img_6)
img_hash7 = get_pHash2(img_7)
img_hash8 = get_pHash2(img_8)
img_hash9 = get_pHash2(img_9)
img_hash10 = get_pHash2(img_10)
# ------------------------------------- 測試場景二:方式三 --------------------------------------
# img_hash1 = get_pHash3(img_1)
# img_hash2 = get_pHash3(img_2)
# img_hash3 = get_pHash3(img_3)
# img_hash4 = get_pHash3(img_4)
# img_hash5 = get_pHash3(img_5)
# img_hash6 = get_pHash3(img_6)
# img_hash7 = get_pHash3(img_7)
# img_hash8 = get_pHash3(img_8)
# img_hash9 = get_pHash3(img_9)
# img_hash10 = get_pHash3(img_10)
distance1 = hamming_distance(img_hash1, img_hash1)
distance2 = hamming_distance(img_hash1, img_hash2)
distance3 = hamming_distance(img_hash1, img_hash3)
distance4 = hamming_distance(img_hash1, img_hash4)
distance5 = hamming_distance(img_hash1, img_hash5)
distance6 = hamming_distance(img_hash1, img_hash6)
distance7 = hamming_distance(img_hash1, img_hash7)
distance8 = hamming_distance(img_hash1, img_hash8)
distance9 = hamming_distance(img_hash1, img_hash9)
distance10 = hamming_distance(img_hash1, img_hash10)
time_end = time.time()
print(f"圖片名稱:{img_1},圖片HASH:{img_hash1},與圖片1的近似值(漢明距離):{distance1}")
print(f"圖片名稱:{img_2},圖片HASH:{img_hash2},與圖片1的近似值(漢明距離):{distance2}")
print(f"圖片名稱:{img_3},圖片HASH:{img_hash3},與圖片1的近似值(漢明距離):{distance3}")
print(f"圖片名稱:{img_4},圖片HASH:{img_hash4},與圖片1的近似值(漢明距離):{distance4}")
print(f"圖片名稱:{img_5},圖片HASH:{img_hash5},與圖片1的近似值(漢明距離):{distance5}")
print(f"圖片名稱:{img_6},圖片HASH:{img_hash6},與圖片1的近似值(漢明距離):{distance6}")
print(f"圖片名稱:{img_7},圖片HASH:{img_hash7},與圖片1的近似值(漢明距離):{distance7}")
print(f"圖片名稱:{img_8},圖片HASH:{img_hash8},與圖片1的近似值(漢明距離):{distance8}")
print(f"圖片名稱:{img_9},圖片HASH:{img_hash9},與圖片1的近似值(漢明距離):{distance9}")
print(f"圖片名稱:{img_10},圖片HASH:{img_hash10},與圖片1的近似值(漢明距離):{distance10}")
print(f"耗時(shí):{time_end - time_start}")
————————————————
版權(quán)聲明:本文為博主原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接和本聲明。
原文鏈接:https://blog.csdn.net/itanping/article/details/134022715
3、差值哈希算法
def get_dHash(img_path):
# 讀取圖像:通過OpenCV的imread加載RGB圖像
img_rgb = cv2.cvtColor(cv2.imread(img_path), cv2.COLOR_BGR2RGB)
# 縮小圖像:使用OpenCV的resize函數(shù)將圖像縮放為9x8像素,采用Cubic插值方法進(jìn)行圖像重采樣
img_resize = cv2.resize(img_rgb, (9, 8), cv2.INTER_CUBIC)
# 圖像灰度化:將彩色圖像轉(zhuǎn)換為灰度圖像
img_gray = cv2.cvtColor(img_resize, cv2.COLOR_BGR2GRAY)
# 計(jì)算差異值:獲得圖像二進(jìn)制字符串
img_hash_str = ''
# 遍歷圖像的像素,比較相鄰像素之間的灰度值,根據(jù)強(qiáng)弱增減差異情況生成一個(gè)二進(jìn)制哈希值
# 外層循環(huán),遍歷圖像的行(垂直方向),范圍是從0到7
for i in range(8):
# 內(nèi)層循環(huán),遍歷圖像的列(水平方向),范圍也是從0到7
for j in range(8):
# 比較當(dāng)前像素 img[i, j] 與下一個(gè)像素 img[i, j + 1] 的灰度值
if img_gray[i, j] > img_gray[i, j + 1]:
# 如果當(dāng)前像素的灰度值大于下一個(gè)像素的灰度值(灰度值增加),將1添加到名為 hash 的列表中
img_hash_str += '1'
else:
# 否則灰度值弱減,將0添加到名為 hash 的列表中
img_hash_str += '0'
# print(f"圖像的二進(jìn)制哈希值={img_hash_str}")
# 生成哈希值:生成圖像可識別哈希值
img_hash = ''.join(map(lambda x:'%x' % int(img_hash_str[x : x + 4], 2), range(0, 64, 4)))
return img_hash
4、單通道直方圖
def histogram(image1, image2):
# 灰度直方圖算法
# 計(jì)算單通道的直方圖的相似值
hist1 = cv2.calcHist([image1], [0], None, [256], [0.0, 255.0])
hist2 = cv2.calcHist([image2], [0], None, [256], [0.0, 255.0])
# 計(jì)算直方圖的重合度
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