当你得到这幅图之后,就可以使用函数 cv2.minMaxLoc() 来找到其中的最小值和最大值的位置。 第一个值为矩形左上角的点(x, y),(w, h)为 模板矩形的宽和高。这个矩形就是找到的模板区域了。 二 模板匹配的方法 1. 模板匹配支持的方法 模板匹配的方法,参数method支持以下几种: enumcv::TemplateMatchModes ...
代码如下: import cv2 as cvimport numpy as npfrom matplotlib import pyplot as pltimg = cv.imread('pics/fengbo.png',0)img2 = img.copy()template = cv.imread('pics/fengbo_lian.PNG',0)w, h = template.shape[::-1]# All the 6 methods for comparison in a listmethods = ['cv.TM_CCOEF...
import cv2 the_temp = cv2.imread(r'images\sheet_template.jpg') the_img = cv2.imread(r'images\sheet_image.jpg') cv2.namedWindow('img', cv2.WINDOW_AUTOSIZE) cv2.namedWindow('template', cv2.WINDOW_AUTOSIZE) cv2.imshow('img', the_img) cv2.imshow('template', the_temp) 1. 2. 3. 4....
templ = cv2.imread("D:/1a.study/opencv/Python OpenCV/sl/10/01/template.png") # 获取模板的高、宽和通道数 height, width, c = templ.shape # 按照标准平方差方式匹配 result = cv2.matchTemplate(img, templ, cv2.TM_SQDIFF_NORMED) # 获取匹配结果 minValue, maxValue, minLoc, maxLoc = cv2.mi...
template_image = cv2.imread('template.jpg', cv2.IMREAD_COLOR)# 多尺度匹配best_match = multi_scale_template_matching(target_image, template_image)ifbest_match: max_val, max_loc, scale = best_matchprint(f"最佳匹配值:{max_val}, 最佳匹配位置:{max_loc}, 最佳缩放比例:{scale}") ...
importcv2ascvimportnumpyasnp deftemplate_matching():sample=cv.imread(r'./test/031.png')# 模板图像 target=cv.imread(r'./test/030.jpg')# 待检测图像 cv.imshow('sample',sample)cv.imshow('target',target)# 三种模板匹配算法 methods=[cv.TM_SQDIFF_NORMED,cv.TM_CCORR_NORMED,cv.TM_CCOEFF_NORM...
OpenCV使用 cv2.matchTemplate() 实现模板匹配。 importcv2importnumpy as npfrommatplotlibimportpyplot as plt img = cv2.imread('lena.jpg', 0) template= cv2.imread('face.jpg', 0) h, w= template.shape[:2]#rows->h, cols->w 匹配函数返回的是一幅灰度图,最白的地方表示最大的匹配。使用 cv2.mi...
1.Template Matching(模板匹配) 模板匹配是一种在较大图像中搜索和查找模板图像位置的方法。OpenCV提供了一个函数cv2.matchTemplate()。它只是在输入图像上滑动模板图像(如在2D卷积中),并比较模板图像下的输入图像的模板和补丁。在OpenCV中实现了几种比较方法。它返回一个灰度图像,其中每个像素表示该像素的邻域与模板匹...
importcv2ascvimportnumpyasnpdeftemplate_matching():sample=cv.imread(r'./test/031.png')# 模板图像target=cv.imread(r'./test/030.jpg')# 待检测图像cv.imshow('sample',sample)cv.imshow('target',target)# 三种模板匹配算法methods=[cv.TM_SQDIFF_NORMED,cv.TM_CCORR_NORMED,cv.TM_CCOEFF_NORMED]hei...
importcv2ascv deftemplate_matching(img_match,img,arithmetic_model):''' 【作用】 进行图片模板匹配 【参数1】 模板图片 【参数2】 进行匹配的图片 【参数3】 算法模型 【返回】 无''' # 进行模板匹配 result=cv.matchTemplate(img,img_match,arithmetic_model)# 获取最小最大匹配值,还有对应的坐标 ...