python图片匹配方法

python图片匹配方法

装opencv

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python

然后先写个显示图片

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import cv2
#读取图像,支持 bmp、jpg、png、tiff 等常用格式
image = cv2.imread("i:\111.png")
#创建窗口并显示图像
cv2.imshow("11",image)
cv2.waitKey(0)
#释放窗口
cv2.destroyAllWindows()
import cv2 as cv
import numpy as np

target = cv.imread("i:\111.png")
tpl = cv.imread("i:\111x.png")
methods = [cv.TM_SQDIFF_NORMED, cv.TM_CCORR_NORMED, cv.TM_CCOEFF_NORMED]
th, tw = tpl.shape[:2]
for md in methods:
        result = cv.matchTemplate(target, tpl, md)
        min_val, max_val, min_loc, max_loc = cv.minMaxLoc(result)
        if md == cv.TM_SQDIFF_NORMED:
            tl = min_loc
        else:
            tl = max_loc
        br = (tl[0] + tw, tl[1] + th)
        print("左上角坐标")
        print(tl);
        print("右下角坐标")
        print(br);
        cv.rectangle(target, tl, br, [0, 0, 0])
        cv.imshow("pipei"+np.str(md), target)
        # break;
cv.waitKey(0)
cv.destroyAllWindows()

这里用了3个方法遍历匹配 

 有缩进的时候 注意复制的时候 几块几块来复制

因为template match都能匹配到 不管有没有 

所以 要template match要判断有没有找到匹配的东西按照获取到的坐标在不在那个位置。。 用 TM_SQDIFF_NORMED比较精确

原文地址:https://www.cnblogs.com/newmiracle/p/12434282.html