如何将这个numpy数组的形状更改为(3058,480,640,3)?

问题描述 投票:-1回答:1

我正在尝试从文件夹中读取3058张图像。我希望将我的图片读取为大小为(3158、480、640、3)dtype为uint8的np数组。我将所有图像存储到列表(image_list)。将列表更改为数组后,得到一个数组(3158,)。下面是我的代码

import numpy as np
import cv2 as cv

DIR = mydir
takenFrames = 6
counter = 0

 for filename in glob.glob(DIR + '/*.png'):

                            counter += 1
                            # if counter >= no frames, open image, add img and img_label to list.
                            if (counter >= takenFrames):

                                im = cv.imread(filename) #im.shape is 480, 640
                                image_list.append(im)                   

                                #im = np.resize(im, (-1, 490, 640, 3))


image_list = np.array(image_list, dtype='uint8').reshape(-1, 480, 640, 3) / 255.0

[每当尝试执行此操作时,都会出现以下错误,如下所示

image_list = np.array(image_list,dtype ='uint8')。reshape(-1,480,640,3)/ 255.0追溯(最近一次通话):

文件“”,第1行,在image_list = np.array(image_list,dtype ='uint8')。reshape(-1,480,640,3)/ 255.0

ValueError:设置具有序列的数组元素。

我尝试从单个文件夹访问图像,下面的代码行有效x = np.array([文件名中文件的np.array(Image.open(file))])#x.shape =(55,480,640,3)

每当我访问其他文件夹并读取图像以将所有3058张图像转换为]时,我都试图将x存储在一个空的numpy数组中>

data = np.array([])

            #I tried to append numpy array as
                                if(data.size == 0):
                                    data = im

                                else:   
                                    data = np.append(data, im, axis = 0)

但是那也不起作用

我正在尝试从文件夹中读取3058张图像。我希望将我的图片读取为大小为(3158、480、640、3)dtype为uint8的np数组。我将所有图像存储到列表(image_list)。更改列表后...

image numpy resize reshape numpy-ndarray
1个回答
0
投票

我刚刚想通了。原因是某些图像的形状不同。我通过在添加列表之前更改图像形状来解决它。

© www.soinside.com 2019 - 2024. All rights reserved.