当前位置: 首页 > wzjs >正文

余姚做网站阿里免费域名申请

余姚做网站,阿里免费域名申请,oa办公系统管理软件,重庆企业网站建设推荐一、简介 💢 SIFT特征匹配效果较差,谨慎使用。 主要用于在目标图像中找到模板图像的位置,支持单尺度模板匹配(TemplateMatcher)、多尺度模板匹配(MultiScale)和SIFT特征匹配(SIFTFe…

一、简介

💢 SIFT特征匹配效果较差,谨慎使用。

  1. 主要用于在目标图像中找到模板图像的位置,支持单尺度模板匹配(TemplateMatcher)、多尺度模板匹配(MultiScale)和SIFT特征匹配(SIFTFeatureMatcher)三种方法。

  2. 正常使用时: 可以通过调用im_match函数选择最佳匹配结果;

  3. 调试代码: 确认图像识别准确率时,可以draw_rectangle函数绘制匹配区域和中心点以便查看识别效果

    • 加载图像: 需要使用OpenCV加载目标图像和模板图像,格式为numpy数组。

    • 匹配图像: 调用im_match函数,输入图像、模板和置信度阈值(默认0.6),返回匹配的坐标。

    • 绘制结果: 用draw_rectangle函数在图像上绘制矩形,输入图像路径和坐标,返回绘制后的图像和中心点。

二、类和函数详细说明

TemplateMatcher类

1. 功能:

执行标准模板匹配,使用OpenCVmatchTemplate函数,方法默认使用cv2.TM_CCOEFF_NORMED,适合模板和图像尺寸一致的场景。

2. 属性:

  • method:模板匹配方法(默认cv2.TM_CCOEFF_NORMED)。

  • threshold:最小置信度阈值(默认0.7)。

3. 方法:

  • __init__(self, method=cv2.TM_CCOEFF_NORMED, threshold=0.7):初始化匹配器,设置方法和阈值。

  • match(self, image, template):执行模板匹配。

    • 参数:

      • image (numpy.ndarray):目标图像。

      • template (numpy.ndarray):模板图像。

    • 返回:

      • 元组(top_left, bottom_right, confidence),其中:

        • top_left:匹配的左上角坐标(x, y)。

        • bottom_right:匹配的右下角坐标(x, y)。

        • confidence:置信度值(0到1之间)。

      • 若未找到匹配(置信度低于阈值),返回((0, 0), (0, 0), 0)。

相关代码片段:

class TemplateMatcher:def __init__(self, method=cv2.TM_CCOEFF_NORMED, threshold=0.7):self.method = methodself.threshold = max(0.7, threshold)def match(self, image, template):h, w = template.shape[:2]res = cv2.matchTemplate(image, template, self.method)min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)if max_val >= self.threshold:top_left = max_locbottom_right = (top_left[0] + w, top_left[1] + h)return top_left, bottom_right, round(max_val, 3)else:return (0, 0), (0, 0), 0

MultiScale类

1. 功能:

执行多尺度模板匹配,通过缩放模板(默认比例1.0到0.5,步长0.1)进行多次匹配,适合处理不同尺寸的模板。

2. 属性:

  • scales:缩放比例列表(默认[1.0, 0.9, 0.8, 0.7, 0.6, 0.5])。

  • threshold:最小置信度阈值(默认0.7)。

3. 方法:

  • __init__(self, scales=None, threshold=0.7):初始化多尺度匹配器,设置缩放比例和阈值。

  • match(self, image, template, method=cv2.TM_CCOEFF_NORMED):执行多尺度模板匹配。

    • 参数:

      • image (numpy.ndarray):目标图像。

      • template (numpy.ndarray):模板图像。

      • method (int):模板匹配方法(默认cv2.TM_CCOEFF_NORMED)。

    • 返回:

      • 元组(top_left, bottom_right, confidence),其中:

        • top_left:匹配的左上角坐标(x, y)。

        • bottom_right:匹配的右下角坐标(x, y)。

        • confidence:置信度值(0到1之间)。

      • 若未找到匹配(置信度低于阈值),返回((0, 0), (0, 0), 0)。

相关代码片段:

