***** 找了两天问题,对于我这种小白来说,太难了
查找对于图片两种方法都找不到:最后附上两种查找方法,及自动化实例
失败原因:小图是在大图上面截图的,然而我大图没有显示原比例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) # 点击软件位置