[英]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
. im
和array
应该具有相同的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_like
和newaxis
如下:
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.