为什么从 np.ones 和 np.zeros 创建的图像都显示黑色空白图像,而将两者结合起来会产生预期的黑白混合图像?

Why both image created from np.ones and np.zeros shows black blank image while combining both gives black and white mixed image as expected?

我的意思是:

  1. 我希望在使用 np.ones 创建图像时看到空白的黑色图像,我看到了那个结果。
  2. 当我创建由 0 或 1 组成的随机数组时,我希望看到黑白混合图像,我看到了那个结果。
  3. 我希望在使用 np.zeros 创建图像时看到空白的白色图像,但我仍然看到空白的黑色图像。

这是我的代码:

def display_img(img):
    fig = plt.figure(figsize=(12,10))
    ax = fig.add_subplot(111)
    ax.imshow(img,cmap='gray')

1.) 下面的代码给出了预期的黑色图像。

black = np.zeros((600,600),dtype=np.int8)
black
display_img(black)
array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ...,
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=int8)

2.) 下面的代码给出了混合图像黑色图像和预期的白噪声,因为现在一些值是 1。

white_noise = np.random.randint(low=0,high=2,size=(600,600))
white_noise
display_img(white_noise)
array([[0, 1, 1, ..., 0, 1, 0],
       [1, 0, 1, ..., 1, 0, 0],
       [0, 1, 0, ..., 0, 0, 1],
       ...,
       [1, 1, 1, ..., 0, 0, 1],
       [0, 1, 1, ..., 0, 1, 0],
       [0, 0, 0, ..., 0, 1, 0]])

3.) 下面的代码给出了黑色图像,但我希望看到白色

white = np.ones((600,600),dtype=np.int8)
white
white

array([[1, 1, 1, ..., 1, 1, 1],
       [1, 1, 1, ..., 1, 1, 1],
       [1, 1, 1, ..., 1, 1, 1],
       ...,
       [1, 1, 1, ..., 1, 1, 1],
       [1, 1, 1, ..., 1, 1, 1],
       [1, 1, 1, ..., 1, 1, 1]], dtype=int8)
display_img(white)

如果您编码 ax.imshow(img,cmap='gray', vmin=0, vmax=1) 而不是 ax.imshow(img,cmap='gray'),它会起作用。

你可以明白为什么 here :

vmin 和 vmax 定义颜色图覆盖的数据范围。