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()

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

这是为什么?

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

相关推荐
youdexiang23 分钟前
Android录音软件时间关键词检索功能分析
java·人工智能
WUYOUGYLU34 分钟前
大模型时代:人与智能的共同进化
人工智能
netkiller-BG7NYT35 分钟前
基于图像识别的人工智能中医望诊方案
人工智能·百度
W***259238 分钟前
Work Agent深度解读:AI长程任务的执行机制与落地形态
人工智能
for_ever_love__1 小时前
函数——参数、lambda、*args与作用域
python·函数·大模型开发·编程基础
东莞市云毅网络有限公司1 小时前
企业资料时效性判定:Last-Modified、正文日期与版本字段三路交叉
python·sqlite·自动化运维·geo·数据监测
Query*1 小时前
深入浅出LangGraph【一】_基础篇
python·ai·langchain
能源革命1 小时前
Hermes Agent 常用命令速查表
人工智能
XiaoMaqqqq1 小时前
市面上正规的IP驱动产业新场景新工具哪家强
运维·python·网络协议·tcp/ip
zhizhizhuzhuxia1 小时前
电视剧解说配音怎么选工具,从情绪表现到长文本稳定性的一次实测梳理
人工智能·语音识别