ac.find_template 踩坑,寻找相似图片,安卓自动化代码

***** 找了两天问题,对于我这种小白来说,太难了
查找对于图片两种方法都找不到:最后附上两种查找方法,及自动化实例

失败原因:小图是在大图上面截图的,然而我大图没有显示原比例100%
一定要放大到100%再截图
在这里插入图片描述
用代码查看图片也可以,默认就是100%展示

    img_ribs = cv2.imread(big_image)       # 打开大图片,img数据类型是np.array()类型
    # 方法1
    aircv.show(img_ribs)                   # 1.查看图片,再截图
	# 方法2
    cv2.imshow('image title', img_ribs)    # 2.查看图片,再截图
    cv2.waitKey(0)                         # 2.查看图片,再截图

自动化案例
1、两种匹配规则

def image_position(small_image, big_image):
    img_ribs = cv2.imread(big_image)       # 打开大图片,img数据类型是np.array()类型
    # aircv.show(img_ribs)                   # 1.查看图片,再截图
    img_gray = cv2.cvtColor(img_ribs, cv2.COLOR_BGR2GRAY)
    template = cv2.imread(small_image, 0)  # 打开小图片,img数据类型是np.array()类型
    height, width = template.shape[::]     # 小图片大小,高*宽
    res = cv2.matchTemplate(img_gray, template, cv2.TM_SQDIFF)  # 大图,小图,规则
    min_val, max_val, top_left, max_indx = cv2.minMaxLoc(res)   # _, _, top_left, _ = cv2.minMaxLoc(res)
    # min_val 和 max_val ,表示最差匹配 和 最优匹配的匹配值。
    # min_loc 和 max_loc ,表示最差匹配 和 最优匹配的感兴趣区域在图中的坐标。
    print(min_val, max_val, top_left, max_indx)
    bottom_righ = (top_left[0] + width/2, top_left[1] + height/2)   # 如果小数不能点击,则x//2,整除
    return bottom_righ[0], bottom_righ[1]

def matchImg(imgsrc, imgobj, confidence=0.8):   # imgsrc=原始图像,imgobj=待查找的图片
    imsrc = ac.imread(imgsrc)
    imobj = ac.imread(imgobj)
    s = ac.find_template(imsrc, imobj, confidence)   # find_all_template 全部和第一个 find_template
    return s['result'][0], s['result'][1]

2、python操作安卓手机

def adb(command):
    proc = subprocess.Popen(command.split(' '), stdout=subprocess.PIPE, shell=True)
    (out, _) = proc.communicate()
    return out.decode('utf-8')

# 点击坐标  tap(448, 840)
def tap(tap_x, tap_y):
    adb("adb shell input tap {} {}".format(tap_x, tap_y))

imgsrc = os.getcwd() + r"\image\deb.png"
imgobj = os.getcwd() + r"\click_image\xianyu.png"
x, y = image_position(imgobj, imgsrc)
print(x, y)
x1, y1 = matchImg(imgsrc, imgobj)
print(x1, y1)
# tap(x, y)            # 点击软件位置
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值