为了账号安全,请及时绑定邮箱和手机立即绑定

在将其输出(图像)保存在多个变量中的 python opencv

在将其输出(图像)保存在多个变量中的 python opencv

守着一只汪 2021-09-25 14:38:36
我有一个功能可以调整给定图像的大小。但是,我想要做的是,在其中使用 for 循环,这样我就可以根据需要调整尽可能多的图像大小并将它们保存在不同的变量中。这里我有来自opencv的图像读取代码。image1 = cv2.imread(test1.jpg)image2 = cv2.imread(test2.jpg)下面是图像大小调整功能。目前,它采用 image1,调整大小,然后将其保存到 img1。如何使用 for 循环,以便我可以传递 image1、image2、image3 等,并将其分别保存到 img1、img2、img3.....def image_resize(image1, width=None, height=None,inter=cv2.INTER_AREA):    # initialize the dimensions of the image to be resized and    # grab the image size    dim = None    (h, w) = image1.shape[:2]    # if both the width and height are None, then return the    # original image    width is None and height is None:        return image1    # check to see if the width is None    if width is None:        # calculate the ratio of the height and construct the dimensions        r = height / float(h)        dim = (int(w * r), height)    # otherwise, the height is None    else:        # calculate the ratio of the width and construct the dimensions        r = width / float(w)        dim = (width, int(h * r))    # resize the image    resized = cv2.resize(image1, dim, interpolation = inter)    # return the resized image    return resizedimg1 = image_resize(image1, height = 500)问这个问题可能很愚蠢。但我是这个编程区的新手。因此,任何帮助将不胜感激。
查看完整描述

2 回答

?
一只名叫tom的猫

TA贡献1906条经验 获得超2个赞

创建字典并定义图像详细信息,将其传递给函数image_resize并使用循环将有所帮助。

这是示例代码


def image_resize(image1, width = None, height=None,inter=cv2.INTER_AREA):


# initialize the dimensions of the image to be resized and

        # grab the image size

        dim = None

        (h, w) = image1.shape[:2]


        # if both the width and height are None, then return the

        # original image

        if width is None and height is None:

            return image1


        # check to see if the width is None

        if width is None:

            # calculate the ratio of the height and construct the dimensions

            r = height / float(h)

            dim = (int(w * r), height)


        # otherwise, the height is None

        else:

            # calculate the ratio of the width and construct the dimensions

            r = width / float(w)

            dim = (width, int(h * r))


        # resize the image

        resized = cv2.resize(image1, dim, interpolation = inter)


        # return the resized image

        return resized


resized_images = []

#here image_name is name of the images,

# in image_width/image_height add width/height according to image 


obj = { image: [image_name],

       width :[image_width],

       height:[image_heights],

       inter =cv2.INTER_AREA

       }


def fun(obj):

    for i in obj:

        for j in i:

            img = image_resize(i['image'][j],i['width'][j],i['height'][j],i['inter'])

            resized_image.append(img)


fun(obj)


查看完整回答
反对 回复 2021-09-25
?
呼唤远方

TA贡献1856条经验 获得超11个赞

这是使用 for 循环的一种方法,假设您有 10 张图像作为示例。


说明:创建一个空列表resized_images来存储调整大小的图像。让我们假设你有一个名为10倍的图像test1.jpg,test2.jpg,test3.jpg等等。您使用索引i迭代 10 个值,然后使用imreadfor 循环读取图像并调用该函数。该函数的返回值现在存储在一个列表中resized_images,您可以稍后访问该列表。'test%s.jpg' %i是动态读取具有变量名称的不同图像的方法。


现在一旦所有图像都调整了大小,您可以访问第一个调整大小的图像resized_images[0],第二个调整大小的图像resized_images[1]等等。python 中的索引从 0 开始,因此使用 index 访问第一个图像[0]。


def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):

    dim = None

    (h, w) = image.shape[:2]


    if width is None and height is None:

        return image


    if width is None:

        r = height / float(h)

        dim = (int(w * r), height)

    else:

        r = width / float(w)

        dim = (width, int(h * r))


    resized = cv2.resize(image, dim, interpolation = inter)

    return resized


resized_images = []

number_of_images = 10


for i in range(1, number_of_images+1):

    image = cv2.imread('test%s.jpg' %i)

    img = image_resize(image, height = 500)

    resized_images.append(img)


查看完整回答
反对 回复 2021-09-25
  • 2 回答
  • 0 关注
  • 241 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信