【问题标题】:How can I change the shape of this numpy array to (3058, 480, 640, 3)?如何将此 numpy 数组的形状更改为 (3058, 480, 640, 3)?
【发布时间】:2020-04-04 09:52:08
【问题描述】:

我正在尝试从一个文件夹中读取 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 Traceback(最近一次调用最后一次):

文件“”,第 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)) for file in filename]) #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)

但这也不起作用

【问题讨论】:

  • 这个一维数组的dtype 是什么?可能是object。组合形状不同的数组会产生这种数组。对于图像,问题可能是大小,或者是 3 通道和 1 通道图像的混合。
  • 是的,一维数组的数据类型是对象。那么您对如何解决此问题有任何想法吗?
  • np.append 不是列表附加的有效替代品。
  • 我刚刚想通了。一些图像具有不同的形状,这就是原因。我通过在添加列表之前更改图像形状来解决它。

标签: image numpy resize reshape numpy-ndarray


【解决方案1】:

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

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)

#some images are of size (480, 640, 3) whereas some are (490, 640, 3). I resized all images to (480, 640, 3)          

                                im = np.resize(im, (480, 640, 3))

                                image_list.append(im)
                                labels.append(label)

#converting image_list to numpy array changes my list to (3058, 480, 640, 3)

import numpy as np
image_list = np.asarray(image_list)                                                                             

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 2012-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多