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
标签:COLOR,dtype,image,cv2,缺口,template,np,滑动
From: https://www.cnblogs.com/wangshx666/p/16922949.html