详解如何使用opencv实现图片相似度检测
import cv2
import numpy as np
def calculate_complexity_similarity(img1_path, img2_path):
# 加载两张图片
img1 = cv2.imread(img1_path)
img2 = cv2.imread(img2_path)
# 将图片转换为灰度图像
gray_img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray_img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
# 创建ORB特征检测器
orb = cv2.ORB_create()
# 检测特征点和描述符
kp1, des1 = orb.detectAndCompute(gray_img1, None)
kp2, des2 = orb.detectAndCompute(gray_img2, None)
# 将描述符类型转换为CV_8U
des1 = np.uint8(des1)
des2 = np.uint8(des2)
# 创建暴力匹配器
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
# 进行特征匹配
matches = bf.match(des1, des2)
similarity = 0.0
# 根据特征点匹配结果计算相似度
if len(matches) > 0:
similarity = sum([match.distance for match in matches]) / len(matches)
print('图片相似度为:', similarity)
else:
print('未找到匹配的特征点')
return similarity
def calculate_histogram_similarity(img1_path, img2_path):
# 读取两张图片
img1 = cv2.imread(img1_path, cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread(img2_path, cv2.IMREAD_GRAYSCALE)
# 计算直方图
hist1 = cv2.calcHist([img1], [0], None, [256], [0, 256])
hist2 = cv2.calcHist([img2], [0], None, [256], [0, 256])
# 归一化直方图
cv2.normalize(hist1, hist1, 0, 1, cv2.NORM_MINMAX)
cv2.normalize(hist2, hist2, 0, 1, cv2.NORM_MINMAX)
# 比较直方图
similarity = cv2.compareHist(hist1, hist2, cv2.HISTCMP_CORREL)
if similarity < 0.2:
return similarity
# 将第二个图片切割为4个区域并分别计算相似度
h, w = img2.shape[:2]
img2_1 = img2[:h // 2, :w // 2]
img2_2 = img2[:h // 2, w // 2:]
img2_3 = img2[h // 2:, :w // 2]
img2_4 = img2[h // 2:, w // 2:]
sim_list = []
# 创建ORB特征检测器
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(img1, None)
# 计算四个区域的相似度
for img in [img2_1, img2_2, img2_3, img2_4]:
kp2, des2 = orb.detectAndCompute(img, None)
if des1 is None or des2 is None:
print('未找到足够的特征点')
sim_list.append(0.1)
else:
des1 = np.uint8(des1)
des2 = np.uint8(des2)
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1, des2)
orb_similarity = 0.0
if len(matches) > 0:
orb_similarity = sum([match.distance for match in matches]) / len(matches)
sim_list.append(orb_similarity)
max_sim = max(sim_list)
print('图片相似度为:', max_sim)
return max_sim
if __name__ == '__main__':
img1_path = 'pic/straw1.png'
img2_path = 'pic/straw2.png'
# 加载两张图片
img1 = cv2.imread(img1_path)
img2 = cv2.imread(img2_path)
# 计算ORB特征相似度
orb_similarity = calculate_complexity_similarity(img1_path, img2_path)
# 计算直方图相似度
hist_similarity = calculate_histogram_similarity(img1_path, img2_path)
# 将第二张图片切割为4个区域
h, w = img2.shape[:2]
img2_1 = img2[:h // 2, :w // 2]
img2_2 = img2[:h // 2, w // 2:]
img2_3 = img2[h // 2:, :w // 2]
img2_4 = img2[h // 2:, w // 2:]
# 在图片上绘制绿色框和相似度
cv2.rectangle(img2, (0, 0), (w // 2, h // 2), (0, 255, 0), 2)
cv2.putText(img2, f" {orb_similarity:.2f}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.rectangle(img2, (w // 2, 0), (w, h // 2), (0, 255, 0), 2)
cv2.putText(img2, f" {hist_similarity:.2f}", (w // 2 + 10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.rectangle(img2, (0, h // 2), (w // 2, h), (0, 255, 0), 2)
cv2.putText(img2, f" {orb_similarity:.2f}", (10, h // 2 + 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.rectangle(img2, (w // 2, h // 2), (w, h), (0, 255, 0), 2)
cv2.putText(img2, f" {hist_similarity:.2f}", (w // 2 + 10, h // 2 + 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
# 显示结果图片
cv2.namedWindow('Image', cv2.WINDOW_NORMAL)
cv2.resizeWindow('Image', 800, 600)
cv2.imshow('Image', cv2.resize(img2, (0, 0), fx=0.5, fy=0.5))
cv2.waitKey(0)
cv2.destroyAllWindows()