Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Image template matching with Python OpenCV

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you the relevant knowledge points about how to achieve image template matching in Python OpenCV. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

1. What is template matching and introduction of template matching method matchTemplate ()

Provide a template image, a target image, and satisfy that the template image is a part of the target image. The process of finding a specific template image from the target image is template matching. OpenCV provides the matchTemplate () method to help us achieve template matching.

The method syntax is as follows:

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

Among them

Image is the target image

Templ is template image

Method is the way to match.

Mask is the mask, optional. This parameter is supported only if method is cv2.TM_SQDIFF or cv2.TM_CCORR_NORMED.

The method parameter can have the following values:

Parameter values describe the sum of squares of cv2.TM_SQDIFF differences, also known as square difference matching. It can be understood as the matching based on the degree of difference, the smaller the degree of difference, the higher the degree of matching. When there is a perfect match, the sum of the squares of the difference is 0. Cv2.TM_SQDIFF_NORMED related matching. It can be understood as the matching based on the degree of similarity, the higher the degree of similarity, the greater the result, the higher the degree of matching. Cv2.TM_CCORR standard related matching. The rules are the same as above. Cv2.TM_CCORR_NORMED correlation coefficient match cv2.TM_CCOEFF correlation coefficient match. Also based on the degree of similarity of the match, the result is a floating-point number from-1 to 1, 1 indicates a perfect match, 0 indicates no relationship, and-1 indicates that the brightness of the two images is exactly the opposite. The cv2.TM_CCOEFF_NORMED standard correlation coefficient matches, and the rule is the same as above.

Using the matchTemplate () method, the template covers every region in the image and uses the selected method method to calculate each time, and the result of each calculation is finally returned to us in the form of a two-dimensional array.

Material preparation

To facilitate the display, the following picture materials are specially prepared:

Select the world famous painting "three British War Lv Bu" (test.png), the image shape is (738,675,3):

Pick out some of the image elements as the template material to be used below. The selection code is as follows (screenshots are not recommended. The size of screenshots may not be guaranteed):

Import cv2img = cv2.imread ("test.png") print (img.shape) # Electric img1 = img [20 img0 220, 320 print 480,:] # Hulaoguan plaque img2 = img [75 print 150, 200 test.png 310,:] # Qinglong Dao img3 = img [170 test.png 530,575 print 650,:] # Guan Yunchang img4 = img [270Rd 670,160 img4 330,] cv2.imshow ("img0", img) cv2.imshow ("img1", img1) cv2.imshow ("img2" Img2) cv2.imshow ("img3", img3) cv2.imshow ("img4", img4) cv2.waitKey () cv2.destroyAllWindows () cv2.imwrite ('template_pic1.jpg', img1) cv2.imwrite (' template_pic2.jpg', img2) cv2.imwrite ('template_pic3.jpg', img3) cv2.imwrite (' template_pic4.jpg', img4)

The template material taken out is as follows:

Lights

Hulaoguan plaque

Qinglong Dao

Guan Yunchang

two。 Single template matching

Single template matching, that is, the matching process in which only one template is used. It can be divided into single-target matching and multi-objective matching.

2.1 single target matching

Single-target matching, that is, the template only matches the matching result with the highest degree of matching in the target image.

This requires finding out the coordinates of the location of the matching result this time to determine its location.

OpenCV provides cv2.minMAXLoc () for implementation.

The parameter of this method is the return value of matchTemplate (), which returns a tuple. There are four values in the tuple, namely, the minimum value, the maximum value, the vertex coordinate of the upper-left corner of the image, and the vertex coordinate of the upper-left corner of the image when the maximum value.

Next, use the template_pic1 image to match the original image, and circle the template image in the original image with a red rectangle, using the matching method of the sum of squares of the standard deviation, the code is as follows:

Import cv2img = cv2.imread ("test.png") templ = cv2.imread ("template_pic1.jpg") height, width, c = templ.shaperesults = cv2.matchTemplate (img, templ, cv2.TM_SQDIFF_NORMED) # get the minimum, maximum, minimum and maximum coordinates minValue, maxValue, minLoc, maxLoc = cv2.minMaxLoc (results) resultPoint1 = minLocresultPoint2 = (resultPoint1 [0] + width, resultPoint1 [1] + height) cv2.rectangle (img, resultPoint1, resultPoint2, (0,0) Cv2.imshow ("img", img) cv2.waitKey () cv2.destroyAllWindows ()

As shown in the figure, the template diagram is marked successfully.

If you want to find the result that best matches the template from multiple images

Taking the matching method of the sum of squares of standard deviation as an example

