我试图枚举一个2D NumPy数组的形状(512,512)与像素值,以输出[y_index, x_index, pixel_value]
。输出数组应具有以下形状(262144,3):262144个像素来自512 x512矩阵,3个像素用于列pixel value
、y coordinate
和x coordinate
。
我使用ndenumerate
,但如何在数组中存储值?我最后一行的尝试是不正确的:
img = as_np_array[:, :, 0]
img = img.reshape(512, 512)
coordinates = np.empty([262,144, 3])
for index, x in np.ndenumerate(img):
coordinates = np.append(coordinates, [[x], index[0], index[1]])
Intel Core i7 2.7GHz(4核心)需要7-10分钟。有没有更有效的方法?
2条答案
按热度按时间fgw7neuy1#
你可以使用
numpy.indices
来实现。你最终想要的是image_data
,y
,x
索引和相应的像素(px
)。在image_data
中有三列。详细示例:
yc0p9oo02#
对我有效的解决方案是这样的: