简体   繁体   English

将图像分配给numpy数组会使图像的颜色发生变化

[英]Assigning an image to a numpy array makes colors of the image change

I load an image and add it to an empty numpy array: 我加载图像并将其添加到空的numpy数组中:

plt.imshow(im)
plt.show()
im = imread('/path_to_image',mode = 'RGB')
array = np.zeros((1, 299, 299,3), dtype = np.int8)
array[0][:,:,:3] = im
print(array[0].shape)
print(im.shape)
plt.imshow(array[0])
plt.show()

这是我得到的输出

I expect the two images to look the same when displayed. 我希望两个图像在显示时看起来相同。 The image seems to change when I assign it to an array with: 当我使用以下命令将图像分配给数组时,图像似乎发生了变化:

array = np.zeros((1, 299, 299,3), dtype = np.int8)
array[0][:,:,:3] = im

im and array should have the same dtype . imarray应该具有相同的dtype You can explicitly define the appropriate dtype for the array of zeros (as suggested by @Divakar) or alternatively you can use NumPy's zeros_like and newaxis as follows: 你可以明确地定义适当的dtype为零的数组(由@Divakar所建议的),或者你可以用与NumPy的zeros_likenewaxis如下:

import numpy as np
import matplotlib.pyplot as plt
from skimage import io

im = io.imread('https://i.stack.imgur.com/crpfS.png')

array = np.zeros_like(im[np.newaxis, :])
array[0] = im

fig, (ax0, ax1) = plt.subplots(1, 2)
ax0.imshow(im)
ax0.set_title('im')
ax1.imshow(array[0])
ax1.set_title('array[0]')
plt.show()

我和数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM