matplotlib:彩色映射

在图像处理,尤其是医学图像处理的过程中,我们经常会遇到将灰度图映射成彩色图的情形,如将灰度图根据灰度的高低映射成彩虹色图。这个过程我们通常将之称为伪彩映射,伪彩映射的关键在于找到合适的彩色映射表,即colormap,也称color bar。

前段时间做了一个涉及到伪彩映射的项目,在找colormap的过程中,我发现Python的matplotlib模块中内嵌了一大批常用的colormaps,使用如下代码:


 
 
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. # Have colormaps separated into categories:
  4. # http://matplotlib.org/examples/color/colormaps_reference.html
  5. cmaps = [( 'Perceptually Uniform Sequential',
  6. [ 'viridis', 'inferno', 'plasma', 'magma']),
  7. ( 'Sequential', [ 'Blues', 'BuGn', 'BuPu',
  8. 'GnBu', 'Greens', 'Greys', 'Oranges', 'OrRd',
  9. 'PuBu', 'PuBuGn', 'PuRd', 'Purples', 'RdPu',
  10. 'Reds', 'YlGn', 'YlGnBu', 'YlOrBr', 'YlOrRd']),
  11. ( 'Sequential (2)', [ 'afmhot', 'autumn', 'bone', 'cool',
  12. 'copper', 'gist_heat', 'gray', 'hot',
  13. 'pink', 'spring', 'summer', 'winter']),
  14. ( 'Diverging', [ 'BrBG', 'bwr', 'coolwarm', 'PiYG', 'PRGn', 'PuOr',
  15. 'RdBu', 'RdGy', 'RdYlBu', 'RdYlGn', 'Spectral',
  16. 'seismic']),
  17. ( 'Qualitative', [ 'Accent', 'Dark2', 'Paired', 'Pastel1',
  18. 'Pastel2', 'Set1', 'Set2', 'Set3']),
  19. ( 'Miscellaneous', [ 'gist_earth', 'terrain', 'ocean', 'gist_stern',
  20. 'brg', 'CMRmap', 'cubehelix',
  21. 'gnuplot', 'gnuplot2', 'gist_ncar',
  22. 'nipy_spectral', 'jet', 'rainbow',
  23. 'gist_rainbow', 'hsv', 'flag', 'prism'])]
  24. nrows = max(len(cmap_list) for cmap_category, cmap_list in cmaps)
  25. gradient = np.linspace( 0, 1, 256)
  26. gradient = np.vstack((gradient, gradient))
  27. def plot_color_gradients(cmap_category, cmap_list):
  28. fig, axes = plt.subplots(nrows=nrows)
  29. fig.subplots_adjust(top= 0.95, bottom= 0.01, left= 0.2, right= 0.99)
  30. axes[ 0].set_title(cmap_category + ' colormaps', fontsize= 14)
  31. for ax, name in zip(axes, cmap_list):
  32. ax.imshow(gradient, aspect= 'auto', cmap=plt.get_cmap(name))
  33. pos = list(ax.get_position().bounds)
  34. x_text = pos[ 0] - 0.01
  35. y_text = pos[ 1] + pos[ 3]/ 2.
  36. fig.text(x_text, y_text, name, va= 'center', ha= 'right', fontsize= 10)
  37. # Turn off *all* ticks & spines, not just the ones with colormaps.
  38. for ax in axes:
  39. ax.set_axis_off()
  40. for cmap_category, cmap_list in cmaps:
  41. plot_color_gradients(cmap_category, cmap_list)
  42. plt.show()

我们可以得到matplotlib中内嵌的colormaps(应该是全部,但不是很确定):








如何获取colormap

如此众多的colormaps,应该能满足我们大部分的需求。当然,我们更关心的是如何将这些colormap中具体的数值导出来,这样使用起来会更加的灵活方便。当然,只要你想做到,是没有什么能够阻拦你的。

以获取最常用的jet映射表为例,我们可以使用如下代码分别获取整型和浮点型的jet map,并将其保存在txt文件中:


 
 
  1. from matplotlib import cm
  2. def get_jet():
  3. colormap_int = np.zeros(( 256, 3), np.uint8)
  4. colormap_float = np.zeros(( 256, 3), np.float)
  5. for i in range( 0, 256, 1):
  6. colormap_float[i, 0] = cm.jet(i)[ 0]
  7. colormap_float[i, 1] = cm.jet(i)[ 1]
  8. colormap_float[i, 2] = cm.jet(i)[ 2]
  9. colormap_int[i, 0] = np.int_(np.round(cm.jet(i)[ 0] * 255.0))
  10. colormap_int[i, 1] = np.int_(np.round(cm.jet(i)[ 1] * 255.0))
  11. colormap_int[i, 2] = np.int_(np.round(cm.jet(i)[ 2] * 255.0))
  12. np.savetxt( "jet_float.txt", colormap_float, fmt = "%f", delimiter = ' ', newline = '\n')
  13. np.savetxt( "jet_int.txt", colormap_int, fmt = "%d", delimiter = ' ', newline = '\n')
  14. print colormap_int
  15. return

