日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

【python图像处理】彩色映射

發布時間:2025/3/21 python 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【python图像处理】彩色映射 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在圖像處理,尤其是醫學圖像處理的過程中,我們經常會遇到將灰度圖映射成彩色圖的情形,如將灰度圖根據灰度的高低映射成彩虹色圖。這個過程我們通常將之稱為偽彩映射,偽彩映射的關鍵在于找到合適的彩色映射表,即colormap,也稱color bar。

前段時間做了一個涉及到偽彩映射的項目,在找colormap的過程中,我發現Python的matplotlib模塊中內嵌了一大批常用的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中內嵌的colormaps(應該是全部,但不是很確定):








如何獲取colormap

如此眾多的colormaps,應該能滿足我們大部分的需求。當然,我們更關心的是如何將這些colormap中具體的數值導出來,這樣使用起來會更加的靈活方便。當然,只要你想做到,是沒有什么能夠阻攔你的。

以獲取最常用的jet映射表為例,我們可以使用如下代碼分別獲取整型和浮點型的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
獲取其他種類的colormap與之類似:

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
當然,我們也可以根據需要對獲得的colormap中的值進行調整。當我們獲得心儀的colormap之后,偽彩映射就成了水到渠成的事情了。

下面是用Python寫的偽彩映射的代碼:

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。


總結

以上是生活随笔為你收集整理的【python图像处理】彩色映射的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。