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比較精確

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM