如果使用colormap或RGB数组,则matplotlib imshow绘图会有所不同 [英] matplotlib imshow plots different if using colormap or RGB array

查看:151
本文介绍了如果使用colormap或RGB数组,则matplotlib imshow绘图会有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题:我正在用显微镜保存16位的tiff图像,需要对其进行分析.我想用numpy和matplotlib做到这一点,但是当我想做一些简单的事情,例如以绿色绘制图像时(以后我将需要叠加其他图像),它会失败.

I am having the following problem: I am saving 16-bit tiff images with a microscope and I need to analyze them. I want to do that with numpy and matplotlib, but when I want to do something as simple as plotting the image in green (I will later need to superpose other images), it fails.

这里是一个示例,当我尝试将图像绘制为RGB阵列或默认的jet颜色图时.

Here is an example when I try to plot the image either as a RGB array, or with the default jet colormap.

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

imageName = 'image.tif'

# image as luminance 
img1 = cv2.imread(imageName,-1)

# image as RGB array
shape = (img1.shape[0], img1.shape[1], 3)
img2 = np.zeros(shape,dtype='uint16')
img2[...,1] += img1

fig = plt.figure(figsize=(20,8))
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)

im1 = ax1.imshow(img1,interpolation='none')
im2 = ax2.imshow(img2,interpolation='none')
fig.show()

对我而言,这将产生以下图形:

Which to me yields the following figure:

很抱歉,这个问题太基本了,但是我不知道为什么正确的图显示了这些工件.我想使用绿色标尺,例如数字的外观(imageJ也会产生类似于左侧图的东西).

I am sorry if the question is too basic, but I have no idea why the right plot is showing this artifacts. I would like to get with the green scale, something like how the figure looks (imageJ also yields somthing similar to the left plot).

非常感谢您的协作.

推荐答案

我发现正确的情节更具艺术感...

I find the right plot much more artistic...

matplotlib在解释图像时相当复杂.它大致如下:

matplotlib is rather complicated when it comes to interpreting images. It goes roughly as follows:

  • 如果图像是任何类型的NxM数组,则将通过色图(自动缩放,如果没有其他说明)对其进行解释. (原则上,如果数组是缩放为0..1的float数组,则应将其解释为灰度图像.这是文档中所说的,但实际上不会发生.)

  • if the image is a NxM array of any type, it is interpreted through the colormap (autoscale, if not indicated otherwise). (In principle, if the array is a float array scaled to 0..1, it should be interpreted as a grayscale image. This is what the documentation says, but in practice this does not happen.)

如果图像是NxMx3 float数组,则RGB分量被解释为0..1之间的RGB分量.如果该值不在此范围内,则以正模1取值,即1.2-> 0.2,-1.7-> 0.3等.

if the image is a NxMx3 float array, the RGB components are interpreted as RGB components between 0..1. If the values are outside of this range, they are taken with positive modulo 1, i.e. 1.2 -> 0.2, -1.7 -> 0.3, etc.

如果图像是NxMx3 uint8数组,则将其解释为标准图像(0..255个分量)

if the image is a NxMx3 uint8 array, it is interpreted as a standard image (0..255 components)

如果图像是NxMx4,则解释如上所述,但是第四个成分是不透明度(alpha)

if the image is NxMx4, the interpretation is as above, but the fourth component is the opacity (alpha)

因此,如果将matplotlibfloat以外的整数提供给matplotlib,则未定义结果.但是,通过查看源代码,可以理解奇怪的行为:

So, if you give matplotlib a NxMx3 array of integers other than uint8 or float, the results are not defined. However, by looking at the source code, the odd behavour can be understood:

if A.dtype != np.uint8:
    A = (255*A).astype(np.uint8)

其中,A是图像数组.因此,如果您给它uint16值0、1、2、3、4 ...,则得到0、255、254、253....是的,它看起来很奇怪. (恕我直言,解释可能会更直观一些,但这就是这样做的方法.)

where A is the image array. So, if you give it uint16 values 0, 1, 2, 3, 4..., you get 0, 255, 254, 253, ... Yes, it will look very odd. (IMHO, the interpretation could be a bit more intuitive, but this is how it is done.)

在这种情况下,最简单的解决方案是将数组除以65535.然后图像应该像预期的那样.另外,如果您的原始图像是真正的线性图像,则需要进行反向伽玛校正:

In this case the easiest solution is to divide the array by 65535., and then the image should be as expected. Also, if your original image is truly linear, then you'll need to make the reverse gamma correction:

img1_corr = (img1 / 65535.)**(1/2.2)

否则您的中间色调会太暗.

Otherwise your middle tones will be too dark.

这篇关于如果使用colormap或RGB数组,则matplotlib imshow绘图会有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