无法一次将多个图像传递给OpenCv [英] Unable to pass multiple images to OpenCv at once

查看:36
本文介绍了无法一次将多个图像传递给OpenCv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几天来一直在沉迷于阅读,但是我只花了几周的时间就开始学习python和openCv了,对此我一无所获.

I've been reading fairly obsessively for days now, but I'm only a few weeks into learning python and openCv and I'm at a total loss with this.

我有一个类函数,可在针"周围绘制矩形.在干草堆"上找到的图像图像.大海捞针是我实时捕获的应用程序窗口.

I have a class function that draws rectangles around a "needle" image found on a "haystack" image. The haystack is an application window I'm capturing in real-time.

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]

这就是我将单个图像传递给上述功能的方式.

This is how I'm passing a single image into the above function.

# 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 glob.glob('localpath')]中的文件

And when I try storing the the images in an array 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 ,所以我认为它期望一个字符串而不是一个数组,但是我不确定该怎么做.

When I place print(images) below a single successfully loaded image images = x.jpg it returns x.jpg so I think it's expecting a string and not an array but I'm not sure how to do that.

推荐答案

如果您的参数作为元组传递,则需要遍历图像路径列表.如果将其作为字符串传递,则需要将其直接传递给 imread 调用.

If your argument is passed as a tuple, you'll need to iterate over the list of image paths. If it's passed as a string, you'll want to pass that directly to the imread call.

请考虑进行以下修改:

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

这篇关于无法一次将多个图像传递给OpenCv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