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

实现效果:

相关推荐
十三画者1 天前
【文献分享】SpatialZ弥合从平面空间转录组学到三维细胞图谱之间的维度差距
人工智能·数据挖掘·数据分析·数据可视化
一条咸鱼_SaltyFish1 天前
[Day13] 微服务架构下的共享基础库设计:contract-common 模块实践
开发语言·人工智能·微服务·云原生·架构·ai编程
童欧巴1 天前
DeepSeek V4,定档春节
人工智能·aigc
爱学习的张大1 天前
深度学习中稀疏专家模型研究综述 A REVIEW OF SPARSE EXPERT MODELS IN DEEP LEARNING
人工智能·深度学习
爱打代码的小林1 天前
CNN 卷积神经网络 (MNIST 手写数字数据集的分类)
人工智能·分类·cnn
川西胖墩墩1 天前
游戏NPC的动态决策与情感模拟
人工智能
E_ICEBLUE1 天前
零成本实现文档智能:本地化 OCR 提取与 AI 处理全流程实战
人工智能·ocr
乾元1 天前
无线定位与链路质量预测——从“知道你在哪”,到“提前知道你会不会掉线”的网络服务化实践
运维·开发语言·人工智能·网络协议·重构·信息与通信
MistaCloud1 天前
Pytorch深入浅出(十五)之GPU加速与设备管理
人工智能·pytorch·python·深度学习
源于花海1 天前
迁移学习的第一类方法:数据分布自适应(3)——联合分布自适应
人工智能·机器学习·迁移学习·联合分布自适应