将图像展平并重塑为Numpy阵列以显示

时间:2019-02-20 11:06:31

标签: python arrays numpy image-processing

我有一个RGB图像,我想展平以存储在numpy数组的单个列中,并且在列中包含多个此类图像。问题是,当我存储numpy数组并将其加载回时,数据将正确加载并具有正确的值。但是,当我尝试显示图像时,它会显示非常奇怪的畸形图像。我不明白问题出在哪里。这是用于存储和检索的代码以供参考。

npimg = imread(os.path.join(path, filename))
if cnt == 0:
    np.savetxt(os.path.join(path_save, 'image_1.txt'), npimg[:, :, 0].reshape((dimensions[0], dimensions[1])), fmt='%d')
    np.savetxt(os.path.join(path_save, 'image_2.txt'), npimg[:, :, 1].reshape((dimensions[0], dimensions[1])), fmt='%d')
    np.savetxt(os.path.join(path_save, 'image_3.txt'), npimg[:, :, 2].reshape((dimensions[0], dimensions[1])), fmt='%d')
    np.savetxt(os.path.join(path_save, 'image_flatten.txt'), npimg.flatten(), fmt='%d')
X[:, cnt] = npimg.flatten()

以上代码是在展平前后分别保存图像数据。请注意,单独保存了三维(RGB),以实现正确的可视化。

current_row = X[:, 0]
np.savetxt(os.path.join(path, 'reverse_image_flatten.txt'), current_row, fmt='%d')
current_row = np.reshape(current_row, dimensions)
np.savetxt(os.path.join(path, 'reverse_image_1.txt'), current_row[:, :, 0].reshape((dimensions[0], dimensions[1])), fmt='%d')
np.savetxt(os.path.join(path, 'reverse_image_2.txt'), current_row[:, :, 1].reshape((dimensions[0], dimensions[1])), fmt='%d')
np.savetxt(os.path.join(path, 'reverse_image_3.txt'), current_row[:, :, 2].reshape((dimensions[0], dimensions[1])), fmt='%d')
plt.imshow(current_row)
plt.axis('off')
plt.show()

这是为了检索相同的数据。可以看到存储在image_1、2和3中的数据与reverse_iamge_1、2和3相同。但是当使用imshow显示图像时,该图像不合适。任何帮助将不胜感激。

0 个答案:

没有答案