如何解决不断收到错误 w, h = small_image.shape[:-1] AttributeError: 'NoneType' 对象没有属性 'shape'
我的知识很少,正在尝试学习 Python,并且一直做得很好。
直到!
当试图匹配图像时,我得到 w,h = small_image.shape[:2]
AttributeError: 'nonetype' 对象没有属性 'shape'
我的图片在正确的路径中,但一直收到上面的错误,已经调试了很多次并在互联网上搜索了答案。
我找到的唯一答案是检查我拥有的路径,它们似乎是正确的。
请原谅我所知甚少。我只是想学习任何指针,将不胜感激。
def __init__(self,handle=None):
self._handle = handle
self._item_memory = {}
self._item_area = (525,227,80,275)
def match_image(self,largeImg,smallImg,threshold=0.1,debug=False):
method = cv2.TM_SQDIFF_norMED
# Read the images from the file
small_image = cv2.imread(smallImg)
large_image = cv2.imread(largeImg)
w,h = small_image.shape[:2]
result = cv2.matchTemplate(small_image,large_image,method)
# We want the minimum squared difference
mn,_,mnLoc,_ = cv2.minMaxLoc(result)
if (mn >= threshold):
return False
# Extract the coordinates of our best match
x,y = mnLoc
if debug:
# Draw the rectangle:
# Get the size of the template. This is the same size as the match.
trows,tcols = small_image.shape[:2]
# Draw the rectangle on large_image
cv2.rectangle(large_image,(x,y),(x+tcols,y+trows),(0,255),2)
# display the original image with the rectangle around the match.
cv2.imshow('output',large_image)
# The image is only displayed if we call this
cv2.waitKey(0)
# Return coordinates to center of match
return (x + (w * 0.5),y + (h * 0.5))`
def find_item(self,item_name,threshold=0.15,max_tries=1,recapture=True):
self.set_active()
tries = 0
res = False
while not res and tries < max_tries:
tries += 1
if tries > 1:
# Wait 1 second before re-trying
self.wait(0.5)
recapture = True
if recapture:
self.mouse_out_of_area(self._item_area)
self.screenshot('item_area.png',region=self._item_area)
res = self.match_image(
'item_area.png',('items/' + item_name + '.png'),threshold)
if res is not False:
x,y = res
offset_x,offset_y = self._item_area[:2]
item_pos = (offset_x + x,offset_y + y)
# Remember location
self._item_memory[item_name] = item_pos
return item_pos
else:
return False
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。