将不同维度的图像存储在numpy数组中 [英] store images of different dimension in numpy array

查看:254
本文介绍了将不同维度的图像存储在numpy数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个图像,尺寸为(32,43,3)的图像1和尺寸为(67,86,3)的图像2。我如何将其存储在numpy数组中,每当我尝试追加数组

I have two images , image 1 of dimension (32,43,3) and image2 of dimension (67,86,3) . How can i store this in a numpy array , Whenever i try to append the array

image=cv2.imread(image1,0)
image=cv2.resize(image,(32,43))
x_train=np.array(image.flatten())
x_train=x_train.reshape(-1,3,32,43)
X_train =np.append(X_train,x_train) #X_train is my array

image=cv2.imread(image2,0)
image=cv2.resize(image,(67,86))
x_train=np.array(image.flatten())
x_train=x_train.reshape(-1,3,67,86)
X_train =np.append(X_train,x_train) 

Value Error: total size of new array must be unchanged.

我想要X_train的形状(-1,深度,高度,宽度)。这样我就可以将其输入我的神经网络。有什么方法可以将不同维数的图像存储在数组中并馈入神经网络?

i want the X_train in shape (-1,depth,height,width).So that i can feed it into my neural network. Is there any way to store images of different dimension in array and feed into neural network ?

推荐答案

不要使用 np.append 。如果必须加入数组,请从 np.concatenate 开始。

Don't use np.append. If you must join arrays, start with np.concatenate. It'll force you to pay more attention to the compatibility of dimensions.

您不能将2个形状为(32,43,3)(67,86)的数组连接起来,3)制作一些兼容形状的较大阵列。他们共享的唯一维度是最后一个维度。

You can't join 2 arrays with shapes (32,43,3) (67,86,3) to make a larger array of some compatible shape. The only dimension they share is the last.

这些重塑也没有任何意义:(-1,3,32,43),(-1,3, 67,86)。

These reshapes don't make sense either: (-1,3,32,43), (-1,3,67,86).

它有效,但也弄乱了图像。您不只是添加第4维。看来您也想进行一些轴交换或转置。练习一些小阵列,以便您了解发生了什么,例如(2,4,3)。

It works, but it also messes up the 'image'. You aren't just adding a 4th dimension. It looks like you want to do some axis swapping or transpose as well. Practice with some small arrays so you can see what's happening, e.g. (2,4,3).

您期望 Xtrain 的最终形状是什么?

What final shape do you expect for Xtrain?

您可以将这两个图像放入对象dtype数组中,该数组与列表 [image1,image2] 基本上相同。但是我怀疑您的神经网络是否可以做到这一点。

You can put these two images in a object dtype array, which is basically the same as the list [image1, image2]. But I doubt if your neuralnet can do anything practical with that.

如果您重塑了(32,43,3)数组(16,86,3)可以将其与轴= 0上的(67,86,3)串联以生成(83,86,3)数组。如果您首先需要 3 ,我会使用 np.transpose(...,(2,0,1))

If you reshaped the (32,43,3) array to (16,86,3) you could concatenate that with (67,86,3) on axis=0 to produce a (83,86,3) array. If you needed the 3 to be first, I'd use np.transpose(..., (2,0,1)).

相反地将(67,86,3)重塑为(2 * 67,43,3)。

Conversely reshape (67,86,3) to (2*67,43,3).

将(32,43,3)传递给(32,86,3)是另一种选择。

Passing the (32,43,3) to (32,86,3) is another option.

将它们加入新的第4维,要求行的数量和列的数量都匹配。

Joining them on a new 4th dimension, requires that the number of 'rows' match as well as the number of 'columns'.

这篇关于将不同维度的图像存储在numpy数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