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?

Viewed 475

What i mean that:

  1. I expect to see blank black image when i create image using np.ones which i see that result.
  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.
  3. I expect to see blank white image when i create image using np.zeros which i still see blank black image.

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.

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.

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

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)
1 Answers

It is working if instead of ax.imshow(img,cmap='gray') you code ax.imshow(img,cmap='gray', vmin=0, vmax=1).

You can see why here :

vmin and vmax define the data range that the colormap covers.

Related