내가 오류가 점점 오전 :오류 시간, 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)
가 어떻게이 문제를 해결할 수 있습니까?
내가 cv2.imread''에서 (또는 적어도 나쁜 API 디자인)이 버그 고려할 것; 요청 된 파일을 열 수 없으면'None'을 반환하지 않고 예외를 발생시켜야합니다. 즉 반환 값을 사용하려고 시도하기 전에 반환 값을 확인하여 작업이 성공했는지 확인하는 것은 사용자의 책임입니다. – chepner