如何在NP 3D数组中重塑帧数据?



>我在 3d 数组中有数据,如下所示:

[[[ 41  57  64 255]
[ 57  76  79 255]
[ 92 113 115 255]
...
[ 70  89  80 255]
[106 127 118 255]
[140 161 152 255]]]

array.shape = (360, 640, 4)

我想把它变成这样:

[[[120 125 110]
[120 125 110]
[120 126 109]
...
[192 207 189]
[194 207 189]
[195 208 190]]]

array.shape = (480, 640, 3)

这两组数据是从两个不同的相机中取出的。 我可以重塑阵列吗?重塑后,框架会正确显示吗?

我尝试使用 np.reshape(( 并转换为列表和弹出,但列表方法非常慢,无法正确显示

NP.重塑ValueError: cannot reshape array of size 921600 into shape (360,640,3)

第一张图像似乎是 360 x 640 rgba 图像,第二张似乎是 480 x 640 rgb 图像。因此,您可以做的是用零填充第一个元素并删除其第 3 维的最后一个元素。

import numpy as np
array1 = np.arange(360*640*4).reshape((360, 640, 4)) # example
array2 = np.arange(480*640*3).reshape((480, 640, 3)) # example
zeros = np.zeros(array2.shape, dtype=np.int32)
zeros[60:-60,:,:] = array1[:,:,:-1]
modified_array1 = zeros
print modified_array1.shape, array2.shape

生产:

(480, 640, 3) (480, 640, 3)

由于两种情况下元素总数的差异,您会收到错误:

360*640*3=691200
480*640* 3=921600

Numpy 重塑仅在前一个数组和重新塑造的数组中的元素数量相同时才有效。

如果需要,您可以尝试用 0 填充。

我拿了这个示例,它对我有用。

import numpy as np
x = np.zeros((360,640,4))
print(x.shape)
print(x)
y = x.reshape(480,640,3)
print(y)
print(y.shape)

最新更新