1.概述
一般游戲是使用SurfaceView,所有的東西都是自己繪制,所以很難定位控件打月。常規(guī)的游戲測(cè)試方案,是用一個(gè)通用的測(cè)試框架配合計(jì)算機(jī)視覺(jué)蚕捉。而平時(shí)在對(duì)游戲做自動(dòng)化測(cè)試時(shí)奏篙,最常使用的就是利用opencv進(jìn)行圖像匹配,獲取匹配到的圖像中心點(diǎn)坐標(biāo)迫淹,然后通過(guò)adb命令去點(diǎn)擊該坐標(biāo)秘通。
2.Demo
先對(duì)手機(jī)截屏,然后進(jìn)行圖像匹配敛熬,計(jì)算匹配到的圖像中心點(diǎn)坐標(biāo)肺稀。
# 載入圖像
target_img = cv2.imread("screencap.png")
find_img = cv2.imread("images/btn_close_full.png")
find_height, find_width, find_channel = find_img.shape[::]
# 模板匹配
result = cv2.matchTemplate(target_img, find_img, cv2.TM_CCOEFF_NORMED)
min_val,max_val,min_loc,max_loc = cv2.minMaxLoc(result)
# 計(jì)算位置
pointUpLeft = max_loc
pointLowRight = (max_loc[0]+find_width, max_loc[1]+find_height)
pointCentre = (max_loc[0]+(find_width/2), max_loc[1]+(find_height/2))
3.matchTemplate
關(guān)于參數(shù) method:
CV_TM_SQDIFF 平方差匹配法:該方法采用平方差來(lái)進(jìn)行匹配;最好的匹配值為0应民;匹配越差话原,匹配值越大。
CV_TM_CCORR 相關(guān)匹配法:該方法采用乘法操作诲锹;數(shù)值越大表明匹配程度越好繁仁。
CV_TM_CCOEFF 相關(guān)系數(shù)匹配法:1表示完美的匹配;-1表示最差的匹配归园。
CV_TM_SQDIFF_NORMED 歸一化平方差匹配法
CV_TM_CCORR_NORMED 歸一化相關(guān)匹配法
CV_TM_CCOEFF_NORMED 歸一化相關(guān)系數(shù)匹配法
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread("orginal.jpg",0)#以灰度模式讀入圖像
img2 = img.copy()
template = cv2.imread("part.PNG",0)#以灰度模式讀入圖像
w, h = template.shape[::-1]
# 6 中匹配效果對(duì)比算法
methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR',
'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']
for meth in methods:
img = img2.copy()
method = eval(meth)
res = cv2.matchTemplate(img, template, method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
top_left = min_loc
else:
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
#cv2.rectangle(img, top_left, bottom_right, (255,255,255), 2)
print meth
cv2.rectangle(img, top_left, bottom_right, (0, 255, 0), 2)
# cv2.imwrite('res.png',img_rgb)
cv2.imshow('res', img)
cv2.waitKey(0)
cv2.destroyAllWindows()