[英]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:我的意思是:
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.