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

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

这是为什么?

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

相关推荐
YHHLAI6 分钟前
[特殊字符] Agent 智能体开发实战 · 第一课:Tool Use —— 让大模型自动干活
前端·人工智能
Mx_coder15 分钟前
8年Java开发者AI转型第一周:从零搭建RAG文档问答系统(Day 5-7)
人工智能
hixiong12319 分钟前
TensorRT转换工具分享
人工智能·计算机视觉·ai·c#
NiceCloud喜云19 分钟前
Anthropic 一周三连发:Cowork 多端、Fable 5 按需付费、J-space 论文的技术解读
java·服务器·网络·人工智能·ai
凉云生烟21 分钟前
机器学习 02- KNN算法
人工智能·算法·机器学习
fenglllle22 分钟前
chromadb emmbedding 向量检索
人工智能·python·embedding
cui_ruicheng26 分钟前
Python从入门到实战(六):非序列容器
开发语言·python
月光刺眼27 分钟前
用LangChain 打造第一个 Agent:LLM 大脑与 Tool 手脚的完整闭环
javascript·人工智能·全栈
小林ixn28 分钟前
手写AI编程Agent:从0到1复刻Trae/Cursor的核心能力
人工智能·aigc·ai编程
冬哥聊AI31 分钟前
腾讯三面:Claude Code 写书超过上下文长度怎么办?/compact 只是应急,五步架构才是正解
人工智能