python逆向计算滑块距离

import cv2


def show(name):
    '''展示圈出来的位置'''
    cv2.imshow('Show', name)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


def _tran_canny(image):
    """消除噪声"""
    image = cv2.GaussianBlur(image, (3, 3), 0)
    return cv2.Canny(image, 50, 150)


def detect_displacement(img_slider_path, image_background_path):
    """detect displacement"""
    # # 参数0是灰度模式
    image = cv2.imread(img_slider_path, 0)
    # 缩放0.616
    image = cv2.resize(image, None, fx=0.616, fy=0.616, interpolation=cv2.INTER_AREA)
    template = cv2.imread(image_background_path, 0)
    # 缩放0.616
    template = cv2.resize(template, None, fx=0.616,fy=0.616,interpolation=cv2.INTER_AREA)

    # 寻找最佳匹配
    res = cv2.matchTemplate(_tran_canny(image), _tran_canny(template), cv2.TM_CCOEFF_NORMED)
    # 最小值,最大值,并得到最小值, 最大值的索引
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

    top_left = max_loc[0]  # 横坐标
    # 展示圈出来的区域
    x, y = max_loc  # 获取x,y位置坐标

    w, h = image.shape[::-1]  # 宽高
    cv2.rectangle(template, (x, y), (x + w, y + h), (7, 249, 151), 2)
    show(template)
    return top_left


if __name__ == '__main__':
    top_left = detect_displacement("ddd.png", "ccc.png")
    print(top_left)

猜你喜欢

转载自blog.csdn.net/weixin_47723549/article/details/129621013