使用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

实现效果:

相关推荐
志栋智能35 分钟前
运维超自动化:构建弹性IT架构的关键支撑
运维·服务器·网络·人工智能·架构·自动化
薛定猫AI41 分钟前
【深度解析】Open Design:用本地优先架构重塑 AI UI 生成工作流
人工智能·ui·架构
嵌入式小企鹅2 小时前
CPU供需趋紧、DeepSeek V4全链适配、小米开源万亿模型
人工智能·学习·开源·嵌入式·小米·算力·昇腾
草莓熊Lotso2 小时前
Vibe Coding 时代:LangChain 与 LangGraph 全链路解析
linux·运维·服务器·数据库·人工智能·mysql·langchain
快乐非自愿3 小时前
RAG夺命10连问,你能抗住第几问?
人工智能·面试·程序员
千匠网络5 小时前
破局出海壁垒,千匠网络新能源汽车跨境出海解决方案
人工智能
马丁聊GEO7 小时前
解码AI用户心智,筑牢可信GEO根基——悠易科技深度参与《中国AI用户态度与行为研究报告(2026)》发布会
人工智能·科技
nap-joker7 小时前
Fusion - Mamba用于跨模态目标检测
人工智能·目标检测·计算机视觉·fusion-mamba·可见光-红外成像融合·远距离/伪目标问题
一只幸运猫.7 小时前
2026Java 后端面试完整版|八股简答 + AI 大模型集成技术(最新趋势)
人工智能·面试·职场和发展
Promise微笑7 小时前
2026年国产替代油介损测试仪:油介损全场景解决方案与技术演进
大数据·网络·人工智能