opencv learning ten: template matching

Template matching principle

Template matching is one of the methods to find a specific target in an image. The principle of this method is very simple. It traverses every possible position in the image and compares whether it is "similar" to the template. When the similarity is high enough At that time, we thought we found our goal.

In fact, the idea of ​​template matching is also very simple and violent. It is to hold the template picture and slide from top left to bottom right in the original image until the similarity of a certain area is lower than the threshold we set, then we think The area matches the template, that is, we found the location we were looking for and marked it.
Let T represent the template image, I represent the image to be matched, the width of the cut template image is w and the height is h, and R is the matching result. The matching process is shown in the figure below:
Insert picture description here
OpenCV provides 6 template matching algorithms:
Insert picture description hereCV_TM_SQDIFF Squared difference matching method , The best match is 0, the larger the value, the worse the match
CV_TM_SQDIFF_NORMED Normalized square difference matching method
CV_TM_CCORR Correlation matching method, using multiplication operation, the larger the value indicates the better the match
CV_TM_CCORR_NORMED Normalized correlation matching method
CV_TM_CCOEFF Correlation coefficient matching method , The best match is 1, and -1 means the worst match.
CV_TM_CCOEFF_NORMED Normalized correlation coefficient matching method. The
smaller the value of the first two methods, the better the match, and the larger the value of the last four methods, the better the match.

Insert picture description here

OpenCV template matching implementation

cv2.matchTemplate(image, templ, method, result=None, mask=None)

image: image to be searched
templ: template image
method: method of calculating the matching degree
result: matching result, a matrix

cv2.minMaxLoc(src, mask=None)
src is a matrix. Enter the return value of cv2.matchTemplate

Function function: Suppose there is a matrix a, and now you need to request the minimum and maximum values ​​of this matrix, and get the maximum and minimum indexes. Index is position coordinate

import cv2 as cv
import numpy as np

def template_demo():
    tpl = cv.imread("C:/Users/lenovo/Desktop/opencv/daima/banknum/template-matching-ocr/images/tpl.png") #模板
    target = cv.imread("C:/Users/lenovo/Desktop/opencv/daima/banknum/template-matching-ocr/images/credit_card_01.png")#待检测图
    cv.imshow("template image", tpl)
    cv.imshow("target image", target)
    methods = [cv.TM_SQDIFF_NORMED, cv.TM_CCORR_NORMED, cv.TM_CCOEFF_NORMED]#归一化均方误差,归一化相关匹配,归一化相关系数
    th, tw = tpl.shape[:2] #取出模板的高宽 [:n]表示此列表的前n维度
    for md in methods:
        print(md)
        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 #tl是矩形左上角的点
        else:
            tl = max_loc
        br = (tl[0] + tw, tl[1] + th);
        cv.rectangle(target, tl, br, (0, 0, 255), 2)#rectangle函数用法,第一个参数是在哪个图上绘图,第二个和第三个是左上、右下位置顶点坐标,颜色为红色,宽度为2
        #cv.imshow("match--"+np.str(md), target)
        cv.imshow("match--" + np.str(md), result)
#match1 是平方不同的归一化,3是相关性的归一化,5是相关性因子的归一化
#以0~255的灰度范围标记算法算出来的相关性,目标图像的区域越白,这个区域与template的相关性越大


cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)

template_demo()
cv.waitKey(0)
cv.destroyAllWindows()

Run screenshot:
match1 is the normalization of different squares, 3 is the normalization of the correlation, and 5 is the normalization of the correlation factor.
Insert picture description here
You can also run the result to view:
the correlation calculated by the gray scale mark algorithm from 0 to 255 The whiter the area of ​​the target image, the greater the correlation between this area and the template.
Insert picture description here
Why is the result image smaller than the image to be detected?
Because result subtracts the width and height of the template, the center of the area covered by the four vertices of the template is exactly half of the width and height.
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44145452/article/details/112505445