OpenCV 您所在的位置:网站首页 图像识别模板 OpenCV

OpenCV

2024-07-10 13:39| 来源: 网络整理| 查看: 265

目录

0 原理      1 OpenCV中的模板匹配       2 多对象的模板匹配

0 原理

和 2D 卷积一样,它也是用模板图像在输入图像(大图)上滑动,并在每一个位置对模板图像和与其对应的输入图像的子区域进行比较。OpenCV 提供了几种不同的比较方法。返回的结果是一个灰度图像,每一个像素值表示了此区域与模板的匹配程度。

如果输入图像的大小是(WxH),模板的大小是(wxh),输出的结果的大小就是(W-w+1,H-h+1)。当你得到这幅图之后,再找到其中的最小值和最大值的位置就可以了。第一个值为矩形左上角的点(位置),(w,h)为 moban 模板矩形的宽和高。这个矩形就是找到的模板区域了。

 

1 OpenCV中的模板匹配

模板匹配函数:

cv2.matchTemplate(image, templ, method, result, mask)

image:待搜索图像templ:模板图像method:评价匹配程度的方法,共六种,下面细讲result:匹配结果,如果输入图像为W x H,模板为w x h,则结果为(W - w + 1) x (H - h + 1) mask:掩膜

寻找全局极值函数:

cv2.minMaxLoc(src, mask)

src:输入图像mask:掩膜

输出4个值,分别是最小值,最大值,最小值位置,最大值位置,例如

min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(img)

下面举个例子来说明不同匹配算法的区别:

输入图像:

       待搜索图像:                             模板:              

import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread('test6.jpg', 0) img2 = img.copy() template = cv2.imread('test6_face.jpg', 0) w, h = template.shape[::-1] # 所有的匹配方法 methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR', 'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED'] for meth in methods: img = img2.copy() method = eval(meth) # 去掉字符串的引号 # 匹配 res = cv2.matchTemplate(img, template, method) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) # 使用不同的比较方法,对结果的解释不同 # 如果方法是 TM_SQDIFF or TM_SQDIFF_NORMED, 取最小值 if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]: top_left = min_loc else: top_left = max_loc bottom_right = (top_left[0] + w, top_left[1] + h) cv2.rectangle(img, top_left, bottom_right, 255, 2) plt.subplot(121), plt.imshow(res, cmap='gray'), plt.title('Matching Result'), plt.axis('off') plt.subplot(122), plt.imshow(img, cmap='gray'), plt.title('Detected Point'), plt.axis('off') plt.suptitle(meth) plt.show()

结果如下

1. 相关性系数匹配:cv2.TM_CCOEFF

R(x,y)= \sum _{x',y'} (T'(x',y') \cdot I(x+x',y+y'))

其中:

\begin{array}{l} T'(x',y')=T(x',y') - 1/(w \cdot h) \cdot \sum _{x'',y''} T(x'',y'') \\ I'(x+x',y+y')=I(x+x',y+y') - 1/(w \cdot h) \cdot \sum _{x'',y''} I(x+x'',y+y'') \end{array}

匹配结果:取最大值

2. 标准相关性系数匹配:cv2.TM_CCOEFF_NORMED

R(x,y)= \frac{ \sum_{x',y'} (T'(x',y') \cdot I'(x+x',y+y')) }{ \sqrt{\sum_{x',y'}T'(x',y')^2 \cdot \sum_{x',y'} I'(x+x',y+y')^2} }

匹配结果:取最大值

3. 相关匹配:cv2.TM_CCORR

R(x,y)= \sum _{x',y'} (T(x',y') \cdot I(x+x',y+y'))

匹配结果:取最大值,这个方法效果最不好

4. 标准相关匹配:cv2.TM_CCORR_NORMED

R(x,y)= \frac{\sum_{x',y'} (T(x',y') \cdot I'(x+x',y+y'))}{\sqrt{\sum_{x',y'}T(x',y')^2 \cdot \sum_{x',y'} I(x+x',y+y')^2}}

匹配结果:取最大值

5. 平方差匹配:cv2.TM_SQDIFF

R(x,y)= \sum _{x',y'} (T(x',y')-I(x+x',y+y'))^2

匹配结果:取最小值

6. 标准平方差匹配:cv2.TM_SQDIFF_NORMED

R(x,y)= \frac{\sum_{x',y'} (T(x',y')-I(x+x',y+y'))^2}{\sqrt{\sum_{x',y'}T(x',y')^2 \cdot \sum_{x',y'} I(x+x',y+y')^2}}

匹配结果:取最小值

 

2 多对象的模板匹配

想要多模板匹配只需使用阈值即可。

例如:

import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread('test24.jpg') img_rgb = img.copy() img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY) template = cv2.imread('test24_7.jpg', 0) w, h = template.shape[::-1] res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED) threshold = 0.8 loc = np.where(res >= threshold) for pt in zip(*loc[::-1]): cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), 2) plt.subplot(131), plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)), plt.title('Original'), plt.axis('off') plt.subplot(132), plt.imshow(template, cmap='gray'), plt.title('Template'), plt.axis('off') plt.subplot(133), plt.imshow(cv2.cvtColor(img_rgb, cv2.COLOR_BGR2RGB)), plt.title('Matching Result'), plt.axis('off') plt.show()

结果如下:

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有