OpenCV + matchTemplate 的简单例子

# 导入所需库文件,numpy和cv2。
import cv2
import numpy as np

# 加载原始图像和要搜索的图像模板
img = cv2.imread('wife.jpg')
# OpenCV对原始图像进行处理,创建一个灰度版本
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# 加载将要搜索的图像模板#记录图像模板的尺寸
template = cv2.imread('template2.jpg',0)
# 记录模板尺寸
w, h,  = template.shape[::-1]#输出前两列
# 在灰度图像里进行处理和查找匹配。然后使用相同的坐标在原始图像中进行还原并输出。
# cv2.matchTemplate(image, templ, method[, result])
res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED) #TM_CCOEFF_NORMED是标准相关性系数匹配,matchtemplate常用方法
# 设定阈值 
threshold = 0.7 
# 输出res大于70%的
loc = np.where( res >= threshold)
# 使用灰度图像中的坐标对原始RGB图像进行标记 
for pt in zip(*loc[::-1]): 
    cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (7,249,151), 2) 

# 显示图像 
cv2.imshow('matchTemplate.jpg', img)

# save图像
cv2.imwrite('matchTemplate.jpg', img)

Q&A

Q1:cv2.imread()

A1:

cv2.imread()读取图片,第二个参数:

  • cv2.IMREAD_COLOR:加载彩色图片,这个是默认参数。
  • cv2.IMREAD_GRAYSCALE:以灰度模式加载图片。
  • cv2.IMREAD_UNCHANGED:包括alpha。

上面三个flag分别对应的值为1,0,-1。

Q2:zip函数是什么

A2:

zip使用方法实例

>>>a = [1,2,3]
>>> b = [4,5,6]
>>> c = [4,5,6,7,8]
>>> zipped = zip(a,b)     # 打包为元组的列表
[(1, 4), (2, 5), (3, 6)]
>>> zip(a,c)              # 元素个数与最短的列表一致
[(1, 4), (2, 5), (3, 6)]
>>> zip(*zipped)          # 与 zip 相反,*zipped 可理解为解压,返回二维矩阵式
In []: loc[::-1]
Out []: 
(array([522, 521, 522, 523, 521, 522, 523, 520, 521, 522, 523, 520, 521,
        522, 523, 520, 521, 522, 523, 520, 521, 522, 523, 520, 521, 522,
        523, 519, 520, 521, 522, 523, 519, 520, 521, 522, 523, 519, 520,
        521, 522, 523, 519, 520, 521, 522, 523, 518, 519, 520, 521, 522,
        523, 518, 519, 520, 521, 522, 523, 518, 519, 520, 521, 522, 523,
        518, 519, 520, 521, 522, 523, 517, 518, 519, 520, 521, 522, 523,
        517, 518, 519, 520, 521, 522, 523, 517, 518, 519, 520, 521, 522,
        523, 517, 518, 519, 520, 521, 522, 523, 517, 518, 519, 520, 521,
        522, 523, 517, 518, 519, 520, 521, 522, 523, 517, 518, 519, 520,
        521, 522, 517, 518, 519, 520, 521, 522, 517, 518, 519, 520, 521,
        522, 517, 518, 519, 520, 521, 522, 517, 518, 519, 520, 521, 522,
        517, 518, 519, 520, 521, 517, 518, 519, 520, 521, 517, 518, 519,
        520, 521, 517, 518, 519, 520, 521, 517, 518, 519, 520, 521, 517,
        518, 519, 520, 521, 517, 518, 519, 520, 521, 517, 518, 519, 520,
        517, 518, 519, 520, 517, 518, 519, 520, 517, 518, 519, 520, 517,
        518, 519, 517, 518, 519, 517, 518, 519, 517, 518, 519, 518],
       dtype=int64),
 array([834, 835, 835, 835, 836, 836, 836, 837, 837, 837, 837, 838, 838,
        838, 838, 839, 839, 839, 839, 840, 840, 840, 840, 841, 841, 841,
        841, 842, 842, 842, 842, 842, 843, 843, 843, 843, 843, 844, 844,
        844, 844, 844, 845, 845, 845, 845, 845, 846, 846, 846, 846, 846,
        846, 847, 847, 847, 847, 847, 847, 848, 848, 848, 848, 848, 848,
        849, 849, 849, 849, 849, 849, 850, 850, 850, 850, 850, 850, 850,
        851, 851, 851, 851, 851, 851, 851, 852, 852, 852, 852, 852, 852,
        852, 853, 853, 853, 853, 853, 853, 853, 854, 854, 854, 854, 854,
        854, 854, 855, 855, 855, 855, 855, 855, 855, 856, 856, 856, 856,
        856, 856, 857, 857, 857, 857, 857, 857, 858, 858, 858, 858, 858,
        858, 859, 859, 859, 859, 859, 859, 860, 860, 860, 860, 860, 860,
        861, 861, 861, 861, 861, 862, 862, 862, 862, 862, 863, 863, 863,
        863, 863, 864, 864, 864, 864, 864, 865, 865, 865, 865, 865, 866,
        866, 866, 866, 866, 867, 867, 867, 867, 867, 868, 868, 868, 868,
        869, 869, 869, 869, 870, 870, 870, 870, 871, 871, 871, 871, 872,
        872, 872, 873, 873, 873, 874, 874, 874, 875, 875, 875, 876],
       dtype=int64))

Q3:cv2.rectangle()

A3:Python: cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) → None

感谢以下链接提供方法和解决思路

http://www.runoob.com/python/python-func-zip.html

http://bluewhale.cc/2017-09-22/use-python-opencv-for-image-template-matching-match-template.html

https://stackoverflow.com/questions/38122948/w-h-template-shape-1-results-in-value-error-too-many-values-to-unpack

https://docs.opencv.org/trunk/d4/dc6/tutorial_py_template_matching.html

https://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html

http://blog.topspeedsnail.com/?s=template.shape (真的是个很棒的博客,强烈推荐)

猜你喜欢

转载自blog.csdn.net/Weizhiyuan37927/article/details/87970919