【问题标题】:Selecting images inside of images选择图像内的图像
【发布时间】:2020-06-16 15:52:13
【问题描述】:

我有 this image and 我想选择其中的所有地图,包括使用 python 的名称。

我已经用 OpenCv 试过了

from cv2 import cv2
import numpy as np
import os

for image in os.listdir('assets'):
    print(image)
    img_rgb = cv2.imread(image)
    template = cv2.imread('assets/template/template.png')
    w, h = template.shape[:-1]
    res = cv2.matchTemplate(img_rgb, template, cv2.TM_CCOEFF_NORMED)
    threshold = .8
    loc = np.where(res >= threshold)
    for pt in zip(*loc[::-1]):  # Switch columns and rows
        cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), 2)

    cv2.imwrite('result.png', img_rgb)
    break

但我总是得到这个错误:

OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\templmatch.cpp:1104: 错误: (-215:Assertion failed) (depth == CV_8U || depth = = CV_32F) && type == _templ.type() && _img.dims()

我该怎么做?请帮忙

【问题讨论】:

  • 你能显示完整的回溯吗?我不确定 for 循环中的哪个 cv2 调用会引发此错误
  • 你让它工作了吗?什么是错误

标签: python opencv


【解决方案1】:

cv2.matchTemplate 的参数之一是NoneType 时将返回此错误。换句话说,img_rgb 什么都不是,很可能是因为在该位置没有找到图像。

我是这样重现你的错误的:

from cv2 import cv2
import numpy as np

img_rgb = cv2.imread('fjdlfkjdlkfjdlfksdad')  # or None
template = np.random.randint(0, 256, (100, 100, 3))
res = cv2.matchTemplate(img_rgb, template, cv2.TM_CCOEFF_NORMED)

cv2.error: OpenCV(4.2.0) C:\projects\opencv- python\opencv\modules\imgproc\src\templmatch.cpp:1104:错误:(-215:断言失败) (深度 == CV_8U || 深度 == CV_32F)&& 类型 == _templ.type() && _img.dims()

【讨论】:

  • 不,确实找到了图片
  • 我认为您忘记将'assets/' 添加到文件夹'assets' 中的文件以读取它们。 img_rgb = cv2.imread('assets/' + image)
猜你喜欢
  • 1970-01-01
  • 2016-11-09
  • 1970-01-01
  • 1970-01-01
  • 2023-01-04
  • 2010-10-06
  • 2014-06-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多