103.md 3.5 KB
Newer Older
W
init  
wizardforcel 已提交
1 2 3 4 5
# 使用模板进行对象检测

> 原文: [https://pythonspot.com/object-detection-with-templates/](https://pythonspot.com/object-detection-with-templates/)

**模板匹配**是一种用于查找与补丁(模板)相似的图像区域的技术。
W
wizardforcel 已提交
6

W
init  
wizardforcel 已提交
7 8 9 10 11 12
它的应用可以是[机器人](https://pythonspot.com/robotics)或制造业。

## 简介

补丁是具有某些功能的小图像。 模板匹配的目的是在图像中找到补丁/模板。

W
wizardforcel 已提交
13 14
![template matching opencv](img/3d85797cd44b2c4b3348bfd4fc8a7795.jpg) 

W
wizardforcel 已提交
15
与 OpenCV 和 Python 匹配的模板。 模板(左),结果图像(右)
W
init  
wizardforcel 已提交
16 17 18 19 20

[下载代码](https://pythonspot.com/download-vision-examples/)

要找到它们,我们都需要:

W
wizardforcel 已提交
21
*   **源图像(`S`)**:在其中查找匹配项的空间
W
init  
wizardforcel 已提交
22

W
wizardforcel 已提交
23
*   **模板图像(`T`)**:模板图像
W
init  
wizardforcel 已提交
24

W
wizardforcel 已提交
25
模板图像`T`在源图像`S`上滑动(在源图像上移动),并且程序尝试使用统计信息查找匹配项。
W
init  
wizardforcel 已提交
26 27 28

## 模板匹配示例

W
wizardforcel 已提交
29
让我们看一下代码:
W
init  
wizardforcel 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

```py
import numpy as np
import cv2

image = cv2.imread('photo.jpg')
template = cv2.imread('template.jpg')

# resize images
image = cv2.resize(image, (0,0), fx=0.5, fy=0.5)
template = cv2.resize(template, (0,0), fx=0.5, fy=0.5)

# Convert to grayscale
imageGray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
templateGray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)

# Find template
result = cv2.matchTemplate(imageGray,templateGray, cv2.TM_CCOEFF)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
top_left = max_loc
h,w = templateGray.shape
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(image,top_left, bottom_right,(0,0,255),4)

# Show result
cv2.imshow("Template", template)
cv2.imshow("Result", image)

cv2.moveWindow("Template", 10, 50);
cv2.moveWindow("Result", 150, 50);

cv2.waitKey(0)

```

## 说明

W
wizardforcel 已提交
67
首先,我们使用`imread()`加载源图像和模板图像。我们调整它们的大小并将其转换为灰度以便更快地进行检测:
W
init  
wizardforcel 已提交
68 69 70 71 72 73 74 75 76 77 78 79

```py

image = cv2.imread('photo.jpg')
template = cv2.imread('template.jpg')
image = cv2.resize(image, (0,0), fx=0.5, fy=0.5)
template = cv2.resize(template, (0,0), fx=0.5, fy=0.5)
imageGray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
templateGray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)

```

W
wizardforcel 已提交
80
我们使用`cv2.matchTemplate(image, template, method)`方法查找图像中最相似的区域。第三个参数是[统计方法](https://docs.opencv.org/modules/imgproc/doc/object_detection.html?highlight=matchtemplate#matchtemplate)
W
init  
wizardforcel 已提交
81 82 83

![Template Matching](img/671697da89293504fef04cfadad29c6b.jpg)

W
wizardforcel 已提交
84
为您的应用选择正确的统计方法。`TM_CCOEFF`(右),`TM_SQDIFF`(左)
W
init  
wizardforcel 已提交
85

W
wizardforcel 已提交
86 87
此方法具有六个匹配方法:`CV_TM_SQDIFF``CV_TM_SQDIFF_NORMED``CV_TM_CCORR``CV_TM_CCORR_NORMED``CV_TM_CCOEFF``CV_TM_CCOEFF_NORMED`

W
init  
wizardforcel 已提交
88 89 90 91 92 93
这是完全不同的[统计比较方法](https://docs.opencv.org/modules/imgproc/doc/object_detection.html?highlight=matchtemplate#matchtemplate)

最后,我们获得矩形变量并显示图像。

## 局限性

W
wizardforcel 已提交
94
模板匹配不是比例不变的,也不是旋转不变的。 这是一种非常基本和直接的方法,可在其中找到最相关的区域。 因此,这种对象检测方法取决于您要构建的应用程序的类型。 对于非比例和旋转变化的输入,此方法效果很好。
W
init  
wizardforcel 已提交
95

W
wizardforcel 已提交
96
您可能会喜欢:[机器人技术](https://pythonspot.com/robotics)[使用级联](https://pythonspot.com/car-tracking-with-cascades/)的汽车跟踪。
W
init  
wizardforcel 已提交
97

W
wizardforcel 已提交
98
[下载计算机视觉示例和课程](https://pythonspot.com/download-vision-examples/)