SoFunction
Updated on 2024-12-20

Example of OpenCV-Python template matching for the human eye

What is template matching

Template matching refers to finding the most similar part to image B within the current image A. It can be understood as finding fault, but here it is finding out the same information.

Generally we refer to image A as the input image and image B as the template image. The principle of template matching is to slide the template B image over image A and traverse it to find the parts that match it.

Template Matching Functions

In OpenCV, it gives us the () function to accomplish template matching. The complete definition of its function is given below:

def matchTemplate(image, templ, method, result=None, mask=None):

image: original image

templ: template image

method: the matching method. This parameter is realized by TemplateMatchModes as shown in the following table:

parameters retrieve a value hidden meaning
cv2.TM_SQDIFF 0 Matching is done on the basis of variance. If there is a perfect match, the result is 0; if there is no match, a large variance is obtained
cv2.TM_SQDIFF_NORMED 1 Standard (normalized) square difference matching
cv2.TM_CCORR 2 Correlation matching, this type of method multiplies the template image with the input image, if the product is larger, the match is higher; if the product is 0, it means the match is worst
cv2.TM_CCORR_NORMED 3 Standard (normalized) correlation matching
cv2.TM_CCOEFF 4 Correlation systematic matching, this type of method matches the relative value of the template image to its mean, and the correlation value of the input image to its mean. 1 indicates a perfect match, -1 indicates a bad match, and 0 indicates no correlation match whatsoever (random sequence)
cv2.TM_CCOEFF_NORMED 5 Standard (normalized) correlation coefficient matching

result: the return value. It is a result set consisting of a combination of the comparison results for each position, of type single-channel 32-bit floating-point. If the input image size is WH, and the template dimensions are wh, then the size of the returned value is (W-w+1)*(H-h+1).

mask: the template image mask. It must have the same type of size as the template image. Usually the default value is sufficient.

Implementing Template Matching

First of all, we need two images, here we still choose to often use the beauty of the photo as well as to intercept its eyes as part of the template image, as follows:

import cv2
import  as plt

img = ("", 0)
template = ("4_1.jpg", 0)

th, tw = [::]

rv = (img, template, cv2.TM_SQDIFF)
min, max, minLoc, maxLoc = (rv)

topLeft = minLoc
bottomRight = (topLeft[0] + tw, topLeft[1] + th)
(img, topLeft, bottomRight, 255, 2)

(121)
(template, cmap="gray")
('off')
(122)
(img, cmap="gray")
('off')
()

After running it, the result is as follows:

效果

Appendix:

stencil diagram

模板图

original figure

原图

To this article on OpenCV-Python template matching human eye example of the article is introduced to this, more related OpenCV template matching content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!