Python第三方包aircv实现图片定位

aircv是网易维护的一个小项目,里面有几个图像处理工具函数,它的安装很简单:

pip install aircv
其中被使用最多的,就是这个find_template方法,基本用法如下:

import aircv as ac

match_result = ac.find_template(image_origin, image_template, threshold, bgremove)
几个参数分别表示

image_origin:被查找的源图(上面实例中的钉钉主页面截图),要注意源图的宽和高,都必须大于或等于模板图
image_template:要查找的模板小图(上面实例中的logo)
threshold:最小置信度,在0到1之间。因为图像匹配并不需要每个像素精确一致,可以模糊匹配,所以这个值设定得越高,找到的区域就越接近模板图,但设得太高就有可能找不到。
bgremove:是否去除背景。如果这个值设置为True,那么函数内部会用Canny算子先提取图像轮廓,再做查找

返回值match_result是个dict结构:

match_result:{
    'result': (x,y),        #tuple,表示识别结果的中心点
    'rectangle':[            #二位数组,表示识别结果的矩形四个角
        [left, top],
        [left, bottom],
        [right, top],
        [right, bottom]
    ],
    'confidence': percentage   #识别结果的置信度,在0-1之间,越大越精准
}
这样我们就可以用返回值在图片上标出矩形框的位置:

import cv2
rect = match_result['rectangle']
cv2.rectangle(img_result, (rect[0][0], rect[0][1]), (rect[3][0], rect[3][1]), (0, 0, 220), 2)
dingding_found

这个find_template方法只返回一个最可信的结果,如果源图中有多个模板图,都想找回来
那么需要find_all_template函数,参数完全一样,只是返回值是个match_result数组。

但是这个库很久没有维护了,我给它提交了一个PR,也没人理,还是有一些坑的,以后再慢慢聊。

1.首先安装aircv

pip install aircv


2.代码实现

import aircv as ac

imgsrc = ac.imread('D:\\Setting.png')  # 打开查找页,大图
imgobj = ac.imread('D:\\Connect1.png')  # 打开待识别的图片,小图
match_result = ac.find_all_template(imgsrc, imgobj, 0.9)  # 0.9是识别达标率,平匹配度大于0.9才会返回
print('match_result:%s' % match_result)  #识别到的图片是一个数组
# match_result:[{'result': (115.5, 914.0),
#  'rectangle': ((42, 843), (42, 985), (189, 843), (189, 985)),  #匹配图片的四个角的坐标
# 'confidence': 1.0}]   #匹配度为 100%
if len(match_result) > 0:
    print(len(match_result))
    x1, y1 = match_result[0]['result']
    print(x1)  #115.5   X的中心点
    print(y1)  #914.0   y的中心点
    pass
else:
    print('识别不到要点击的目标')

# -*- encoding=utf-8 -*-
# __author__ = 'Jeff.xie'

import aircv as ac
from PIL import Image

# 需要当前目录准备两个图片,big.jpg 和 small.jpg

class CompareImage():
    # 可以通过confidencevalue来调节相似程度的阈值,小于阈值不相似
    def matchImg(self, imgsrc, imgobj, phone_x, phone_y, confidencevalue=0):  # imgsrc=原始图像,imgobj=待查找的小图片
        imsrc = ac.imread(imgsrc)
        imobj = ac.imread(imgobj)
        match_result = ac.find_template(imsrc, imobj, confidencevalue)
        print(match_result,type(match_result),end="*****\n")
        if match_result is not None:
            # match_result:[{'result': (115.5, 914.0),
            #  'rectangle': ((42, 843), (42, 985), (189, 843), (189, 985)),  #匹配图片的四个角的坐标
            # 'confidence': 1.0}]   #匹配度为 100%
            match_result['shape'] = (imsrc.shape[1], imsrc.shape[0])  # 0为高,1为宽
            x, y = match_result['result']  # 标准图中小图中心位置x,y
            print("x",x)
            print("y",y)
            shape_x, shape_y = tuple(map(int, match_result['shape']))  # 标准图中x,y
            print("shape_x",shape_x)  #大图片宽度
            print("shape_y",shape_y)  #大图片高度
            position_x, position_y = int(phone_x * (x / shape_x)), int(phone_y * (y / shape_y))
            print("position_x",position_x)
            print("position_y",position_y)
        else:
            return None,None,None,None
        # print(match_result)
        # return match_result
        return position_x, position_y, str(match_result['confidence'])[:4], match_result

    def fixed_size(self, width, height, infile, outfile):
        """按照固定尺寸处理图片"""
        im = Image.open(infile)
        out = im.resize((width, height), Image.ANTIALIAS)
        out.save(outfile)

    def get_picture_size(self, imgsrc):
        '''获取图片长,宽'''
        imsrc = ac.imread(imgsrc)
        y, x, z = imsrc.shape
        return x, y


if __name__ == '__main__':

    # result = CompareImage().matchImg("D:\\Setting.png","D:\\WIFI1.png",10,10)
    result = CompareImage().matchImg("D:\\Setting.png","D:\\Battery.png",10,10)  # 大图,小图
    zuobiao = result[3]["rectangle"]
    xmin = zuobiao[0][0]
    ymin = zuobiao[0][1]
    xmax = zuobiao[2][0]
    ymax = zuobiao[3][1]

    # 在原始图片上绘制相似的区域
    import cv2
    image = cv2.imread('D:\\Setting.png')
    print("xmin",xmin)
    print("ymin",ymin)
    print("xmax",xmax)
    print("ymax",ymax)
    cv2.rectangle(image, (xmin, ymin), (xmax, ymax), (0, 0, 255), 2)
    cv2.imwrite('D:/result.jpg', image)

版权声明:本文为qq_30273575原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。