【问题标题】:4d np.array() acting like a 1d np.array()4d np.array() 就像 1d np.array()
【发布时间】:2020-05-17 00:59:27
【问题描述】:

好的,这是一个很长的问题。问题来了:

问题:所以我将图像(3d 数组)传输到一个 numpy 数组中。我正在尝试获取一个包含我所有图像的 4d 数组,但由于某种原因,它会打印出一个包含 3d np.arrays 的 1d np.array。

建议的解决方案: - 图像有不同的尺寸。情况确实如此,但在调整它们的大小时没有帮助。

首先我要传输每张图片: cv2.imdecode(np.frombuffer(image_bytes, np.uint8), -1) 将我的图像从字节码转换为 3d np 数组。

def transferPictures():
    x_dataset = []
    y_dataset = []
    x_decoded = []
    shoeNames = os.listdir(SHOES_DIRECTORY)
    print(len(shoeNames))
    for shoes in shoeNames[:20]:
        shoeDirectoriesPath = os.path.join(SHOES_DIRECTORY, shoes)
        if(os.path.isdir(shoeDirectoriesPath)):
            eachPicture = os.listdir(shoeDirectoriesPath)
            for pic in eachPicture:
                print(pic)
                picPath = os.path.join(shoeDirectoriesPath, pic)
                f = open(picPath, 'rb')
                image_bytes = f.read()
                #This is to convert the image from byte format to a 3d np array:
                decoded = cv2.imdecode(np.frombuffer(image_bytes, np.uint8), -1) 
                x_dataset.append(decoded) 
                y_dataset.append(pic)
    return [x_dataset, y_dataset]

并将我的 x_dataset 列表(包含我所有的图像)转换为 np.array():

dataset = transferPictures()
x_dataset=np.array(dataset[0])

我得到一个包含 3d np.arrays 的 1d x_dataset np.array:

print("This is the type of the array of images, x_dataset: "+ str(type(x_dataset)))
print("This is the type of each image, x_dataset[0]: "+ str(type(x_dataset[0])))

print("This is the dimension of the array of images, x_dataset: "+ str(np.shape(x_dataset)))
print("This is the dimension of each image, x_dataset[0]: "+ str(np.shape(x_dataset[0])))

打印:

This is the type of the array of images, x_dataset: <class 'numpy.ndarray'>
This is the type of each image, x_dataset[0]: <class 'numpy.ndarray'>
This is the dimension of the array of images, x_dataset: (130,)
This is the dimension of each image, x_dataset[0]: (289, 200, 3)

为什么np.shape(x_dataset) 返回 (130,) 和 np.shape(x_dataset[0]) 返回 (289, 200, 3)?

1) np.shape(x_dataset) 不应该返回 (130, 289, 200, 3) 吗?

2) 如何获得形状为 (130, 289, 200, 3) 的 4d x_dataset?

编辑:

V。 Ayrat 认为这可能是因为每张图像的形状不同。有些图像实际上与其他图像具有不同的尺寸。所以我调整了所有图像的大小以具有相同的形状。但是,问题似乎仍然存在:

查找最小图像的尺寸:

minx = 1000
miny = 1000
minz = 1000


for image in x_dataset:
    if np.shape(image)[0] < minx:
        minx = np.shape(image)[0]
    if np.shape(image)[1] < miny:
        miny = np.shape(image)[1]
    if np.shape(image)[2] < minz:
        minz = np.shape(image)[2]

print(minx)
print(miny)
print(minz)

打印:

288
200
3

调整图片大小:

for i in range(0,len(x_dataset)):
    x_dataset[i] = np.resize(x_dataset[i], (minx,miny,minz))

图像具有所有相同的尺寸,但检查尺寸 x_dataset 仍然返回 (130,)。让它返回一个 4d 数组的任何其他想法?

编辑 2: 100% 确保每张图片的尺寸相同:

minx = 1000
miny = 1000
minz = 1000
maxx = 0
maxy = 0
maxz = 0

for image in x_dataset:
    if np.shape(image)[0] < minx:
        minx = np.shape(image)[0]
    if np.shape(image)[1] < miny:
        miny = np.shape(image)[1]
    if np.shape(image)[2] < minz:
        minz = np.shape(image)[2]
    if np.shape(image)[0] > maxx:
        maxx = np.shape(image)[0]
    if np.shape(image)[1] > maxy:
        maxy = np.shape(image)[1]
    if np.shape(image)[2] > maxz:
        maxz = np.shape(image)[2] 
print()    
print(minx)
print(miny)
print(minz)
print(maxx)
print(maxy)
print(maxz)

打印:

288
200
3

288
200
3

并重新检查类型:

print(type(x_dataset))
print(type(x_dataset[0]))
print(np.shape(x_dataset))

打印:

<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
(130,)

这真是令人困惑......

【问题讨论】:

    标签: python numpy


    【解决方案1】:

    x_dataset 是 python list 对象,它存储 nunpy.array 对象。如果内部 numpy 数组的大小不同 np.shape 仅返回列表的长度。因此,只有当所有图片的大小都相同时,您才能将其设为完整的 numpy 对象,我猜这不是这种情况。

    为了检查您的图像数组x_dataset 是否具有不同大小的图像,请运行:

    minx = 10000
    miny = 10000
    minz = 10000
    maxx = 0
    maxy = 0
    maxz = 0
    
    for image in x_dataset:
        if np.shape(image)[0] < minx:
            minx = np.shape(image)[0]
        if np.shape(image)[1] < miny:
            miny = np.shape(image)[1]
        if np.shape(image)[2] < minz:
            minz = np.shape(image)[2]
        if np.shape(image)[0] > maxx:
            maxx = np.shape(image)[0]
        if np.shape(image)[1] > maxy:
            maxy = np.shape(image)[1]
        if np.shape(image)[2] > maxz:
            maxz = np.shape(image)[2] 
    
    print(minx)
    print(miny)
    print(minz)
    print(maxx)
    print(maxy)
    print(maxz)
    
    

    然后,如果图像的尺寸不同,请使用以下命令调整它们的大小:

    for i in range(0,len(x_dataset)):
        x_dataset[i] = np.resize(x_dataset[i], (minx,miny,minz))
    

    如果问题仍然存在,请将您的数组更改为列表,然后返回数组:

    x_dataset = np.array(x_dataset.tolist())
    x_dataset = np.array(x_dataset)
    

    【讨论】:

    • 感谢您的回答 V. Ayrat。我听从了你的建议,但似乎还有另一个问题。我在问题中添加了我的进度。您还有其他想法吗?
    • 试试x_dataset = np.array(x_dataset)。如果所有尺寸都相同,x_dataset.shape 应该是 (130, 288, 200, 3)。
    • 我只是不确定 x_dataset 的类型是什么,如果它仍然是 list x_dataset = np.array(x_dataset) 应该可以工作。
    • 我刚刚检查了您的建议,但似乎不是问题....检查在 Edit2 部分
    • 所有这些列表和数组都变得很纠结)):你能试试x_dataset = np.array(x_dataset.tolist())吗?
    猜你喜欢
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多