imshow(img, cmap=cm.gray) 显示 128 值的白色 [英] imshow(img, cmap=cm.gray) shows a white for 128 value

查看:55
本文介绍了imshow(img, cmap=cm.gray) 显示 128 值的白色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 MatLab 转向 python 并尝试使用 imshow 函数.

I'm moving from MatLab to python and playing around with the imshow function.

我似乎无法理解为什么它没有将值 128 显示为灰色,因为我选择了 cmap 为灰度.

I can't seem to get my head around why it doesn't show the value 128 as grey with I have chosen the cmap to be gray-scale.

它似乎对最高 (128) 和最低值使用灰度.我希望它对 [0:255] 使用灰度.我该怎么做?

It seems as it uses the grayscale for highest (128) and lowest values.. I want it to use the grayscale for [0:255]. How do I do that?

推荐答案

使用 vminvmax 参数:

plt.imshow(bg, cmap=plt.get_cmap('gray'), vmin=0, vmax=255)

不指定vminvmaxplt.imshow 会自动将其范围调整为数据的最小值和最大值.

Without specifying vmin and vmax, plt.imshow auto-adjusts its range to the min and max of the data.

我不知道有什么方法可以为所有 imshow 图设置默认的 vminvmax 参数,但您可以使用 functools.partial准备一个自定义的类似 imshow 的命令,设置默认参数:

I do not know of a way to set default vmin and vmax parameters for all imshow plots, but you could use functools.partial to prepare a custom imshow-like command with default parameters set:

import matplotlib.pyplot as plt
import numpy as np
import functools

bwimshow = functools.partial(plt.imshow, vmin=0, vmax=255,
                             cmap=plt.get_cmap('gray'))

dots = np.random.randn(10, 10)*255
bwimshow(dots)
cbar = plt.colorbar()

plt.show()

这篇关于imshow(img, cmap=cm.gray) 显示 128 值的白色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