ac.find_templateは画像を認識し、それを見つけます

ここのACはaircvで、pipで直接インストールできます

pip install aircv

これを使用する場合は、次を使用できます。importaircv as
acaircvのinitファイルのfind_template関数の説明は次のとおりです。

def find_template(im_source, im_search, threshold=0.5, rgb=False, bgremove=False):
    '''
    @return find location
    if not found; return None
    '''
    result = find_all_template(im_source, im_search, threshold, 1, rgb, bgremove)
    return result[0] if result else None

たとえば、大きな画像で小さな画像の位置を見つけて、マークを付けます。
ここに画像の説明を挿入

ここで2枚の写真を見つけましたここに画像の説明を挿入

import aircv as ac
import pyautogui
import cv2
import time
import os

def match(IMSRC, IMOBJ):
    # 匹配图标位置
    imsrc = cv2.imread(IMSRC)
    imobj = cv2.imread(IMOBJ)
    pos = ac.find_template(imsrc, imobj, 0.2)
    if pos == None:
        print("最终没能匹配到:" + imsrc)
    else:
        try:
            show_and_save(IMSRC, pos)
        except Exception as e:
            print("保存匹配结果show_and_save这里出错,错误原因为{}".format(e))
        print(pos)
        point = pos['result']
        pyautogui.moveTo(point)
        print("匹配成功:{}".format(IMSRC))
        time.sleep(0.5)
        return (point)


def show_and_save(imgPath, pos):
    print("Img:",imgPath)
    img = cv2.imread(imgPath)
     # 画矩形
    cv2.rectangle(img, pos['rectangle'][0], pos['rectangle'][3], (0, 0, 255), 2)  # 红
    # 画中心点
    cv2.circle(img, tuple(map(int, pos['result'])), 3, (255, 0, 0), -1)  # -1表示填充

    time_now = time.strftime('%Y-%m-%d %H%M%S', time.localtime(time.time()))
    save_jpg = os.path.dirname(imgPath) + "/" + time_now + '.jpg'
    print("path:",save_jpg)
    cv2.imwrite(save_jpg, img)


if __name__=='__main__':
    IMSRC="E:/program/python/test/test1/13.png"
    IMOBJ="E:/program/python/test/test1/12.png"
    match(IMSRC,IMOBJ)


操作の結果は次のとおりです。


Img: E:/program/python/test/test1/13.png
path: E:/program/python/test/test1/2021-03-09 142504.jpg
{
    
    'result': (234.5, 176.5), 'rectangle': ((150, 143), (150, 210), (319, 143), (319, 210)), 'confidence': 0.5964502096176147}
匹配成功:E:/program/python/test/test1/13.png

結果のデータの意味は、元の画像上の一致する画像の中心座標点であり、これは私たちが探しているクリックポイントです。長方形は直交座標(左上隅は座標の原点)であり、信頼度は一致の程度。

ここに画像の説明を挿入

注:find_template(im_source、im_search、threshold = 0.5)は、最終的な信頼度<thresholdの場合、Noneを返します。

おすすめ

転載: blog.csdn.net/liulanba/article/details/114578093