具有调整大小的图像的匹配模板不起作用:(-215:声明失败)_img.size()。height <= _templ.size()。height

问题描述 投票:0回答:1

我在matchTemplate调用之前调整图片大小以确保两个图像的尺寸相同。

# -*- coding: utf-8 -*-
import cv2 as cv
import numpy as np
import time

img_rgb = cv.imread('t2.png')
img_gray = cv.cvtColor(img_rgb, cv.COLOR_BGR2GRAY)
template = cv.imread('template1.png', 0)

# Make sure that we use the exact same size in the comparison
if img_gray.shape != template.shape:
    img_gray = cv.resize(img_gray, template.shape, interpolation=cv.INTER_AREA)


res = cv.matchTemplate(img_gray, template, cv.TM_CCOEFF_NORMED)
print res

但是我收到此错误:

res = cv.matchTemplate(img_gray, template, cv.TM_CCOEFF_NORMED)
cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\imgproc\src\templmatch.cpp:1112: error: (-215:Assertion failed) _img.size().height <= _templ.size().height && _img.size().width <= _templ.size().width in function 'cv::matchTemplate'
python opencv matchtemplate
1个回答
0
投票

[当ypu调整图像大小时,您正在使用template.shape,但需要使用

template.shape[::-1]
© www.soinside.com 2019 - 2024. All rights reserved.