简体   繁体   English

将 3D 数组调整为 2D

[英]Resize 3D array to 2D

I have some code that looks like this我有一些看起来像这样的代码

    number_of_pairs = int(len(path_list) / 2)
    pairs_of_images = [np.zeros(
        (number_of_pairs, self.image_height, self.image_height, 1)) for i in range(2)]
    labels = np.zeros((number_of_pairs, 1))
    size = 105,105
    for pair in range(number_of_pairs):
        image = Image.open(path_list[pair * 2])
        image = image.resize((105,105))
        image = np.asarray(image).astype(np.float64)
        print("before resize is{}".format(image))

        pairs_of_images[0][pair, :, :, 0] = image

However, I'm getting an error where但是,我收到一个错误,其中

pairs_of_images[1][pair, :, :, 0] = image pair_of_images[1][pair, :, :, 0] = 图像
ValueError: could not broadcast input array from shape (105,105,4) into shape (105,105) ValueError:无法将输入数组从形状 (105,105,4) 广播到形状 (105,105)

Is there a way to get rid of the 3rd dimension of the array?有没有办法摆脱数组的第三维?

Replace代替

image = Image.open(path_list[pair * 2])

with

image = Image.open(path_list[pair * 2]).convert('L')

'L' mode turns an image into a single-channel image. “L”模式将图像转换为单通道图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM