OpenCV(3.4.2):错误:(-215:声明失败)使用模板匹配方法

时间:2019-11-25 11:58:25

标签: 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)&& type == _templ.type()&& _img.dims()<= 2在函数'matchTemplate'中

这是我的预处理方法:

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 
    mean, stdDev = cv2.meanStdDev(resized_image)  #Get Mean and Standard-deviation
    Normalized_image = (resized_image-mean)/stdDev  #Normalize the image  
    # Scale the normalized values to integer range
    Normalized_image -= Normalized_image.min() 
    Normalized_image /= Normalized_image.max()
    Normalized_image *= 255 # [0, 255] range

    return  Normalized_image

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

无论如何,您应该验证@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,则应该没有问题。