2016-07-09 4 views
-1

내가 오류가 점점 오전 :오류 시간, w를 사용하는 동안 = template.shape [:: - 1]

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

내 코드 :

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) 

가 어떻게이 문제를 해결할 수 있습니까?

답변

2

나는 opencv에 익숙하지 않지만이 오류는 cv2.imread('opencv-template-for-matching.jpg',0)이 해당 파일을 읽지 못해 None을 반환한다는 것을 의미합니다.

이 파일이 있고 지원되는 형식인지 확인하십시오. documentationimread에서 :

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)

+0

내가 cv2.imread''에서 (또는 적어도 나쁜 API 디자인)이 버그 고려할 것; 요청 된 파일을 열 수 없으면'None'을 반환하지 않고 예외를 발생시켜야합니다. 즉 반환 값을 사용하려고 시도하기 전에 반환 값을 확인하여 작업이 성공했는지 확인하는 것은 사용자의 책임입니다. – chepner

관련 문제