class MultiScale:def __init__(self, scales=None, threshold=0.7):if scales is None:self.scales = [1.0, 0.9, 0.8, 0.7, 0.6, 0.5]else:self.scales = scalesself.threshold = max(0.7, threshold)def match(self, image, template, method=cv2.TM_CCOEFF_NORMED):template_h, template_w = template.shape[:2]best_loc = Nonebest_scale = 1best_value = -1for scale in self.scales:resized = cv2.resize(template, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)res = cv2.matchTemplate(image, resized, method)min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)if max_val > best_value and max_val > self.threshold:best_value = max_valbest_loc = max_locbest_scale = scaleif best_loc is not None:best_h, best_w = int(template_h * best_scale), int(template_w * best_scale)top_left = best_locbottom_right = (top_left[0] + best_w, top_left[1] + best_h)return top_left, bottom_right, round(best_value, 3)else:return (0, 0), (0, 0), 0

SIFTFeatureMatcher类

1. 功能:

使用SIFT算法进行特征匹配,适合处理旋转和缩放的场景,通过检测关键点和描述符进行匹配。

2. 属性:

  • sift (cv2.SIFT):SIFT特征检测器和描述符。

3. 方法:

  • __init__(self):初始化SIFT检测器。

  • match(self, image, template):执行特征匹配。

    • 参数:

      • image (numpy.ndarray):目标图像。

      • template (numpy.ndarray):模板图像。

    • 返回:

      • 元组(start_point, end_point),其中:

        • start_point:匹配的左上角坐标(x1, y1)。

        • end_point:匹配的右下角坐标(x2, y2)。

      • 若未找到匹配,返回((0, 0), (0, 0))。

相关代码片段:

class SIFTFeatureMatcher:def __init__(self):self.sift = cv2.SIFT_create()def match(self, image, template):img1_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)img2_gray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)kp1, des1 = self.sift.detectAndCompute(img1_gray, None)kp2, des2 = self.sift.detectAndCompute(img2_gray, None)matcher = cv2.BFMatcher()matches = matcher.knnMatch(des1, des2, k=2)good = []for m, n in matches:if m.distance < 0.75 * n.distance:good.append(m)if len(good) > 10:src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)M, mask = cv2.findHomography(dst_pts, src_pts, cv2.RANSAC, 5.0)h, w = template.shape[:2]pts = np.float32([[0, 0], [0, h-1], [w-1, h-1], [w-1, 0]]).reshape(-1, 1, 2)dst = cv2.perspectiveTransform(pts, M)points = list(np.int32(dst).flatten())y1, x1 = points[0:2]y2, x2 = points[4:6]return (x1, y1), (x2, y2)else:return (0, 0), (0, 0)

im_match函数

1. 功能:

结合TemplateMatcher和MultiScale的结果,找到最佳匹配。

2. 参数:

  • image (numpy.ndarray):目标图像。

  • template (numpy.ndarray):模板图像。

  • threshold (float):最小置信度阈值(默认0.6)。

返回:

  • 元组(start_point, end_point),其中:

    • start_point:匹配的左上角坐标。

    • end_point:匹配的右下角坐标。

  • 若未找到匹配,返回((0, 0), (0, 0))。

相关代码片段:

def im_match(image, template, threshold=0.6):template_matcher = TemplateMatcher().match(image, template)multi_scale = MultiScale().match(image, template)print(f"match results >>> template_matcher: {template_matcher}; multi_scale: {multi_scale}")if template_matcher[2] >= threshold or multi_scale[2] >= threshold:if template_matcher[2] >= multi_scale[2]:points = template_matcher[:2]else:points = multi_scale[:2]return pointselse:return (0, 0), (0, 0)

draw_rectangle函数

1. 功能:

在图像上绘制矩形,标记匹配区域,并计算中心点。

2. 参数:

  • image_path (str):图像文件路径。

  • start_point (tuple):矩形的左上角坐标(x1, y1)。

  • end_point (tuple):矩形的右下角坐标(x2, y2)。

  • color (tuple):矩形颜色(BGR格式,默认绿色(0, 255, 0))。

  • thickness (int):矩形边框粗细(默认2)。

