Python OpenCV(3.4.2):错误:(-215:断言失败),模板匹配方法

Python OpenCV(3.4.2):错误:(-215:断言失败),模板匹配方法,python,opencv,template-matching,Python,Opencv,Template Matching,我使用规范化作为模板匹配的预处理方法。 但是,我在运行代码时遇到了一个错误 错误: 错误:OpenCV(3.4.2)/opt/concourse/worker/volumes/live/9523d527-1b9e-48e0-7ed0-a36adde286f0/volume/OpenCV-suite_1535558719691/work/modules/imgproc/src/templmatch.cpp:1102:错误:(-215:断言失败)(深度==0 |深度==5)和类型=|模板类型=|类型

我使用规范化作为模板匹配的预处理方法。 但是,我在运行代码时遇到了一个错误

错误:
错误:OpenCV(3.4.2)/opt/concourse/worker/volumes/live/9523d527-1b9e-48e0-7ed0-a36adde286f0/volume/OpenCV-suite_1535558719691/work/modules/imgproc/src/templmatch.cpp:1102:错误:(-215:断言失败)(深度==0 |深度==5)和类型=|模板类型=|类型()和&&&&&&&&&img.dims()在任何情况下,您都应该验证@HansHirse的答案,如果问题甚至是您的预处理,您可以尝试以下方法:

def Image_Preprocessing (image):
    Gray_image = cv2.cvtColor(image , cv2.COLOR_BGR2GRAY)  # converting the image to grayscale image
    resized_image = cv2.resize(Gray_image, (width, height))  # Resize the image
    Normalized_image = np.array(np.divide(resized_image, np.amax(resized_image)), dtype=np.float64) # Normalizes to float 0 - 1, ensure float
    # Scale the normalized values to integer range
    Normalized_image *= 255 # [0, 255] range
    Normalized_image = np.uint8(Normalized_image)

    return  Normalized_image


这将返回一个uint8图像,如果您的模板也是uint8,则不应出现问题。

在任何情况下,您都应该验证@HansHirse的答案,如果问题甚至是您的预处理,您可以尝试以下方法:

def Image_Preprocessing (image):
    Gray_image = cv2.cvtColor(image , cv2.COLOR_BGR2GRAY)  # converting the image to grayscale image
    resized_image = cv2.resize(Gray_image, (width, height))  # Resize the image
    Normalized_image = np.array(np.divide(resized_image, np.amax(resized_image)), dtype=np.float64) # Normalizes to float 0 - 1, ensure float
    # Scale the normalized values to integer range
    Normalized_image *= 255 # [0, 255] range
    Normalized_image = np.uint8(Normalized_image)

    return  Normalized_image


这将返回一个uint8图像,如果您的模板也是uint8,则不应出现问题。

您的
标准化\u图像
的类型为
float64
。您是否验证了:(a)模板匹配通常适用于
float64
,如果是这样的话(b)您的模板也是
float64
类型?您的
Normalized_Image
float64
类型。您是否验证过,(a)模板匹配通常适用于
float64
,如果是这样,(b)您的模板也是
float64
类型?非常感谢,通过添加这一行Normalized\u image=np解决了问题。uint8(Normalized\u image)非常感谢,通过添加以下行解决了该问题:Normalized_image=np.uint8(Normalized_image)