Pin 8
Pin 8

Reputation: 90

How can I transform a matplotlib.pyplot colormap representation of an image into an RGB image

I have a microscopy image that describes a certain property of the specimen (i.e., the fluorescence lifetime) stored as a greyscale float64 object.

enter image description here

I also have a second microscopy image of the exact same area that describes another property of the specimen (i.e., the fluorescence intensity) and this second image is also stored as a greyscale float64 object.

enter image description here

I would like to combine both images into a single matplotlib.pyplot representation in which the color of the pixels is given by the values in the first image (for example, using a colormap such as 'jet' from matplotlib) and the intensity of the pixels is given by the values in the second image. Ideally, I would also like to have colorbars for both color (jet colormap for the lifetime values stored in the first image) and for the intensity (grayscale colormap for the intensity values stored in the second image). Could you please tell me how to achieve this in Python? Below you can see an example of what I would like to achieve, obtained with a commercial software.

enter image description here

Upvotes: 0

Views: 1169

Answers (2)

Pin 8
Pin 8

Reputation: 90

I found the solution, which is to use alpha blending as shown below. The alpha parameter can be tweaked to alter the extent to which both images are simultaneously shown.

fig = plt.figure(figsize=(15,15)) # This creates the figure. 
im1 = plt.imshow(intensity_image, cmap='gray') # This shows the first image as a grayscale image.
plt.axis('off') # This removes the axes around the image.
plt.clim(200,800) # This clips the values within the first image to the 200-800 range. It affects the values of the colorbar.
plt.colorbar(shrink=0.3, aspect=5.0) # This draws a colorbar that 0.3x the size of the image and is a little bit thicker (aspect=5.0) than the default one.
im2 = plt.imshow(flim_image, cmap='jet', alpha=0.5) # This shows the second image using a 'jet' colorspace.
plt.axis('off') # Removes the image axes.
plt.clim(2.0,3.0) # Clips the image values to the range 2.0-3.0.
plt.colorbar(shrink=0.3, aspect=5.0) # Draws the colorbar for the second image. 
plt.show() # Shows the figure.

enter image description here

Upvotes: 0

Max
Max

Reputation: 4045

I guess that you are looking for a false color image representation. While I am not sure if matplotlib is the ideal way to do this, here is a post in how to do this with pillow. It takes the entire image (including the colorbar) and converts the gray-scale image to a colored RGB-image.

You can do the same thing with matplotlib. Unfortunately, it is not as easy as changing the colormap in matplotlib.pyplot.imshow. You will need to do some manual scaling.

Upvotes: 1

Related Questions