搜索

cv2获取滑动缺口的距离


发布时间: 2022-11-24 19:23:00    浏览次数:80 次
import cv2
import numpy as np
from base64 import b64encode, b64decode


def get_distance(slider_image, bg_image):
    ''' 获取缺口位置
    '''
    distance = 0
    try:
        # 滑块处理
        b_image = np.frombuffer(b64decode(slider_image), dtype="uint8")
        # b_image = np.frombuffer(slider_image, dtype="uint8")
        target_rgb = cv2.imdecode(b_image, cv2.IMREAD_COLOR)
        target_gray = cv2.cvtColor(target_rgb, cv2.COLOR_BGR2GRAY)

        # 背景图片处理
        template = np.frombuffer(b64decode(bg_image), dtype="uint8")
        # template = np.frombuffer(bg_image, dtype="uint8")
        template_rgb = cv2.imdecode(template, cv2.IMREAD_COLOR)
        template_gray = cv2.cvtColor(template_rgb, cv2.COLOR_BGR2GRAY)

        # 距离计算
        res = cv2.matchTemplate(target_gray, template_gray, cv2.TM_CCOEFF_NORMED)
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
        if abs(1 - min_val) <= abs(1 - max_val):
            distance = min_loc[0]
        else:
            distance = max_loc[0]
    except Exception as e:
        print(e)

    return distance
免责声明 cv2获取滑动缺口的距离,资源类别:文本, 浏览次数:80 次, 文件大小:-- , 由本站蜘蛛搜索收录2022-11-24 07:23:00。此页面由程序自动采集,只作交流和学习使用,本站不储存任何资源文件,如有侵权内容请联系我们举报删除, 感谢您对本站的支持。 原文链接:https://www.cnblogs.com/wangshx666/p/16922949.html