如何将图像的matplotlib.pyplot颜色映射表示转换为RGB图像

2024-06-17 09:20:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一张显微镜图像,它描述了作为灰度对象存储的样本的特定属性(即荧光寿命)

enter image description here

我还有一个与描述样本另一特性(即荧光强度)完全相同的区域的第二个显微镜图像,该第二个图像也存储为灰度对象

enter image description here

我想将两个图像合并为一个matplotlib.pyplot表示,其中像素的颜色由第一个图像中的值给出(例如,使用matplotlib中的“jet”等颜色贴图),像素的强度由第二个图像中的值给出。理想情况下,我还希望为颜色(存储在第一幅图像中的寿命值的jet colormap)和强度(存储在第二幅图像中的强度值的灰度colormap)设置颜色条。您能告诉我如何在Python中实现这一点吗?下面你可以看到一个例子,我想实现,获得了商业软件

enter image description here


Tags: 对象图像区域属性matplotlib颜色像素特性
2条回答

我找到了解决方案,即使用alpha混合,如下所示。可以调整alpha参数以改变同时显示两个图像的范围

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

我猜您正在寻找一个假彩色图像表示。 虽然我不确定matplotlib是否是实现这一点的理想方式,但是here is a post in how to do this with ^{}。它获取整个图像(包括颜色条),并将灰度图像转换为彩色RGB图像

你可以用matplotlib做同样的事情。不幸的是,这不像在^{}中更改颜色映射那么容易。您需要进行一些手动缩放

相关问题 更多 >