使用OpenCV计算滑块缺口(2)

上一篇 openCV 计算滑块缺口,执行可能出现问题,这一篇文章,是上一版本的补充(https://blog.csdn.net/weixin_42883164/article/details/137604965)

实现计算滑块缺口的步骤:

接口部分参照上述文章,重写detect_displacement 方法:

bash 复制代码
def detect_displacement(img_slider_path, image_background_path):
    """detect displacement"""
    # # 参数0是灰度模式
    image = cv2.imread(img_slider_path, 0)
    # print("灰度模式")
    # show(image)

    image_cv2 = cv2.imread(img_slider_path)

    gray = cv2.cvtColor(image_cv2, cv2.COLOR_BGR2GRAY)
    # print("BGR模式")
    # show(gray)

    # 3. 二值化处理
    _, binary_image = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
    # print("二值化")
    # show(binary_image)

    # 4. 查找轮廓
    contours, hierarchy = cv2.findContours(binary_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)


    # 5. 在原图上绘制轮廓
    image_with_contours = cv2.drawContours(image, contours, -1, (0, 255, 0), 1)  # 最后一个参数是轮廓线条的厚度

    # print("绘制轮廓")
    # show(image_with_contours)

    template = cv2.imread(image_background_path, 0)
    # show(template)

    #使用 matchTemplate 函数进行模板匹配
    res = cv2.matchTemplate(_tran_canny(image), _tran_canny(template), cv2.TM_CCOEFF_NORMED)
    # 最小值,最大值,并得到最小值, 最大值的索引
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

    print(max_val)
    print(max_loc)

    # top_left = min_loc[0]  # 横坐标
    # 展示圈出来的区域
    x, y = max_loc  # 获取x,y位置坐标
    w, h = image.shape[::-1]  # 宽高
    cv2.rectangle(template, (x, y), (x+w, y+h), (0, 0, 255), 2) #左上 右下 边框颜色,线条厚度
    show(template)
    top_left = x+w
    return top_left

实现效果:

相关推荐
Ztopcloud极拓云视角1 小时前
ChatGPT超级应用改版技术解析:Codex集成架构与多模型路由实战
人工智能·chatgpt·架构
秋98 小时前
从 Python 后端工程师转型 AI Engineer(AI 工程化)的完整补课清单(2026实战版)
开发语言·人工智能·python
啦啦啦_99998 小时前
5. 迁移学习
人工智能·机器学习·迁移学习
A.说学逗唱的Coke8 小时前
【AI·Coding】TDD × SDD × AI Coding:从“测试驱动“到“规范驱动“的智能协作实践
人工智能·驱动开发·tdd
云烟成雨TD8 小时前
Spring AI Alibaba 1.x 系列【78】沙箱(Sandbox)
java·人工智能·spring
tq10868 小时前
基于SLIP的防幻觉的指南
人工智能
甲维斯9 小时前
Kimi版超级玛丽效果“惊人”,配额不足5厘米!
前端·人工智能
console.log('npc')9 小时前
AI前端工程与生成式UI学习路线
前端·人工智能·ui
秋910 小时前
3年经验Python后端转AI Engineer:3个月实战转型计划(2026版)
开发语言·人工智能·python
圣殿骑士-Khtangc10 小时前
GPT-5.5 技术深度解析与企业级生产落地实战:从幻觉率下降到百万Token工程化
人工智能·gpt