为什么从np.ones和np.zeros创建的图像都显示黑色空白图像,而同时合并却给出了预期的黑白混合图像呢?

问题描述

我的意思是

  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]],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,1],0]])

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

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

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

解决方法

如果您使用isThreadContextMapInheritable而不是true进行编码,则可以正常工作。

您可以了解为什么here

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