Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法同时将多个图像传递到OpenCv_Python_Opencv_Image Processing - Fatal编程技术网

Python 无法同时将多个图像传递到OpenCv

Python 无法同时将多个图像传递到OpenCv,python,opencv,image-processing,Python,Opencv,Image Processing,我已经沉迷于阅读好几天了,但我只学习了几周python和openCv,对此我完全不知所措 我有一个类函数,它可以在“干草堆”图像上的“针”图像周围绘制矩形。haystack是我实时捕获的应用程序窗口 class Search: def __init__(self, needle_img_path, method=cv.TM_CCOEFF_NORMED): # set the method used to load the image self.method = me

我已经沉迷于阅读好几天了,但我只学习了几周python和openCv,对此我完全不知所措

我有一个类函数,它可以在“干草堆”图像上的“针”图像周围绘制矩形。haystack是我实时捕获的应用程序窗口

class Search:

def __init__(self, needle_img_path, method=cv.TM_CCOEFF_NORMED):
    
    # set the method used to load the image 
    self.method = method

    # load the needle image
    self.needle_img = cv.imread(needle_img_path, cv.IMREAD_UNCHANGED)

    # Save the dimensions of the needle image
    self.needle_w = self.needle_img.shape[1]
    self.needle_h = self.needle_img.shape[0]
这就是我将单个图像传递到上述函数的方式

# the window to capture 
wincap = WindowCapture('X')

# set the needle image
images = x.jpg

# perform the search 
search = Search(images)
当我尝试直接在
images=(“x.jpg”、“y.jpg”)

我得到一个错误:

   self.needle_img = cv.imread(needle_img_path, cv.IMREAD_UNCHANGED)
TypeError: Can't convert object of type 'tuple' to 'str' for 'filename'
    self.needle_img = cv.imread(needle_img_path, cv.IMREAD_UNCHANGED)
TypeError: Can't convert object of type 'list' to 'str' for 'filename'
当我尝试将图像存储在数组中时,
images=[cv.imread(file)for file in glob.glob('localpath')]

我得到一个错误:

   self.needle_img = cv.imread(needle_img_path, cv.IMREAD_UNCHANGED)
TypeError: Can't convert object of type 'tuple' to 'str' for 'filename'
    self.needle_img = cv.imread(needle_img_path, cv.IMREAD_UNCHANGED)
TypeError: Can't convert object of type 'list' to 'str' for 'filename'

当我将
print(images)
放在一个成功加载的图像下方
images=x.jpg
时,它返回
x.jpg
,因此我认为它需要的是一个字符串而不是数组,但我不确定如何执行该操作。

如果您的参数作为元组传递,则需要迭代图像路径列表。如果它是作为字符串传递的,那么您需要将其直接传递给
imread
调用

考虑进行以下修改:

import cv2 as cv

class Search:
    def __init__(self, needle_img_path, method=cv.TM_CCOEFF_NORMED):
        # set the method used to load the image
        self.method = method
        self.needle_imgs = []

        # 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))

    def do_something_with_images(self):
        for img in self.needle_imgs:
            print(img.shape)

# set the needle image
images = ("C:\\Test\\x.jpg", "C:\\Test\\y.jpg")

# perform the search
search = Search(images)

# Do something to each individual image
search.do_something_with_images()

谢谢@Jon,上面的代码也有同样的错误@L23P做了一些编辑,并在Python 3.7.6Amazing上进行了测试,删除了错误!但是,现在看起来它只使用了图像y,而不是同时使用x和y。它找到的是y,但不是x。有没有可能我们只是用第二个图像覆盖第一个图像@JonI已经交换了它们,现在它只找到x,所以它只存储/搜索最后加载的图像,而不是同时存储/搜索这两个图像。@L32P已编辑。您可以存储图像,然后通过遍历列表对每个图像进行处理。当然,如果您这样做是为了进行实时采集,那么在处理完图像后,您需要将其从列表中删除。