获取其他种类的colormap与之类似:


 
 
  1. def get_spectral():
  2. colormap_int = np.zeros(( 256, 3), np.uint8)
  3. colormap_float = np.zeros(( 256, 3), np.float)
  4. for i in range( 0, 256, 1):
  5. colormap_float[i, 0] = cm.spectral(i)[ 0]
  6. colormap_float[i, 1] = cm.spectral(i)[ 1]
  7. colormap_float[i, 2] = cm.spectral(i)[ 2]
  8. colormap_int[i, 0] = np.int_(np.round(cm.spectral(i)[ 0] * 255.0))
  9. colormap_int[i, 1] = np.int_(np.round(cm.spectral(i)[ 1] * 255.0))
  10. colormap_int[i, 2] = np.int_(np.round(cm.spectral(i)[ 2] * 255.0))
  11. np.savetxt( "spectral_float.txt", colormap_float, fmt = "%f", delimiter = ' ', newline = '\n')
  12. np.savetxt( "spectral_int.txt", colormap_int, fmt = "%d", delimiter = ' ', newline = '\n')
  13. print colormap_int
  14. return

当然,我们也可以根据需要对获得的colormap中的值进行调整。当我们获得心仪的colormap之后,伪彩映射就成了水到渠成的事情了。

下面是用Python写的伪彩映射的代码:


 
 
  1. def gray2color(gray_array, color_map):
  2. rows, cols = gray_array.shape
  3. color_array = np.zeros((rows, cols, 3), np.uint8)
  4. for i in range( 0, rows):
  5. for j in range( 0, cols):
  6. color_array[i, j] = color_map[gray_array[i, j]]
  7. #color_image = Image.fromarray(color_array)
  8. return color_array


 
 
  1. def test_gray2color():
  2. gray_image = Image.open( 'Image.png').convert( "L")
  3. gray_array = np.array(gray_image)
  4. figure()
  5. subplot( 211)
  6. plt.imshow(gray_array, cmap = 'gray')
  7. jet_map = np.loadtxt( 'E:\\Development\\Thermal\\ColorMaps\\jet_int.txt', dtype = np.int)
  8. color_jet = gray2color(gray_array, jet_map)
  9. subplot( 212)
  10. plt.imshow(color_jet)
  11. show()
  12. return



这一篇先就介绍到这里,后面一篇将向大伙介绍如何使用python生成自定义的colormap。


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在保存图像时,可以使用以下方法来指定彩色映射: 1. 使用图像处理软件(如Photoshop)来调整彩色映射,然后保存图像。 2. 在代码中使用相应的库(如matplotlib)来指定彩色映射,并将图像保存为特定格式(如PNG)。 例如,在使用matplotlib绘制图像并保存时,可以使用以下代码指定彩色映射: ```python import matplotlib.pyplot as plt import numpy as np data = np.random.rand(10, 10) plt.imshow(data, cmap='viridis') plt.colorbar() plt.savefig('output.png') ``` 在这个例子中,`cmap='viridis'`指定了使用Viridis彩色映射,`plt.savefig('output.png')`将图像保存为PNG格式文件。 ### 回答2: 要确保保存的图像与显示的图像相同,我们可以在保存时指定彩色映射彩色映射是将图像中的颜色值映射到显示设备上的过程。下面是一些步骤来实现这个目标: 1. 查找和确定当前显示设备的色彩空间和色彩配置。这些信息通常可以在显示设备的设置或说明书中找到。色彩空间可以是sRGB、Adobe RGB、ProPhoto RGB等。 2. 在图像编辑软件中,确保使用相同的色彩空间和色彩配置。可以在软件的设置中找到这些选项。这样,图像在编辑时会按照指定的色彩空间进行处理。 3. 在保存图像之前,查找软件中的“保存选项”或“导出选项”。在这些选项中,应该有一个用于指定彩色映射的设置。 4. 在彩色映射设置中,选择与显示设备相同的色彩空间和配置。这样,保存的图像将与显示设备上的图像具有相同的颜色。 5. 进行最后的保存。可以选择保存为常见的图像格式,如JPEG、PNG或TIFF。这些格式都支持色彩映射设置。 通过以上步骤,我们可以在保存图像时指定彩色映射,确保保存的图像与显示的图像相同。这对于需要准确呈现颜色的特定应用非常重要,如摄影、艺术设计和打印等。 ### 回答3: 要在保存时指定彩色映射以确保保存的图像与显示的图像相同,可以采取以下步骤: 1. 确定图像的颜色空间:首先要确定图像的颜色空间,常见的有RGB、CMYK、HSV等。根据图像所使用的颜色空间,选择适当的彩色映射方式。 2. 选择正确的文件格式:选择支持彩色映射的文件格式,常见的有JPG、PNG和TIFF等。不同文件格式对彩色映射的支持程度有所差异,需要根据具体需求选择。 3. 校准显示器:确保显示器的颜色校准是准确的。通过使用校准仪器或软件,可以调整显示器的颜色设置,使显示器能够准确地显示图像的颜色。 4. 设置保存参数:在保存图像时,需要注意设置正确的保存参数。根据文件格式和颜色空间,选择相应的保存选项,如使用正确的色彩空间和压缩质量等。 5. 使用色彩管理系统:如果有必要,可以使用色彩管理系统来管理图像的颜色。色彩管理系统可以帮助调整颜色的一致性,确保在不同设备上显示的图像颜色一致。 6. 预览保存图像:在最终保存图像之前,可以预览保存的图像以确保其与显示的图像相同。检查图像的颜色、饱和度和对比度等要素,确保保存后的图像与显示的图像一致。 通过以上步骤,可以在保存图像时指定彩色映射,从而确保保存的图像与显示的图像相同。这样可以保证图像在不同设备上的一致性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值