【问题标题】:Assigning an image to a numpy array makes colors of the image change将图像分配给 numpy 数组会使图像的颜色发生变化
【发布时间】:2017-07-07 13:47:27
【问题描述】:

我加载一个图像并将其添加到一个空的 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()

我希望这两个图像在显示时看起来相同。当我将它分配给一个数组时,图像似乎发生了变化:

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

【问题讨论】:

  • 你不应该使用np.uint8array = np.zeros((1, 299, 299,3), dtype = np.uint8)吗?
  • 你是对的,使用 uint 有帮助!

标签: python arrays image numpy


【解决方案1】:

imarray 应该具有相同的dtype。您可以为零数组显式定义适当的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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-14
    • 2022-08-21
    • 1970-01-01
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    • 2012-03-08
    相关资源
    最近更新 更多