需求:当一个页面存在多个相同的按键,通过图像识别的方式点击指定的按键
思路:
1.每次匹配到按键的区域后将区域从源图像中去掉
2.根据按键与左上角 (0,0) 的长度对按键进行排序
(忽略长度相同的情况,可换其他角的坐标计算解决)
source.jpg
match.jpg
from PIL import Image import numpy from airtest.core.cv import Template from airtest import aircv from airtest.core.helper import G from airtest.utils.transform import TargetPos import time from math import sqrt class Template2(Template): def __int__(self,*args, **kwargs): super.__init__(*args, **kwargs) #重写match_in,return加上识别到的区域(match_result) def match_in(self, screen): match_result = self._cv_match(screen) G.LOGGING.debug("match result: %s", match_result) if not match_result: return None focus_pos = TargetPos().getXY(match_result, self.target_pos) return focus_pos,match_result def p(search,source,total,index): """ :param search: 匹配图像 :param source: 源图像 :param total: 相同图像总数 :param index: 匹配索引 :return: 匹配坐标 """ result = [] source_array=numpy.array(Image.open(source)) t = time.time() for i in range(total): im_search = Template2(search) im_source = aircv.imread(source) pos,match = im_search.match_in(im_source) #匹配坐标和匹配区域 array = numpy.array(Image.open(source))#创建矩阵 #将区域填充为白色 for col in range(match['rectangle'][0][0],match['rectangle'][-1][0]): for row in range(match['rectangle'][0][1],match['rectangle'][1][1]): array[row,col] = [255,255,255] Image.fromarray(array).save(source)#更新源图像 result.append(pos) result.sort(key= lambda x: sqrt(x[0]*x[0]+x[1]*x[1]))#根据距离(0,0)排序 print(time.time() - t) print(result) Image.fromarray(source_array).save(source) return result[index] print(p('./search.jpg','./source.jpg',10,0))
#输出 0.0870201587677002 [(112, 63), (112, 119), (160, 63), (160, 119), (208, 63), (208, 119), (256, 63), (256, 119), (304, 63), (304, 119)] (112, 63)