提交 f6f69c61 authored 作者: 陈泽健's avatar 陈泽健

处理优化。

上级 260a7950
......@@ -112,85 +112,67 @@ def compare_brightness(light_down_path, light_on_path, threshold=1):
import cv2
import numpy as np
import logging
from PIL import Image
import numpy as np
# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def compare_images_feature_matching(image1_path, image2_path):
"""
使用特征匹配对比两张图片的相似度。
"""
try:
image1 = cv2.imread(image1_path, cv2.IMREAD_GRAYSCALE)
image2 = cv2.imread(image2_path, cv2.IMREAD_GRAYSCALE)
# 打开两张图片
img1 = Image.open(image1_path)
img2 = Image.open(image2_path)
if image1 is None or image2 is None:
logging.error(f"无法读取图像: {image1_path} 或 {image2_path}")
return False
# 将图片转换为相同的模式和尺寸
img1 = img1.convert("RGB")
img2 = img2.convert("RGB")
# 初始化ORB检测器
orb = cv2.ORB_create()
# 如果尺寸不同,先调整大小
if img1.size != img2.size:
logging.info("图片尺寸不同,调整为相同尺寸进行比较")
img2 = img2.resize(img1.size, Image.ANTIALIAS) # 保持纵横比
# 查找关键点和描述符
kp1, des1 = orb.detectAndCompute(image1, None)
kp2, des2 = orb.detectAndCompute(image2, None)
# 转换为 numpy 数组进行比较
img1_array = np.array(img1)
img2_array = np.array(img2)
logging.info(f"图像1的关键点数量: {len(kp1)}")
logging.info(f"图像2的关键点数量: {len(kp2)}")
# 比较两个数组是否相同
return np.array_equal(img1_array, img2_array)
if des1 is None or des2 is None:
logging.error("无法检测到描述符")
except FileNotFoundError as e:
logging.error(f"文件未找到: {e}")
return False
# FLANN参数
FLANN_INDEX_LSH = 6
index_params = dict(algorithm=FLANN_INDEX_LSH, table_number=6, key_size=12, multi_probe_level=1)
search_params = dict(checks=50)
# FLANN匹配器
flann = cv2.FlannBasedMatcher(index_params, search_params)
# 匹配描述符
matches = flann.knnMatch(des1, des2, k=2)
# 使用比率测试过滤匹配
good_matches = []
for m, n in matches:
if m.distance < 0.7 * n.distance:
good_matches.append(m)
# 计算匹配点的数量
num_matches = len(good_matches)
logging.info(f"匹配点数量: {num_matches}")
INFO(f"匹配点数量:{num_matches}")
if num_matches > 10:
INFO("图片相同")
return True
else:
INFO("图片不同")
except OSError as e:
logging.error(f"图片处理错误: {e}")
return False
except Exception as e:
logging.error(f"对比图片时发生错误: {e}", exc_info=True)
except MemoryError as e:
logging.error(f"内存不足: {e}")
return False
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logging.info("开始对比图片")
image1_path = r'D:\GithubData\自动化\ubains-module-test\预定系统\reports\imgs\Exhibit_Inspect\No_PaperLess\同屏后-无纸化设备A界面截屏.png'
image2_path = r'D:\GithubData\自动化\ubains-module-test\预定系统\reports\imgs\Exhibit_Inspect\No_PaperLess\同屏后-无纸化设备B界面截屏.png'
if not os.path.exists(image1_path):
logging.error(f"图片 {image1_path} 不存在")
exit(1)
if not os.path.exists(image2_path):
logging.error(f"图片 {image2_path} 不存在")
exit(1)
# if __name__ == '__main__':
# logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# logging.info("开始对比图片")
#
# image1_path = r'D:\GithubData\自动化\ubains-module-test\预定系统\reports\imgs\Exhibit_Inspect\No_PaperLess\DeviceA-ShareScreen.png'
# image2_path = r'D:\GithubData\自动化\ubains-module-test\预定系统\reports\imgs\Exhibit_Inspect\No_PaperLess\DeviceB-ShareScreen.png'
#
# if not os.path.exists(image1_path):
# logging.error(f"图片 {image1_path} 不存在")
# exit(1)
# if not os.path.exists(image2_path):
# logging.error(f"图片 {image2_path} 不存在")
# exit(1)
#
# # 对比两张截图的相似度
# if compare_images_feature_matching(image1_path, image2_path):
# logging.info("图片相同")
# else:
# logging.error("图片不同")
# 对比两张截图的相似度
if compare_images_feature_matching(image1_path, image2_path):
logging.info("图片相同")
else:
logging.error("图片不同")
import cv2
import logging
......
......@@ -211,4 +211,4 @@
- 增加get_test_result函数通过正则匹配获取通过率、失败率以及异常率,再调用钉钉消息通知输出通过率等值。优化处理。
54. 2025-02-14
- 补充讯飞语音转录功能流程,调试通过。
- 补充实现无纸化同屏巡检流程,但是还存在图片匹配失败问题。
\ No newline at end of file
- 补充实现无纸化同屏巡检流程,但是还存在图片匹配失败问题。处理优化。
\ No newline at end of file
......@@ -84,8 +84,8 @@ class same_screen_share_001:
# 调用图片对比函数判断相似度
match_text = compare_images_feature_matching(
r"D:\GithubData\自动化\ubains-module-test\预定系统\reports\imgs\Exhibit_Inspect\No_PaperLess\同屏后-无纸化设备A界面截屏.png",
r"D:\GithubData\自动化\ubains-module-test\预定系统\reports\imgs\Exhibit_Inspect\No_PaperLess\同屏后-无纸化设备B界面截屏.png")
r"/预定系统/reports/imgs/Exhibit_Inspect/No_PaperLess/DeviceA-ShareScreen.png",
r"/预定系统/reports/imgs/Exhibit_Inspect/No_PaperLess/DeviceB-ShareScreen.png")
INFO(f"图片相似度:{match_text}")
CHECK_POINT("校验同屏功能是否成功", match_text == True)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论