返回:

  • 元组(image, center_point),其中:

    • image:绘制矩形后的图像。

    • center_point:矩形中心点坐标(center_x, center_y)

相关代码片段:

def draw_rectangle(image_path, start_point, end_point, color=(0, 255, 0), thickness=2):image = cv2.imread(image_path)if image is None:raise ValueError(f"Unable to load image: {image_path}")cv2.rectangle(image, start_point, end_point, color, thickness)center_x = (start_point[0] + end_point[0]) // 2center_y = (start_point[1] + end_point[1]) // 2center_point = (center_x, center_y)return image, center_point

三、思路总结

提示: SIFTFeatureMatcher虽然定义了,但未集成到im_match函数中,SIFT特征匹配方法当前实现尚不成熟,需进一步优化。

通过TemplateMatcher、MultiScale和SIFTFeatureMatcher覆盖不同场景需求。im_match函数结合单尺度和多尺度匹配,确保鲁棒性,draw_rectangle提供可视化反馈。



文章转载自:

http://WfomQBwA.dnycx.cn
http://ADFl92jq.dnycx.cn
http://bJBzctlE.dnycx.cn
http://XY1VNVwe.dnycx.cn
http://22oYcAgQ.dnycx.cn
http://LTLT6HuG.dnycx.cn
http://k1fym1IX.dnycx.cn
http://8koxC9Tu.dnycx.cn
http://CYf3pKQU.dnycx.cn
http://xIgpHKcl.dnycx.cn
http://jNKEK0Fx.dnycx.cn
http://4VH8fVh2.dnycx.cn
http://ikwAWif7.dnycx.cn
http://OGJdFRVO.dnycx.cn
http://zoq7KfNV.dnycx.cn
http://0tpWDB2Q.dnycx.cn
http://YN83hFbt.dnycx.cn
http://8aZwf7Ey.dnycx.cn
http://tCXxvaKu.dnycx.cn
http://UXF9yoWb.dnycx.cn
http://2CMULaLi.dnycx.cn
http://gESxC5wE.dnycx.cn
http://YrXVtid4.dnycx.cn
http://uRkSgts6.dnycx.cn
http://QBKxbFG8.dnycx.cn
http://9U0AcG6o.dnycx.cn
http://Rh3ImJ2o.dnycx.cn
http://MwPauPtK.dnycx.cn
http://OANoswP0.dnycx.cn
http://qbZG7s0B.dnycx.cn
http://www.dtcms.com/wzjs/698810.html

相关文章:

  • 网站建设策划怎么沟通支付宝小程序api
  • 网站页面设计培训班设计师素材库
  • 简述网站一般建设的流程图fizz wordpress
  • wordpress 二次元模板昆明seo外包
  • 怎样写网站描述简约大方网站
  • 禁止指定ip访问网站那家网站做的效果好
  • 佛山市外贸网站建设搭建网站需要学什么软件
  • 做网站必须要买空间上海 装修公司推荐
  • 网站建设办公怎么注册域名免费
  • 什么网站可以做报名系统wordpress速度慢解决方法
  • 怎么做网站服务器专业建站方案
  • 什么是网站二级目录企业网站建设计划
  • 小型求职招聘网站源码 phpwordpress小工具开发教程
  • 外贸商城网站系统临汾做网站的公司
  • 网站设计建设公司教程软件开发接单网站
  • 这么用自己的电脑做网站服务器网站建设合同建设方注意事项
  • 保定模板建站平台什邡门户网站
  • 020网站开发兰州网站设计有限公司
  • 摄影网站建立购买空间后怎么上传网站
  • 网站设计者哈尔滨seo优化
  • 小榄网站建设公司html5手机网站开发教程
  • 腾冲网站建设建设公司网站都需要什么科目
  • 地方门户网站如何宣传电商网站建设流程图
  • 网站备案 影响wordpress 模版 推荐
  • 企业建站 炫酷模板一人有限公司怎么注册
  • 郴州网站建设公司官网网站如何防止重登录
  • 企业网站建设的困难和问题甘肃第九建设集团公司网站
  • 网站主机的类型网站程序更换
  • 天津的公司能在北京做网站备案吗高大上网站欣赏
  • 做宣传网站大概多少钱800多块做网站