Then we can traverse these images, compare the minimum values in the corresponding results of each image, and find out the minimum value of the minimum value, which is the best match.

Take two images as an example, flip the original image once to produce a new image (the result is similar to the original image, but there is a great difference)

Flip to produce material (test1.png)

Import cv2img = cv2.imread ("test.png") dst1 = cv2.flip (img, 1) cv2.imshow ("dst1", dst1) cv2.waitKey () cv2.destroyAllWindows () cv2.imwrite ('test1.png', dst1)  

Then use the template Guan Yunchang (template_pic4.jpg) to match the two images, output the best match results, and draw a red box to show:

Import cv2image = [] image.append (cv2.imread ("test.png")) image.append (cv2.imread ("test1.png")) templ = cv2.imread ("template_pic4.jpg") height, width, c = templ.shape# Loop variable initialization # this is just a random value, which is meaningless, just to define the variable # using the TM_SQDIFF_NORMED calculation method, the result is usually less than 1 So minValue can be set to 1. If it is the TM_SQDIFF calculation method, it will not work, the calculated value will be very large. The code is no longer valid, and you need to make the minMax larger, or make other changes. Index =-1minValue = 1minLoc1 = (0,0) # traverse each image for i in range (0, len (image)): results = cv2.matchTemplate (image [I], templ, cv2.TM_SQDIFF_NORMED) min = cv2.minMaxLoc (results) [0] if min

< minValue: minValue = min minLoc1 = cv2.minMaxLoc(results)[2] index = iminLoc2 = (minLoc1[0] + width, minLoc1[1] + height)cv2.rectangle(image[index], minLoc1, minLoc2, (0, 0, 255), 2)cv2.imshow("result", image[index])cv2.waitKey()cv2.destroyAllWindows() 如图,test.png中的关云长与模板更为匹配。 2.2 多目标匹配 多目标匹配,即在目标图像中匹配出所有与模板图像匹配的结果。可以使用相关匹配或相关系数匹配。 素材准备 还以原图像"test.png"为参照, 为了产生方便我们做示例的图像,我们在该图像的基础上多加一盏电灯,生成"test2.png" import cv2img = cv2.imread("test.png")templ = cv2.imread("template_pic1.jpg")img[20:220, 30:190, :] = templcv2.imshow("img", img)cv2.waitKey()cv2.destroyAllWindows()cv2.imwrite('test2.png', img)       多目标匹配 多目标匹配即对matchTemplate()匹配的总的结果,的计算情况数据,使用for循环遍历,并设定一个判断标准。 如使用标准相关系数(cv2.TM_CCOEFF_NORMED)的方法判断,如:如果计算值大于0.99,则我们认为匹配成功了。 使用电灯模板"template_pic1.jpg",匹配图像test2.png。并对匹配的结果用红色的矩形框标记。 代码示例如下: import cv2img = cv2.imread("test2.png")templ = cv2.imread("template_pic1.jpg")height, width, c = templ.shape# 按照标准相关系数匹配results = cv2.matchTemplate(img, templ, cv2.TM_CCOEFF_NORMED)for y in range(len(results)): for x in range(len(results[y])): if results[y][x] >

Cv2.rectangle (img, (x, y), (x + width, y + height), (0,0,255), 2) cv2.imshow ("img", img) cv2.waitKey () cv2.destroyAllWindows ()

The execution results of the program are as follows, and the two lights are matched successfully.

3. Multi-template matching

Multi-template matching, that is, the matching process of single template for n times.  

Go straight to the example:

Match the four image templates of Electric Lamp, Qinglong Dao, Hulaoguan plaque and Guan Yunchang in test.png:

Import cv2def myMatchTemplate (img, templ): height, width, c = templ.shape results = cv2.matchTemplate (img, templ, cv2.TM_CCOEFF_NORMED) loc = list () for i in range (len (results)): for j in range (len (results [I]): if results [I] [j] > 0.99: loc.append ((j, I, j + width) I + height)) return loc# reads the original image img = cv2.imread ("test.png") # template list templs = list () templs.append (cv2.imread ("template_pic1.jpg")) templs.append (cv2.imread ("template_pic2.jpg")) templs.append (cv2.imread ("template_pic3.jpg")) templs.append (cv2.imread ("template_pic4.jpg") loc = list () for t in templs: loc + = myMatchTemplate (img) T) # iterate through the coordinates of all red boxes for i in loc: cv2.rectangle (img, (I [0], I [1]), (I [2], I [3]), (0,0,255), 2) cv2.imshow ("img", img) cv2.waitKey () cv2.destroyAllWindows ()

The matching results are as follows:

These are all the contents of the article "how to achieve Image template matching in Python OpenCV". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report