当前位置:网站首页>cv. Matchtemplate image model matching opencv
cv. Matchtemplate image model matching opencv
2022-06-11 03:03:00 【Solitary rest】
Template matching is a method to search and find the position of template image in large image ,OpenCV Provides cv.matchTemplate function , Its essence is to match the small image img1 stay img2 Slide up , Calculate match , Get the matching degree of different regions .
cv.matchTemplate() There are many ways to calculate the matching degree , The demonstration results are presented here , But no detailed explanation .
For the original image :


import cv2.cv2
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread(r'XXXX')
img2 = img.copy()
template = cv.imread(r'xxxx')
w= template.shape[0]
h= template.shape[1]
methods = ['cv.TM_CCOEFF', 'cv.TM_CCOEFF_NORMED', 'cv.TM_CCORR',
'cv.TM_CCORR_NORMED', 'cv.TM_SQDIFF', 'cv.TM_SQDIFF_NORMED']
for meth in methods:
img = img2.copy()
method = eval(meth)# because meth Is string , So we have to deal with
res = cv.matchTemplate(img,template,method)
min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)
if method in [cv.TM_SQDIFF, cv.TM_SQDIFF_NORMED]:
top_left = min_loc
else:
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
cv.rectangle(img,top_left, bottom_right, 255, 2)
plt.subplot(121),plt.imshow(res,cmap = 'gray')
plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(img,cmap = 'gray')
plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
plt.suptitle(meth)
plt.show()
res It is the distribution of matching degree mentioned above , In some ways , Higher matching , The lower the calculated value , for example cv.TM_SQDIFF, cv.TM_SQDIFF_NORMED, So it needs special treatment .
We make use of cv.minMaxLoc Find the best match , And draw the template in the original image .






Multi template matching :
For the original image :


import cv2.cv2
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread(r'XXXXX\marry.jpg')
img2 = img.copy()
template = cv.imread(r'XXXXX\marry1.jpg')
w= template.shape[0]
h= template.shape[1]
res = cv.matchTemplate(img2,template,cv.TM_CCOEFF_NORMED)
threshold = 0.8# Set the threshold , Greater than the threshold is considered to be matched
loc = np.where( res >= threshold)
img = img2.copy()
for pt in zip(*loc[::-1]):
cv.rectangle(img, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
cv.imwrite(r'XXXX\res.png',img)
边栏推荐
- Error excluded identifier before '(' token, grpc enumeration class compilation error
- Arduino Uno接JQ8900-16p语音播报模块
- Blue Bridge Cup_ Xiao Lan eats candy_ Pigeon nest principle / drawer principle
- TCP/IP
- Net core Tianma XingKong series - Interface Implementation for dependency injection and mutual conversion of database tables and C entity classes
- 出栈序列是否是入栈序列
- HOSATAPD_ CLI debug using WiFi whitelist
- How to fix syntax errors in WordPress websites
- 判断一串数字是否是快速排序某一次的结果
- CPT 102_ LEC 20
猜你喜欢

Necessity for banks to choose electronic bidding procurement
![[new open source project] dynamic configuration task scheduling framework gobrs async joins the dromara open source community](/img/26/2d0c4e8f7f4055e614080fe526328c.png)
[new open source project] dynamic configuration task scheduling framework gobrs async joins the dromara open source community

Go quick start of go language (I): the first go program

Young people fleeing big cities: shouldering housing prices and pressure, but not epidemics

B_QuRT_User_Guide(17)

基于互联网架构演进, 构建秒杀系统

The two request sessionids of the same user are inconsistent ----- record the problem
![[big guy show] aiops in the eyes of Borui data, choosing the right track and the right people](/img/a6/61d125326fc81532a56858c384460f.jpg)
[big guy show] aiops in the eyes of Borui data, choosing the right track and the right people

Go language advantages and learning Roadmap
![WordPress upgrade error: briefly unavailable for scheduled maintenance [resolved]](/img/f5/24e283d7d11e4487bfa231d9aa7ae8.jpg)
WordPress upgrade error: briefly unavailable for scheduled maintenance [resolved]
随机推荐
怎样简洁明了地说清楚产品需求?
Solr initialization failure: requesthandler INIT failure
ASLR
The Google search console webmaster tool cannot read the sitemap?
Live broadcast! Feature matching of orb-slam3 series (mlpnp, word bag model, etc.).
Baidu submits sitemap to prompt the solution of "index type is not handled"
Win10 安装Office 2016出现错误代码30204-44怎么处理?
Whether the outbound sequence is the inbound sequence
巴歇尔槽流量计远程采集物联网关在明渠流量监测的应用
[C language classic]: inverted string
C语言数组与指针练习题
CPT 102_ LEC 20
基于互联网架构演进, 构建秒杀系统
The two request sessionids of the same user are inconsistent ----- record the problem
Wechat template message errCode ": 40165," errmsg ":" invalid web pagepath
出栈序列是否是入栈序列
Array full permutation
WordPress article directory plug-in luckywp table of contents setup tutorial
Go语言之Go 快速入门篇(一):第一个 Go 程序
Uni app - one click access to user information