简体   繁体   English

为什么从 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?

What i mean that:我的意思是:

  1. I expect to see blank black image when i create image using np.ones which i see that result.当我使用 np.ones 创建图像时,我希望看到空白的黑色图像,我看到了这个结果。
  2. I expect to see black and white mixed image when i create random array consist either 0 or 1 which i see that result.当我创建随机数组时,我希望看到黑白混合图像,其中包含 0 或 1,我看到了该结果。
  3. I expect to see blank white image when i create image using np.zeros which i still see blank black image.当我使用 np.zeros 创建图像时,我希望看到空白的白色图像,但我仍然看到空白的黑色图像。

Here are my codes:这是我的代码:

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

1.) Below code gives black image as expected. 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.) Below code gives mixed image black image with white noises as expected since some values are 1 now. 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.) Below code gives black image but i expect to see white 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)

It is working if instead of ax.imshow(img,cmap='gray') you code ax.imshow(img,cmap='gray', vmin=0, vmax=1) .如果你编码ax.imshow(img,cmap='gray', vmin=0, vmax=1)而不是ax.imshow(img,cmap='gray')它就可以工作。

You can see why here :你可以在这里看到原因:

vmin and vmax define the data range that the colormap covers. vmin 和 vmax 定义颜色图覆盖的数据范围。

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

相关问题 为什么 np.ones 创建黑色图像? - Why np.ones creates a black image? np.empty,np.zeros和np.ones的性能 - Performance of np.empty, np.zeros and np.ones 将 np.zeros 与 OpenCV imshow 一起使用时,图像看起来曝光过度(几乎全白) - image looks overexposed (nearly all white) when using np.zeros with OpenCV imshow 图像重建(腐蚀和膨胀)都给出黑色图像 output - Image Reconstruction (Erosion and Dilation) both gives black image as output 如何获取numpy.zeros()和(numpy.ones()* 255)分别生成黑白图像? - How to get numpy.zeros() and (numpy.ones() * 255) to produce a black and white image respectively? Matplotlib 将黑白图像显示为灰色 - Matplotlib shows black & white image as gray python中的opencv:一个非常奇怪的错误。 用np.zeros创建一个新图像与简单地复制现有图像有什么区别? - opencv in python: a very strange error. What is the difference between creating a new image with np.zeros and simply copying the existing image? 为什么 np.empty() 和 np.zeros() 返回不同的值? - Why np.empty() and np.zeros() returns different values? 尝试制作黑色图像但 np.unit8 出错 - try to make black image but getting error for np.unit8 np.fft.ifft2 将图像完全变黑 - np.fft.ifft2 turns image completely black
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM