ZGainsforth
11/8/2017 - 3:00 AM

Make an RGB from three hyperspy maps.

Make an RGB from three hyperspy maps.

from skimage.exposure import rescale_intensity, equalize_hist
def MakeRGBFromMaps(R,G,B, bits=16):
    MaxPixelVal = (2**bits)-1
    
    # Make a Signal2D from the three images and copy over the metadata.
    x = hs.stack([R,G,B])
    x = hs.signals.Signal2D(x).T
    x.metadata = R.metadata
    x.original_metadata = R.original_metadata
    
    # Rescale
    x.data[:,:,0] = rescale_intensity(equalize_hist(x.data[:,:,0]))*MaxPixelVal
    x.data[:,:,1] = rescale_intensity(equalize_hist(x.data[:,:,1]))*MaxPixelVal
    x.data[:,:,2] = rescale_intensity(equalize_hist(x.data[:,:,2]))*MaxPixelVal
    
    # Convert to RGB.
    if bits==16:
        x.change_dtype('uint16')
        x.change_dtype('rgb16')
    else:
        x.change_dtype('uint8')
        x.change_dtype('rgb8')    
    
    # Copy the axes info over from the R map.
    x.axes_manager = R.axes_manager.copy()
    # Of course, the R map is a BaseSignal, and so we have to switch from Nav to Signal.
    x = x.T

    return(x)