2

I have a black and white spectrum that I want to colorize spectrum using this colorize image. colors

Adapting the method given here: Applying different color map to mask, I obtain a final colorized image, but it lacks the features of the spectrum (see comments in code for a link to the picture finalimage).

My code is given here (with the hosted images in the comments since I cannot display all of them here):

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](https://i.sstatic.net/NP5mv.png)
#![spectrumfeatures](http://i.imgur.com/MTQ9yMl.png)
#![colorize](https://i.sstatic.net/sOiPT.png)
#![finalimage](http://i.imgur.com/MmnM9qK)
#![finalimagefeatures](http://i.imgur.com/t5PoJiE.png)

If I lower the alpha value, the features of the spectrum show better, but the colors are very dim. If I increase the alpha value, then the colors show much better, but the features of the spectrum do not show.

How can I get the features of the spectrum AND the colors from the colorize image without trading off one for the other?

3
  • 3
    I don't understand what you are trying to achieve....What are these arrays physically (I assume some sort of astro data)? Is the spectrum array just a vertical gradient sampled along the streaks? Commented Oct 9, 2013 at 17:36
  • I'm sorry, I realize the naming convention is very confusing. Physically, the arrays are 2d spectrums. For the spectrum array, each value is a luminance or intensity value. For the colorize array, coordinate values give the wavelength, or color, of the image, which results in the rainbow gradient. Commented Oct 14, 2013 at 7:50
  • What are the axes? wave length and time? k and omega? what does the white in the second image you posted mean? Commented Oct 14, 2013 at 14:22

1 Answer 1

4

One way would be to create an RGBA array, where the RGB values represent the spectral data, and the A values represent the intensity. Alternatively you could use a HSV colormap, and represent spectral and intensity values using hue and brightness/saturation respectively.

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

Sign up to request clarification or add additional context in comments.

1 Comment

Just what I was looking for! However, I ended up using HSV[...,2] = np.ones(intensity.size)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.