【问题标题】:stack 2 images together in numpy在 numpy 中将 2 个图像堆叠在一起
【发布时间】:2021-08-14 01:22:14
【问题描述】:

从下面的 codw 传递 1 张图像时,我得到的形状是 (1,3,640,640)。

def shape(im1):
    image_src = Image.open(im1)
    print('Loaded Image Info : ',image_src.format, image_src.size, image_src.mode) # size order : width*height

    # Resize to img_size_w, img_size_h
    resized =  image_src.resize((640, 640))   # To be imblemnated : letterbox_image(image_src, (img_size_w, img_size_h))
    print('After resizing :' ,resized.size, resized.mode) # size order : width*height
    #display(resized)

    # Preprocess the image
    img_in = np.transpose(resized, (2, 0, 1)).astype(np.float32)  # HWC -> CHW
    img_in = np.expand_dims(img_in, axis=0) # Add redundant dimension for batch-size (Assumed to be 1, check batch_size = session.get_inputs()[0].shape[0])
    img_in /= 255.0 # Normalize all pixels
    print('Batch-Size, Channel, Height, Width : ',img_in.shape)
    return img_in

在代码中进行哪些更改,以便在传递 2 张图像时将它们堆叠在一起并给出形状为 (2,3,640,640)。

【问题讨论】:

    标签: python image numpy stack


    【解决方案1】:

    试试np.concatenate

    out = np.concatenate([im1, im2], axis=0)
    

    【讨论】:

    • 你能看到用于 1 张图像的代码,它返回的形状为 (1,3,64,64) 如何堆叠 2 张图像。想不通
    • 请改写您的评论或问题。真的很难理解你在说什么。对我来说,您似乎只想在两个图像上运行它然后运行连接,但听起来我需要澄清
    • 希望它能让你清楚,是的,你的理解是正确的,只是想在两个图像上运行它,然后运行连接。如果需要任何澄清,我会尝试
    【解决方案2】:

    您可以将 NumPy 数组附加到列表中,并在最后将列表转换为数组。

    • 用可变参数定义你的方法:

       def shape(*args):
      
    • 迭代参数,并将图像附加到列表中:

       imgs_list = []  # List of images (initialize to an empty list)
      
       for im in args:
           image_src = Image.open(im)
      
           ...
      
           imgs_list.append(img_in)  # Append image to list
      
    • 将列表转换为 NumPy 数组,归一化并返回结果:

       imgs_in = np.array(imgs_list)  # Convert the list to NumPy array    
       imgs_in /= 255.0 # Normalize all pixels
       return imgs_in
      

    shape方法的更新代码:

    def shape(*args):
        imgs_list = []  # List of images (initialize to an empty list)
    
        # Iterate all function arguments
        for im in args:
            image_src = Image.open(im)
            print('Loaded Image Info : ',image_src.format, image_src.size, image_src.mode) # size order : width*height
    
            # Resize to img_size_w, img_size_h
            resized =  image_src.resize((640, 640))   # To be implemented: letterbox_image(image_src, (img_size_w, img_size_h))
            print('After resizing :' ,resized.size, resized.mode) # size order : width*height
            #display(resized)
    
            # Preprocess the image
            img_in = np.transpose(resized, (2, 0, 1)).astype(np.float32)  # HWC -> CHW
    
            imgs_list.append(img_in)  # Append image to list
    
    
        #img_in = np.expand_dims(img_in, axis=0) # Add redundant dimension for batch-size (Assumed to be 1, check batch_size = session.get_inputs()[0].shape[0])
    
        imgs_in = np.array(imgs_list)  # Convert the list to NumPy array    
    
        imgs_in /= 255.0 # Normalize all pixels
        print('Batch-Size, Channel, Height, Width : ',imgs_in.shape)
    
        return imgs_in
    

    用法:

    res = shape('image1.png', 'image2.png', 'image3.png')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-08
      • 2015-06-23
      • 2020-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-12
      相关资源
      最近更新 更多