Opncv模板匹配 单模板匹配 多模板匹配

目录

问题引入

单模板匹配

①模板匹配函数:

②查找最值和极值的坐标和值:

整体流程原理介绍

实例代码介绍:

多模板匹配

①定义阈值

②zip函数

整体流程原理介绍

实例代码:


问题引入

下面有请我们的陶大郎登场

这张图片是我们的陶大郎 ,我们接下来将利用陶大郎来介绍我们的模板匹配

我们想要在原图中标记出陶大郎的耳朵,但是又不想手工标记,想要自动标记,这该怎么办呢?

这时候就要利用我们的新知识模板匹配

模板匹配:能够利用我们现有的图片模板,在原图上自动找到我们想要标记的位置

单模板匹配

首先 我们先截取陶大郎的耳朵来作为我们的模板

这个耳朵就是我们的模板图像,从原图上截取下来的

我们先介绍我们要使用的函数:

①模板匹配函数:

cv2.matchTemplate(image, templ, method )

  • image 为原始图像。
  • templ 为模板图像。它的尺寸必须小于或等于原始图像,并且与原始图像具有同样的类型。
  • method 为匹配方法。有6种可能的值

例子:

method 可填写对应数值,也可以直接写参数值

python 复制代码
res = cv2.matchTemplate(img, template, 3)
res = cv2.matchTemplate(img, template, cv2.TM_SQDIFF_NORMED)

②查找最值和极值的坐标和值:

minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc( src )

minVal:最小值

maxVal:最大值

minLoc:最小值坐标

maxLoc:最大值坐标


整体流程原理介绍


实例代码介绍:

python 复制代码
import cv2
from matplotlib import pyplot as plt
img = cv2.imread("hui.jpg",0)
template = cv2.imread("fihui.jpg",0)
# 获取模板的高和宽
h,  w = template.shape[:2]

# 模板匹配
res = cv2.matchTemplate(img, template, 3)
# 定位
# min_val 最小值
# min_loc 最小值坐标
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)

fondsite = cv2.rectangle(img, top_left, bottom_right, 255, 2)

cv2.imshow("fondsite",fondsite)
cv2.waitKey()
cv2.destroyAllWindows()

这里咱们的top_left = max_loc选用的max_loc是因为我们的模板匹配使用的 序号3的,使用了归一化,那当然是越大越大,表示近似度越高!


我们运行看看效果

我们可以看到陶文辉的耳朵被圈起来了!


多模板匹配

多模板匹配咱们就用陶大郎的眼睛吧!

重点代码函数解释:

①定义阈值

python 复制代码
# 取匹配程度大于%97的坐标
# 定义的阈值 threshold
threshold = 0.97
# np.where返回的坐标值(x,y)是(h,w)
loc = np.where(res >= threshold)

我们添加了threshold来表示我们的相似度

通过np.where筛选出相似度大于97%的部分放入loc

重点!!!!!:此时loc里面存放的格式是**((x1,x2,...),(y1,y2,....)) 这样的格式**

②zip函数

python 复制代码
for top_left in zip(*loc[::-1]):
    bottom_right = (top_left[0] + w, top_left[1] + h)
    cv2.rectangle(img, top_left, bottom_right, 255, 1)

这里唯一个点就是这个zip(*loc[::-1]) 是干嘛的?

他其实就是把我们的loc的格式从 ((x1,x2,...),(y1,y2,....)) 变成了((x1,y1),(x2,y2),(x3,y3),..)

然后依次把(x1,y1)放入top_left中进行遍历画出我们的框

整体流程原理介绍

实例代码:

python 复制代码
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread("hui.jpg", 0)
template = cv2.imread("eye.jpg", 0)
# 获取模板的高和宽
h,  w = template.shape[:2]

# 模板匹配
res = cv2.matchTemplate(img, template, 3)
# 取匹配程度大于%97的坐标
# 定义的阈值 threshold
threshold = 0.97
# np.where返回的坐标值(x,y)是(h,w)
loc = np.where(res >= threshold)
for top_left in zip(*loc[::-1]):
    bottom_right = (top_left[0] + w, top_left[1] + h)
    cv2.rectangle(img, top_left, bottom_right, 255, 1)
cv2.imshow('img', img)
cv2.waitKey()
cv2.destroyAllWindows()

运行我们会发现有很多个标记框

这是为什么?

其实这个是咱们图片的问题,我的这个图片比较模糊, 是当时截图截下来的,有很多噪音点,所以图片质量不太行,导致它觉得陶大郎的眼睛有很多个,但其实只有两个

相关推荐
jmoych3 分钟前
AI正悄然地影响着企业数字化转型
人工智能
蜡笔小新星5 分钟前
Python Kivy库学习路线
开发语言·网络·经验分享·python·学习
说私域5 分钟前
社群团购中的用户黏性价值:以开源小程序多商户AI智能名片商城源码为例
人工智能·小程序
篝火悟者20 分钟前
问题-python-运行报错-SyntaxError: Non-UTF-8 code starting with ‘\xd5‘ in file 汉字编码问题
开发语言·python
hakesashou39 分钟前
python如何比较字符串
linux·开发语言·python
深度学习实战训练营1 小时前
基于keras的停车场车位识别
人工智能·深度学习·keras
乔代码嘚1 小时前
AI2.0时代,普通小白如何通过AI月入30万
人工智能·stable diffusion·aigc
墨@#≯1 小时前
机器学习系列篇章0 --- 人工智能&机器学习相关概念梳理
人工智能·经验分享·机器学习
Elastic 中国社区官方博客1 小时前
Elasticsearch:使用 LLM 实现传统搜索自动化
大数据·人工智能·elasticsearch·搜索引擎·ai·自动化·全文检索
_.Switch1 小时前
Python机器学习模型的部署与维护:版本管理、监控与更新策略
开发语言·人工智能·python·算法·机器学习