我试图对两个对象使用相同的函数。
我可以让事情工作与一个单一的对象,但当我试图加载两个它不工作。我已经抛弃了print(self.needle_img)
来检查返回的内容它显示的是none
并给我错误,AttributeError: 'NoneType' object has no attribute 'shape'
def __init__(self, needle_img_path, method=cv.TM_CCOEFF_NORMED):
# Set the method we're using when we load the image
self.method = method
# load the image we're trying to match
self.needle_img = cv.imread(needle_img_path, cv.IMREAD_UNCHANGED)
print(self.needle_img)
# Save the dimensions of the needle image
self.needle_w = self.needle_img.shape[1]
self.needle_h = self.needle_img.shape[0]
这就是我试图传递多个对象的方式:
# set the window to capture object
wincap = WindowCapture('Application')
# empty array
avoid = []
#fill the empty array with images
avoid_images = glob.glob(r"C:Usersavoidavoid*.jpg")
print(avoid_images)
# set the objects I want to find
search = Search('avoid_images')
print(avoid_images)
正确返回我所期望的图像。
我不确定,但我认为我需要循环通过多个图像,然后存储结果略有不同,而不是使用:
self.needle_w = self.needle_img.shape[1]
self.needle_h = self.needle_img.shape[0]
因为它存储的是一个图像的尺寸?
我在谷歌上搜索了很多,发现NoneType
错误通常是cv2的问题。imread或无效的文件路径,我确认了print(avoid_images)
的文件路径是正确的,所以问题必须是我认为我如何试图将这些传递到函数中?
用户回复:无法一次向OpenCv传递多个图像
@Jon的解决方案是将字符串直接传递给imread
调用,并使用.append
将一个图像附加到另一个图像
# load the needle image
if type(needle_img_path) is str:
self.needle_imgs.append(cv.imread(needle_img_path, cv.IMREAD_UNCHANGED))
elif type(needle_img_path) is list or tuple:
for img in needle_img_path:
self.needle_imgs.append(cv.imread(img, cv.IMREAD_UNCHANGED))