Python 单个模板与多个源图像的模板匹配
我有一个模板“X”(符号),它是从“Image1”中裁剪出来的。我正在使用OpenCV的matchTemplate()方法将模板“X”与“Image1”匹配,它成功地做到了这一点。但是,我有另一个名为“Image2”的图像,其中包含X符号,但是当我使用模板“X”与“Image2”匹配时,它显示了无效的匹配。任何帮助都将不胜感激Python 单个模板与多个源图像的模板匹配,python,opencv,template-matching,Python,Opencv,Template Matching,我有一个模板“X”(符号),它是从“Image1”中裁剪出来的。我正在使用OpenCV的matchTemplate()方法将模板“X”与“Image1”匹配,它成功地做到了这一点。但是,我有另一个名为“Image2”的图像,其中包含X符号,但是当我使用模板“X”与“Image2”匹配时,它显示了无效的匹配。任何帮助都将不胜感激 def match_template(img_path, img_template_path): if img_path is not None:
def match_template(img_path, img_template_path):
if img_path is not None:
img = cv.imread(img_path, 0)
if img is not None:
template = cv.imread(img_template_path, 0)
temp_h, temp_w, img_h, img_w = None, None, None, None
if len(img.shape) > 2:
print("This image is in color..")
print("Converting it into a grayscale image")
img = cv.cvtColor(src=img, code=cv.COLOR_BGR2GRAY)
else:
temp_w, temp_h = template.shape[::-1]
img_w, img_h = img.shape[::-1]
# ims = cv.resize(img, (960, 540))
if temp_h and img_h is not None:
if temp_w < img_w and temp_h < img_h:
res = cv.matchTemplate(img, template, cv.TM_SQDIFF)
# loc = np.where(res >= 0.9)
min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)
threshold = 0.9
match = False
if np.max(res) > threshold:
match = True
# Take minimum since we are using TM_SQDIFF
if match is True:
top_left = min_loc
bottom_right = (top_left[0] + temp_w, top_left[1] + temp_h)
cv.rectangle(img=img, pt1=top_left, pt2=bottom_right, color=(0, 255, 0), thickness=5)
# plt.subplot(121), plt.imshow(res, cmap='gray')
# plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
plt.imshow(img, cmap='gray')
plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
plt.show()
print("Coordinates of the plotted image are ", top_left, " ", bottom_right)
cv.waitKey(0)
else:
print("Template not matched with the image")
else:
print("Template height and width must be less than origninal image's height and width \n")
else:
print("Image heigth and template height are None")
else:
print("Image not read successfully!!")
else:
print("Image path not provided")
def匹配模板(img\U路径,img\U模板路径):
如果img_路径不是无:
img=cv.imread(img_路径,0)
如果img不是无:
template=cv.imread(img\u template\u路径,0)
temp_h,temp_w,img_h,img_w=无,无,无,无
如果长度(图像形状)>2:
打印(“此图像为彩色…”)
打印(“将其转换为灰度图像”)
img=cv.cvt颜色(src=img,code=cv.COLOR\u bgr2灰色)
其他:
temp_w,temp_h=模板形状[:-1]
img_w,img_h=img.shape[:-1]
#ims=cv.resize(img,(960540))
如果temp_h和img_h不是None:
如果温度w
=0.9)
最小值、最大值、最小位置、最大位置=cv.最小最大位置(res)
阈值=0.9
匹配=错误
如果np.max(res)>阈值:
匹配=真
#因为我们使用的是TM_SQDIFF,所以取最小值
如果匹配为真:
左上=最小位置
右下=(左上[0]+温度w,左上[1]+温度h)
cv.矩形(img=img,pt1=左上角,pt2=右下角,颜色=(0,255,0),厚度=5)
#plt.subplot(121),plt.imshow(res,cmap='gray')
#plt.title('匹配结果'),plt.xticks([]),plt.yticks([])
plt.imshow(img,cmap='gray')
plt.title('检测点'),plt.xticks([]),plt.yticks([])
plt.show()
打印(“打印图像的坐标为”,左上角,”,右下角)
cv.waitKey(0)
其他:
打印(“模板与图像不匹配”)
其他:
打印(“模板高度和宽度必须小于原始图像的高度和宽度\n”)
其他:
打印(“图像高度和模板高度均为零”)
其他:
打印(“图像未成功读取!!”)
其他:
打印(“未提供图像路径”)
首先,仅适用于几乎相同的图像。对包含在新帧中的所需对象进行微小更改可能会导致难以进行良好匹配
在您的例子中,第一个示例工作正常,因为您刚刚从中裁剪了模板。就像这些例子一样,我们也做了同样的事情:和
我不建议单独使用TemplateMatching。功能也是非常有效的功能。您可以获取模板图像的轮廓阵列(例如符号x轮廓),并与其他轮廓进行比较。我的建议是你应该支持你的模板匹配功能
这些帖子中也提到了同样的问题:和你能上传模板和图片吗?两个源图片几乎相似,因为我只上传了一个。你能同时上传两个源图片吗?这将有助于了解两幅图像之间的差异。您也可以尝试设置一个较低的阈值。若您对彩色图像和彩色模板进行模板匹配,您将获得更好的匹配。OpenCV matchTemplate()允许这样做。请参阅文档。您必须使用动态缩放变量模板匹配。看一看,然后