Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 如何显示灰度图像的彩色地图?_Python 2.7_Matplotlib - Fatal编程技术网

Python 2.7 如何显示灰度图像的彩色地图?

Python 2.7 如何显示灰度图像的彩色地图?,python-2.7,matplotlib,Python 2.7,Matplotlib,我有一个大小为128x128x64的3D图像,它有64个图像,每个图像的大小为128x128。图像显示三个类,标签为0-背景、1-第一对象、2-第二对象。我正在使用matplotlib显示图像 import matplotlib.pyplot as plt fig = plt.figure(figsize=(32,32)) ax1 = plt.add_subplot(111) ax1.imshow(image[:, :, 32]) 我想在彩色地图上显示它。其中,背景应为黑色,对象1为红色,对象

我有一个大小为128x128x64的3D图像,它有64个图像,每个图像的大小为128x128。图像显示三个类,标签为0-背景、1-第一对象、2-第二对象。我正在使用
matplotlib
显示图像

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(32,32))
ax1 = plt.add_subplot(111)
ax1.imshow(image[:, :, 32])
我想在彩色地图上显示它。其中,背景应为黑色,对象1为红色,对象2为绿色。我应该如何修改代码?谢谢 预期结果如图左下角所示
你的问题不是很清楚。你说你想要得到一个像图的左下面板一样的结果,但是这个图像有阴影和不同级别的绿色和红色

从您的问题中,我了解到您有一个128x128数组,只有3个可能的值:
0.
(背景)、
1.
(第一个对象)和
2.
(第二个对象)。对吗?如果是这样的话,本质上你的问题归结为如何创建一个具有3个级别和黑色、红色、绿色的离散颜色贴图

下面是我要做的:

# Generate some fake data for testing
img = np.zeros((128,128))  # Background
img[:50,:50] = np.ones((50,50))  # Object 1
img[-50:,-50:] = np.ones((50,50))*2  # Object 2

#>img
#>array([[ 1.,  1.,  1., ...,  0.,  0.,  0.],
#        [ 1.,  1.,  1., ...,  0.,  0.,  0.],
#        [ 1.,  1.,  1., ...,  0.,  0.,  0.],
#        ..., 
#        [ 0.,  0.,  0., ...,  2.,  2.,  2.],
#        [ 0.,  0.,  0., ...,  2.,  2.,  2.],
#        [ 0.,  0.,  0., ...,  2.,  2.,  2.]])





# Create a custom discret colormap
from matplotlib.colors import LinearSegmentedColormap
cmap = LinearSegmentedColormap.from_list("3colors", ['k','r','g'], N=3)



# Plot
# Don't forget to includes the bounds of your data (vmin/vmax)
# to scale the colormap accordingly
fig,ax = plt.subplots()
im = ax.imshow(img, cmap=cmap, vmin=0, vmax=2)
ax.grid(False)
cax = fig.colorbar(im)
cax.set_ticks([0,1,2])

你的问题不是很清楚。你说你想要得到一个像图的左下面板一样的结果,但是这个图像有阴影和不同级别的绿色和红色

从您的问题中,我了解到您有一个128x128数组,只有3个可能的值:
0.
(背景)、
1.
(第一个对象)和
2.
(第二个对象)。对吗?如果是这样的话,本质上你的问题归结为如何创建一个具有3个级别和黑色、红色、绿色的离散颜色贴图

下面是我要做的:

# Generate some fake data for testing
img = np.zeros((128,128))  # Background
img[:50,:50] = np.ones((50,50))  # Object 1
img[-50:,-50:] = np.ones((50,50))*2  # Object 2

#>img
#>array([[ 1.,  1.,  1., ...,  0.,  0.,  0.],
#        [ 1.,  1.,  1., ...,  0.,  0.,  0.],
#        [ 1.,  1.,  1., ...,  0.,  0.,  0.],
#        ..., 
#        [ 0.,  0.,  0., ...,  2.,  2.,  2.],
#        [ 0.,  0.,  0., ...,  2.,  2.,  2.],
#        [ 0.,  0.,  0., ...,  2.,  2.,  2.]])





# Create a custom discret colormap
from matplotlib.colors import LinearSegmentedColormap
cmap = LinearSegmentedColormap.from_list("3colors", ['k','r','g'], N=3)



# Plot
# Don't forget to includes the bounds of your data (vmin/vmax)
# to scale the colormap accordingly
fig,ax = plt.subplots()
im = ax.imshow(img, cmap=cmap, vmin=0, vmax=2)
ax.grid(False)
cax = fig.colorbar(im)
cax.set_ticks([0,1,2])

对。你做得和我预料的一样。我找不到一个好的例子,所以我只是用谷歌搜索并将其附在问题中。但你做到了我的期望。谢谢汉克斯·迪齐特。我使用了你的代码,效果很好。我只想再问一个关于调整颜色的问题。我们可以选择把黄色调得更醒目,蓝色调得更亮吗。我这样问是因为如果你同时使用红色、蓝色和蓝色,那么我发现蓝色太粗体了,而红色和黄色太轻了。你可以通过(R、G、B、alpha)格式提供它们的值来使用任何颜色。例如:
cmap=LinearSegmentedColormap.from_列表(“3colors”,[(r,g,b),(r,g,b)],N=3)
Right。你做得和我预料的一样。我找不到一个好的例子,所以我只是用谷歌搜索并将其附在问题中。但你做到了我的期望。谢谢汉克斯·迪齐特。我使用了你的代码,效果很好。我只想再问一个关于调整颜色的问题。我们可以选择把黄色调得更醒目,蓝色调得更亮吗。我这样问是因为如果你同时使用红色、蓝色和蓝色,那么我发现蓝色太粗体了,而红色和黄色太轻了。你可以通过(R、G、B、alpha)格式提供它们的值来使用任何颜色。例如:
cmap=LinearSegmentedColormap.from_列表(“3colors”,[(r,g,b),(r,g,b)],N=3)