使用w时出错,h = template.shape[::-1]

时间:2021-02-14 22:51:50

I am getting an error:

我有一个错误:

w, h = template.shape[::-1]
AttributeError: 'NoneType' object has no attribute 'shape'

My code:

我的代码:

import cv2
import numpy as np

img_rgb = cv2.imread('opencv-template-matching-python-tutorial.jpg')

img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)

template = cv2.imread('opencv-template-for-matching.jpg',0)

w, h = template.shape[::-1]
res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)

threshold = 0.8

loc = np.where( res >= threshold)

for pt in zip(*loc[::-1]):

    cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,255,255), 2)

    cv2.imshow('Detected',img_rgb)

How can I fix this problem?

我该如何解决这个问题?

1 个解决方案

#1


2  

I'm not too familiar with opencv, but that error means that cv2.imread('opencv-template-for-matching.jpg',0) fails to read that file and thus returns None.

我不太熟悉opencv,但这个错误意味着cv2.imread('opencv-template-for-match .jpg',0)无法读取该文件,因此返回None。

Make sure that this file exists and in the supported format. From imread's documentation:

确保该文件存在并以支持的格式存在。从imread的文档:

The function imread loads an image from the specified file and returns it. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL). Currently, the following file formats are supported: Windows bitmaps - *.bmp, *.dib (always supported) JPEG files - *.jpeg, *.jpg, *.jpe (see the Notes section) JPEG 2000 files - *.jp2 (see the Notes section) Portable Network Graphics - *.png (see the Notes section) Portable image format - *.pbm, *.pgm, *.ppm (always supported) Sun rasters - *.sr, *.ras (always supported) TIFF files - *.tiff, *.tif (see the Notes section)

函数imread从指定的文件加载一个图像并返回它。如果图像不能被读取(由于缺少文件、不正确的权限、不支持或无效的格式),函数将返回一个空矩阵(Mat::data==NULL)。目前,支持以下文件格式:Windows位图- *。bmp,*。dib(一直支持)JPEG文件- *. JPEG, *.jpg, *.jpe(见注释部分)JPEG 2000文件- *.jp2(见注释部分)便携式网络图形- *。png(见注释部分)便携式图像格式*。pbm,*。的pgm,*。ppm(一直支持)太阳光栅- *。sr,*。ras(始终支持)TIFF文件- *。tiff,*。tif(见注释部分)

#1


2  

I'm not too familiar with opencv, but that error means that cv2.imread('opencv-template-for-matching.jpg',0) fails to read that file and thus returns None.

我不太熟悉opencv,但这个错误意味着cv2.imread('opencv-template-for-match .jpg',0)无法读取该文件,因此返回None。

Make sure that this file exists and in the supported format. From imread's documentation:

确保该文件存在并以支持的格式存在。从imread的文档:

The function imread loads an image from the specified file and returns it. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL). Currently, the following file formats are supported: Windows bitmaps - *.bmp, *.dib (always supported) JPEG files - *.jpeg, *.jpg, *.jpe (see the Notes section) JPEG 2000 files - *.jp2 (see the Notes section) Portable Network Graphics - *.png (see the Notes section) Portable image format - *.pbm, *.pgm, *.ppm (always supported) Sun rasters - *.sr, *.ras (always supported) TIFF files - *.tiff, *.tif (see the Notes section)

函数imread从指定的文件加载一个图像并返回它。如果图像不能被读取(由于缺少文件、不正确的权限、不支持或无效的格式),函数将返回一个空矩阵(Mat::data==NULL)。目前,支持以下文件格式:Windows位图- *。bmp,*。dib(一直支持)JPEG文件- *. JPEG, *.jpg, *.jpe(见注释部分)JPEG 2000文件- *.jp2(见注释部分)便携式网络图形- *。png(见注释部分)便携式图像格式*。pbm,*。的pgm,*。ppm(一直支持)太阳光栅- *。sr,*。ras(始终支持)TIFF文件- *。tiff,*。tif(见注释部分)