用python+opencv实现以下要求: 1、输入参数为模板图 StandardImage 、特征点区域 Areas 、图片压缩倍率 CompressSize 、查找方向X/Y/XY方向 Direction 、查找范围 FindSize 、源图 SrcImage 2、特征点区域 Areas 可能有多个,并在后续得到的 ROI 区域中找到 Areas 。图片压缩倍率 CompressSize 使用图像金字塔技术 3、除去源图 SrcImage,根据输入参数生成粗定位配置 DConfig ,该步骤写在初始化部分,只执行一次; 4、根据粗定位配置对原图进行模板匹配,找到定位。该步骤写在执行部分,因为原图可能有多张,需要多次执行 5、输出参数为模板图 StandardImage 中心位置坐标 GArea 、经过模板匹配后在源图 SrcImage 上找到的中心位置 DArea
时间: 2024-02-01 12:13:02 浏览: 36
好的,以下是实现的代码:
```python
import cv2
class TemplateMatcher:
def __init__(self, StandardImage, Areas, CompressSize=1, Direction='XY', FindSize=None):
"""
初始化模板匹配器
:param StandardImage: 模板图像
:param Areas: 特征点区域,可能有多个
:param CompressSize: 压缩倍率,默认为1
:param Direction: 查找方向,默认为XY方向,可选X、Y、XY
:param FindSize: 查找范围,默认为None,表示整张图像
"""
self.StandardImage = StandardImage
self.Areas = Areas
self.CompressSize = CompressSize
self.Direction = Direction
self.FindSize = FindSize
# 生成粗定位配置
self.DConfig = self.get_coarse_config()
def get_coarse_config(self):
"""
生成粗定位配置,只需执行一次
"""
# 将模板图像缩小
StandardImage_small = cv2.resize(self.StandardImage, (0,0), fx=self.CompressSize, fy=self.CompressSize)
# 转为灰度图像
StandardImage_small_gray = cv2.cvtColor(StandardImage_small, cv2.COLOR_BGR2GRAY)
# 获取模板图像的尺寸
h, w = StandardImage_small_gray.shape
# 根据查找方向生成搜索范围
if self.Direction == 'X':
FindSize = (self.FindSize[0], h)
elif self.Direction == 'Y':
FindSize = (w, self.FindSize[1])
else:
FindSize = self.FindSize
# 对特征点区域缩小,得到粗定位区域
CoarseAreas = []
for area in self.Areas:
x, y, w, h = area
x = int(x * self.CompressSize)
y = int(y * self.CompressSize)
w = int(w * self.CompressSize)
h = int(h * self.CompressSize)
CoarseAreas.append((x, y, w, h))
# 匹配模板图像和粗定位区域,得到粗定位配置
DConfig = []
for area in CoarseAreas:
x, y, w, h = area
ROI = StandardImage_small_gray[y:y+h, x:x+w]
result = cv2.matchTemplate(self.SrcImage, ROI, cv2.TM_CCORR_NORMED, mask=None, searchSize=FindSize)
_, _, _, maxLoc = cv2.minMaxLoc(result)
dx = maxLoc[0] - x
dy = maxLoc[1] - y
DConfig.append((dx, dy))
return DConfig
def match(self, SrcImage):
"""
在原图上执行模板匹配,找到定位
:param SrcImage: 原图像
:return: 中心位置坐标
"""
self.SrcImage = SrcImage.copy()
# 将原图像缩小
SrcImage_small = cv2.resize(self.SrcImage, (0,0), fx=self.CompressSize, fy=self.CompressSize)
# 转为灰度图像
SrcImage_small_gray = cv2.cvtColor(SrcImage_small, cv2.COLOR_BGR2GRAY)
# 对特征点区域缩小,得到粗定位区域
CoarseAreas = []
for area in self.Areas:
x, y, w, h = area
x = int(x * self.CompressSize)
y = int(y * self.CompressSize)
w = int(w * self.CompressSize)
h = int(h * self.CompressSize)
CoarseAreas.append((x, y, w, h))
# 对每个特征点区域执行模板匹配,得到中心位置
GArea = []
for i, area in enumerate(CoarseAreas):
dx, dy = self.DConfig[i]
x, y, w, h = area
x += dx
y += dy
ROI = self.StandardImage[y:y+h, x:x+w]
ROI_small = cv2.resize(ROI, (0,0), fx=self.CompressSize, fy=self.CompressSize)
ROI_small_gray = cv2.cvtColor(ROI_small, cv2.COLOR_BGR2GRAY)
result = cv2.matchTemplate(SrcImage_small_gray, ROI_small_gray, cv2.TM_CCORR_NORMED, mask=None, searchSize=self.FindSize)
_, _, _, maxLoc = cv2.minMaxLoc(result)
cx = int(maxLoc[0] / self.CompressSize + x + w/2)
cy = int(maxLoc[1] / self.CompressSize + y + h/2)
GArea.append((cx, cy))
return GArea
```
使用方法如下:
```python
# 加载模板图像和原图像
StandardImage = cv2.imread("template.png")
SrcImage = cv2.imread("source.png")
# 设置特征点区域和查找范围
Areas = [(100, 100, 50, 50), (200, 200, 50, 50)]
FindSize = (300, 300)
# 创建模板匹配器对象
matcher = TemplateMatcher(StandardImage, Areas, CompressSize=0.5, Direction='XY', FindSize=FindSize)
# 执行模板匹配,得到中心位置坐标
GArea = matcher.match(SrcImage)
# 输出结果
print(GArea)
```
其中,`StandardImage` 是模板图像,`Areas` 是特征点区域,`FindSize` 是查找范围。在创建模板匹配器对象时,需要指定压缩倍率 `CompressSize` 和查找方向 `Direction`,并调用 `get_coarse_config()` 方法生成粗定位配置。在执行模板匹配时,需要传入原图像,调用 `match()` 方法得到中心位置坐标。