python - matchTemplate 与调整大小的图像不起作用:(-215:断言失败)_img.size().height <= _templ.size().height
问题描述
我在 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'
解决方案
当 ypu 调整图像大小时,您正在使用 template.shape 但您需要使用
template.shape[::-1]
推荐阅读
- javascript - Javascript 无法读取 null
- python-3.x - 有没有一种优雅的方式来使用泛型方法返回子页面?
- javascript - 从函数范围内获取数组数据
- java - 返回 java 对象时出现 500 错误,但返回 String 时却没有
- apache - Magento 2 CentOS 7 nginx -> varnish -> apache -> php-fpm 重定向循环
- javascript - JS:TypeError:在对象中找不到功能键
- python - 拆分 Pandas 列,然后将元素与另一个拆分列的匹配元素连接
- c++ - compiling boost.spirit.karma example, customize_embedded_container.cpp fails
- bash - 根据while循环中的输出函数更改计数器 - 在bash中
- c - Does a C function without any argument and return value require a stack to execute?