【python图像处理】彩色映射
在圖像處理,尤其是醫(yī)學(xué)圖像處理的過(guò)程中,我們經(jīng)常會(huì)遇到將灰度圖映射成彩色圖的情形,如將灰度圖根據(jù)灰度的高低映射成彩虹色圖。這個(gè)過(guò)程我們通常將之稱(chēng)為偽彩映射,偽彩映射的關(guān)鍵在于找到合適的彩色映射表,即colormap,也稱(chēng)color bar。
前段時(shí)間做了一個(gè)涉及到偽彩映射的項(xiàng)目,在找colormap的過(guò)程中,我發(fā)現(xiàn)Python的matplotlib模塊中內(nèi)嵌了一大批常用的colormaps,使用如下代碼:
import numpy as np import matplotlib.pyplot as plt# Have colormaps separated into categories: # http://matplotlib.org/examples/color/colormaps_reference.htmlcmaps = [('Perceptually Uniform Sequential',['viridis', 'inferno', 'plasma', 'magma']),('Sequential', ['Blues', 'BuGn', 'BuPu','GnBu', 'Greens', 'Greys', 'Oranges', 'OrRd','PuBu', 'PuBuGn', 'PuRd', 'Purples', 'RdPu','Reds', 'YlGn', 'YlGnBu', 'YlOrBr', 'YlOrRd']),('Sequential (2)', ['afmhot', 'autumn', 'bone', 'cool','copper', 'gist_heat', 'gray', 'hot','pink', 'spring', 'summer', 'winter']),('Diverging', ['BrBG', 'bwr', 'coolwarm', 'PiYG', 'PRGn', 'PuOr','RdBu', 'RdGy', 'RdYlBu', 'RdYlGn', 'Spectral','seismic']),('Qualitative', ['Accent', 'Dark2', 'Paired', 'Pastel1','Pastel2', 'Set1', 'Set2', 'Set3']),('Miscellaneous', ['gist_earth', 'terrain', 'ocean', 'gist_stern','brg', 'CMRmap', 'cubehelix','gnuplot', 'gnuplot2', 'gist_ncar','nipy_spectral', 'jet', 'rainbow','gist_rainbow', 'hsv', 'flag', 'prism'])]nrows = max(len(cmap_list) for cmap_category, cmap_list in cmaps) gradient = np.linspace(0, 1, 256) gradient = np.vstack((gradient, gradient))def plot_color_gradients(cmap_category, cmap_list):fig, axes = plt.subplots(nrows=nrows)fig.subplots_adjust(top=0.95, bottom=0.01, left=0.2, right=0.99)axes[0].set_title(cmap_category + ' colormaps', fontsize=14)for ax, name in zip(axes, cmap_list):ax.imshow(gradient, aspect='auto', cmap=plt.get_cmap(name))pos = list(ax.get_position().bounds)x_text = pos[0] - 0.01y_text = pos[1] + pos[3]/2.fig.text(x_text, y_text, name, va='center', ha='right', fontsize=10)# Turn off *all* ticks & spines, not just the ones with colormaps.for ax in axes:ax.set_axis_off()for cmap_category, cmap_list in cmaps:plot_color_gradients(cmap_category, cmap_list)plt.show()
我們可以得到matplotlib中內(nèi)嵌的colormaps(應(yīng)該是全部,但不是很確定):
如何獲取colormap
如此眾多的colormaps,應(yīng)該能滿(mǎn)足我們大部分的需求。當(dāng)然,我們更關(guān)心的是如何將這些colormap中具體的數(shù)值導(dǎo)出來(lái),這樣使用起來(lái)會(huì)更加的靈活方便。當(dāng)然,只要你想做到,是沒(méi)有什么能夠阻攔你的。
以獲取最常用的jet映射表為例,我們可以使用如下代碼分別獲取整型和浮點(diǎn)型的jet map,并將其保存在txt文件中:
from matplotlib import cmdef get_jet():colormap_int = np.zeros((256, 3), np.uint8)colormap_float = np.zeros((256, 3), np.float)for i in range(0, 256, 1):colormap_float[i, 0] = cm.jet(i)[0]colormap_float[i, 1] = cm.jet(i)[1]colormap_float[i, 2] = cm.jet(i)[2]colormap_int[i, 0] = np.int_(np.round(cm.jet(i)[0] * 255.0))colormap_int[i, 1] = np.int_(np.round(cm.jet(i)[1] * 255.0))colormap_int[i, 2] = np.int_(np.round(cm.jet(i)[2] * 255.0))np.savetxt("jet_float.txt", colormap_float, fmt = "%f", delimiter = ' ', newline = '\n')np.savetxt("jet_int.txt", colormap_int, fmt = "%d", delimiter = ' ', newline = '\n')print colormap_intreturn
獲取其他種類(lèi)的colormap與之類(lèi)似:
def get_spectral():colormap_int = np.zeros((256, 3), np.uint8)colormap_float = np.zeros((256, 3), np.float)for i in range(0, 256, 1):colormap_float[i, 0] = cm.spectral(i)[0]colormap_float[i, 1] = cm.spectral(i)[1]colormap_float[i, 2] = cm.spectral(i)[2]colormap_int[i, 0] = np.int_(np.round(cm.spectral(i)[0] * 255.0))colormap_int[i, 1] = np.int_(np.round(cm.spectral(i)[1] * 255.0))colormap_int[i, 2] = np.int_(np.round(cm.spectral(i)[2] * 255.0))np.savetxt("spectral_float.txt", colormap_float, fmt = "%f", delimiter = ' ', newline = '\n')np.savetxt("spectral_int.txt", colormap_int, fmt = "%d", delimiter = ' ', newline = '\n')print colormap_intreturn
當(dāng)然,我們也可以根據(jù)需要對(duì)獲得的colormap中的值進(jìn)行調(diào)整。當(dāng)我們獲得心儀的colormap之后,偽彩映射就成了水到渠成的事情了。
下面是用Python寫(xiě)的偽彩映射的代碼:
def gray2color(gray_array, color_map):rows, cols = gray_array.shapecolor_array = np.zeros((rows, cols, 3), np.uint8)for i in range(0, rows):for j in range(0, cols):color_array[i, j] = color_map[gray_array[i, j]]#color_image = Image.fromarray(color_array)return color_array
def test_gray2color():gray_image = Image.open('Image.png').convert("L")gray_array = np.array(gray_image)figure()subplot(211)plt.imshow(gray_array, cmap = 'gray')jet_map = np.loadtxt('E:\\Development\\Thermal\\ColorMaps\\jet_int.txt', dtype = np.int)color_jet = gray2color(gray_array, jet_map)subplot(212)plt.imshow(color_jet)show()return
這一篇先就介紹到這里,后面一篇將向大伙介紹如何使用python生成自定義的colormap。
總結(jié)
以上是生活随笔為你收集整理的【python图像处理】彩色映射的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【OpenCV】cv::Mat对单个像素
- 下一篇: 【算法+OpenCV】基于三次Bezie