图像整形问题:三维图像

时间:2019-05-01 17:30:14

标签: image neural-network reshape shapes cv2

我的图像的长和宽为7 x 5,但是图像的形状显然是(7,5,3)。

当我将图像尺寸调整为28 x 28时,形状变为(28,28,3)。 我需要的形状是(1、28、28),但是我不知道该怎么做。

image = cv2.imread(importedImage)
image = cv2.resize(image, (28, 28))
image = img_to_array(image)
image = np.array(image, dtype="float32")/ 255.0

print(image)


q = loaded_model.predict(np.array([image]))[0]

重塑似乎没有帮助。

图像的打印版本为:

[[[1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]
  ...
  [1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]]

 [[1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]
  ...
  [1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]]

 [[1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]
  ...
  [1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]]

 ...

 [[1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]
  ...
  [1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]]

 [[1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]
  ...
  [1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]]

 [[1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]
  ...
  [1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]]]
  ...
  [1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]]

 [[1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]
  ...
  [1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]]]

错误是:ValueError:检查输入时出错:预期flatten_1_input具有3维,但数组的形状为(1、28、28、3)

1 个答案:

答案 0 :(得分:0)

之所以要增加尺寸(3),是因为OpenCV(以及大多数其他处理图像的软件包)为Red,Green和Blue保留了单独的颜色通道。 (OpenCV命令它们为蓝色,绿色,红色。)某些图像具有“ alpha”通道,在这种情况下,您会看到(高度,宽度,4)。

您可以通过将三个颜色通道组合为一个灰度通道来获得(高度,宽度,1)。

pd.concat([df.isnull().any() , df.apply(lambda x: x.count() != x.nunique())], 1).replace({True: "Y", False: "N"})

应该使您接近所需的东西。