微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法一次将多个图像传递给 OpenCv

如何解决无法一次将多个图像传递给 OpenCv

我已经相当痴迷地阅读了好几天,但我只有几周的时间来学习 python 和 openCv,我对此完全不知所措。

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

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'

当我尝试将图像存储在数组中时 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 '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()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?