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
46
47
import cv2
import os
def image_matching(floder_name,picture_name):
# 使用绝对路径
path1 = os.path.abspath(r"C:\information_meeting_on.png")
path2 = os.path.abspath(r"C:\information_brithday_on.png")
if not os.path.exists(path1):
print(f"文件不存在: {path1}")
if not os.path.exists(path2):
print(f"文件不存在: {path2}")
# 加载图片
image1 = cv2.imread(path1, cv2.IMREAD_GRAYSCALE)
image2 = cv2.imread(path2, cv2.IMREAD_GRAYSCALE)
# 检查图片是否加载成功
if image1 is None:
print("图片1加载失败,请检查路径或文件是否存在")
if image2 is None:
print("图片2加载失败,请检查路径或文件是否存在")
# 如果图片加载成功,继续执行特征匹配
if image1 is not None and image2 is not None:
# 初始化ORB检测器
orb = cv2.ORB_create()
# 检测关键点和描述符
keypoints1, descriptors1 = orb.detectAndCompute(image1, None)
keypoints2, descriptors2 = orb.detectAndCompute(image2, None)
# 使用BFMatcher进行匹配
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(descriptors1, descriptors2)
# 根据匹配点数量判断相似性
matches_count = len(matches)
print(f"匹配点数量: {matches_count}")
if matches_count > 400: # 阈值可以根据需求调整
print("图片相似")
else:
print("图片不相似")
if __name__ == "__main__":
panduan()