Matplotlib:使用另一个图像着色灰度图像

时间:2013-10-09 16:36:05

标签: python numpy matplotlib

我想要着色的黑白光谱 spectrum 使用此着色图像。 colors

调整此处给出的方法: Applying different color map to mask, 我获得了最终的彩色图像,但它缺少光谱的功能(请参阅代码中的注释以获取图片 finalimage 的链接)。

我的代码在这里给出(在评论中包含托管图像,因为我无法在这里显示所有这些代码):

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

wavemap = "FITS/wavemap.fits.gz"
spectrum = "FITS/phxspectra.fits.gz"

image = pyfits.getdata(spectrum)
colors = pyfits.getdata(wavemap)
mask = colors > 0
colors_ma = np.ma.array(colors, mask=~mask)

kwargs = {'interpolation': 'none', 'vmin': colors[mask].min(), 'vmax': colors.max(), 'origin': 'lower', 'alpha' : 0.5}

plt.imshow(image, cmap=plt.cm.Greys_r, interpolation='none', origin='lower')
plt.imshow(colors_ma, cmap=plt.cm.jet, **kwargs)

plt.show()

#![spectrum](http://i.imgur.com/nQpzvUo.png)
#![spectrumfeatures](http://i.imgur.com/MTQ9yMl.png)
#![colorize](http://i.imgur.com/v27kjsY.png?1)
#![finalimage](http://i.imgur.com/MmnM9qK)
#![finalimagefeatures](http://i.imgur.com/t5PoJiE.png)

如果我降低alpha值,光谱的功能会更好,但颜色非常暗淡。 如果我增加alpha值,那么颜色会更好,但光谱的功能不会显示。

如何从 colorize 图像中获取光谱 的颜色而不为另一个图像进行折扣?

1 个答案:

答案 0 :(得分:3)

一种方法是创建RGBA阵列,其中RGB值表示光谱数据,A值表示强度。或者,您可以使用HSV色图,并分别使用色调和亮度/饱和度来表示光谱和强度值。

import numpy as np
from matplotlib import pyplot as pp
from scipy.misc import lena

# some 'intensity' data, normalized between 0 and 1
intensity = lena().astype(np.float32)
intensity = (intensity - intensity.min()) / (intensity.max() - intensity.min())

# some 'spectrum' data, normalized between 0 and 1
x,y = np.mgrid[-1:1:intensity.shape[0]*1j,-1:1:intensity.shape[1]*1j]
spectrum = 0.5*(np.sin(20*(x**2 + y**2)) + 1)

# look up RGB values from the 'jet' colormap
RGBA = pp.cm.jet(spectrum)
# fill the A(lpha) channel with the array of intensity data
RGBA[...,3] = intensity

# another way to represent spectral and intensity data is using a 2D 
# HSV color space
HSV = np.ones(intensity.shape + (3,))

# we represent the spectral information using hue...
HSV[...,0] = spectrum
# ... and the intensity data as brightness and saturation
HSV[...,1] = intensity
HSV[...,2] = intensity
# convert back into rgb color space
HSV = matplotlib.colors.hsv_to_rgb(HSV)

# plotting
fig, ax = pp.subplots(2, 2, figsize=(10,10), sharex=True, sharey=True)
ax[0,0].imshow(intensity, cmap=pp.cm.gray)
ax[0,0].set_title('Intensity data', fontsize=14)
ax[0,1].imshow(spectrum, cmap=pp.cm.gray)
ax[0,1].set_title('Spectral data', fontsize=14)
ax[1,0].imshow(RGBA)
ax[1,0].set_title('Jet with varying alpha', fontsize=14)
ax[1,1].imshow(HSV)
ax[1,1].set_title('HSV', fontsize=14)
for aa in ax.flat:
    aa.set_axis_bgcolor((0,0,0,1))
    aa.xaxis.set_visible(False)
    aa.yaxis.set_visible(False)
fig.tight_layout()

pp.show() 

enter image description here