图像匹配导致图像不应该是一个(Python opencv 教程)
Image Matching result in images where it shouldn't be one (Python opencv tutorial)
我正在使用 following python opencv 示例:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('messi5.jpg',0)
img2 = img.copy()
template = cv2.imread('template.jpg',0)
w, h = template.shape[::-1]
# All the 6 methods for comparison in a list
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)
# Apply template Matching
res = cv2.matchTemplate(img,template,method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
# If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
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.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(img,cmap = 'gray')
plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
plt.suptitle(meth)
plt.show()
匹配在一组选定的图像上效果很好,这些图像清楚地包含模板。我的问题是,即使在明显不包含模板的图像中,也会绘制一个矩形。我怎样才能适应源代码,所以它可以处理根本不匹配的图像。
提前致谢
文档中:
It returns a grayscale image, where each pixel denotes how much does the neighbourhood of that pixel match with template
所以为res
设置一个阈值,如果图像中没有相似性,它什么都不做。
res = cv2.matchTemplate(img,template,method)
if res<0.8:
return
...
您的代码始终显示最佳匹配,无论匹配有多好。
您可以检查 max_val
的值(或 min_val
当使用 SQDIFF
时),并且仅当该值超过特定阈值时才显示匹配项。
我正在使用 following python opencv 示例:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('messi5.jpg',0)
img2 = img.copy()
template = cv2.imread('template.jpg',0)
w, h = template.shape[::-1]
# All the 6 methods for comparison in a list
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)
# Apply template Matching
res = cv2.matchTemplate(img,template,method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
# If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
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.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(img,cmap = 'gray')
plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
plt.suptitle(meth)
plt.show()
匹配在一组选定的图像上效果很好,这些图像清楚地包含模板。我的问题是,即使在明显不包含模板的图像中,也会绘制一个矩形。我怎样才能适应源代码,所以它可以处理根本不匹配的图像。
提前致谢
文档中:
It returns a grayscale image, where each pixel denotes how much does the neighbourhood of that pixel match with template
所以为res
设置一个阈值,如果图像中没有相似性,它什么都不做。
res = cv2.matchTemplate(img,template,method)
if res<0.8:
return
...
您的代码始终显示最佳匹配,无论匹配有多好。
您可以检查 max_val
的值(或 min_val
当使用 SQDIFF
时),并且仅当该值超过特定阈值时才显示匹配项。