覆盖灰度颜色通道

时间:2018-05-04 09:32:28

标签: python matplotlib scikit-image

我希望覆盖两个或更多颜色通道以显示共定位。我玩过matplotlib的imshow,到目前为止我得到的是:

enter image description here

所以重叠已经很明显了,虽然我需要设置一个通道的alpha值,以实际看哪个点属于哪种颜色。但似乎这种方法创造了一个不那么清晰的画面。

有更聪明的方法吗?

images here

import matplotlib.colors
import skimage.io
import matplotlib.pyplot as plt

green_c = skimage.io.imread("c1.tif")
red_c   = skimage.io.imread("c2.tif")

cmap_grn = matplotlib.colors.LinearSegmentedColormap.from_list("", ["black", "green"])
cmap_red   = matplotlib.colors.LinearSegmentedColormap.from_list("", ["black", "red"])

cmap_grn.set_under("k", 0)
cmap_red.set_under("k", 0)

fig, ax = plt.subplots(ncols = 3, figsize = (10,5))
ax = ax.ravel()

greenlim = [2e3, 3e3]
redlim = [2e3, 4e3]

ax[0].imshow(green_c, clim = greenlim, cmap = cmap_grn, alpha = 1)

ax[1].imshow(red_c, clim = redlim, cmap = cmap_red, alpha = 1)

ax[2].imshow(red_c, clim = redlim, cmap = cmap_red, alpha = 1)
ax[2].imshow(green_c, clim = greenlim, cmap = cmap_grn, alpha = 0.5)

plt.tight_layout()
plt.show()

2 个答案:

答案 0 :(得分:1)

您是否考虑过查看图片之间的区别? 我不知道这对实际应用有多大意义,但要了解它可能有意义的差异。

enter image description here

import matplotlib.colors
import skimage.io
import matplotlib.pyplot as plt
import numpy as np

green_c = skimage.io.imread("../data/c1.tif")
red_c   = skimage.io.imread("../data/c2.tif")

cmap_grn = matplotlib.colors.LinearSegmentedColormap.from_list("", ["black", "green"])
cmap_red   = matplotlib.colors.LinearSegmentedColormap.from_list("", ["black", "red"])
cmap_diff   = matplotlib.colors.LinearSegmentedColormap.from_list("", ["red", "white", "green"])

cmap_grn.set_under("k", 0)
cmap_red.set_under("k", 0)

fig, ax = plt.subplots(ncols = 3, figsize = (10,5))
ax = ax.ravel()

greenlim = [2e3, 3e3]
redlim = [2e3, 4e3]

green_norm = (green_c-greenlim[0])/np.diff(greenlim)
red_norm = (red_c-redlim[0])/np.diff(redlim)

diff = green_norm - red_norm
difflim = np.max(np.abs(diff))
difflim = [-difflim, +difflim]

ax[0].imshow(green_norm, clim=[0,1], cmap = cmap_grn, alpha = 1)

ax[1].imshow(red_norm, clim=[0,1], cmap = cmap_red, alpha = 1)

im_diff = ax[2].imshow(diff, clim = [-2,2], cmap = cmap_diff)

plt.tight_layout()
cax = fig.add_axes([1,.2,.025,.6])
fig. colorbar(im_diff, ax=ax[2], cax=cax)
plt.show()

答案 1 :(得分:0)

所以我玩了规范化,就像ImportanceOfBeingErnest在他们的例子中所做的那样,并且从那里很容易做到两个图像的一些真正的颜色混合,而不采用我见过的一些hacky画布缓冲技巧这里。

我意识到一些调整步骤可能是多余的,所以有一些游戏要做。

(我也试过白色背景。虽然它适用于单个通道,但对于这种特殊情况下的混合结果看起来有点......很奇怪。)

enter image description here

import matplotlib.colors
import skimage.io
import skimage.exposure
import matplotlib.pyplot as plt
import numpy as np

def rescale_intensity(image, p1 = 0, p2 = 100):
    if p1 and p2 not in range(101):
        raise TypeError

    p1, p2 = np.percentile(image, [p1, p2])
    img_rescale = skimage.exposure.rescale_intensity(image, in_range = (p1, p2))
    return img_rescale

green_c = skimage.io.imread("c1.tif")
red_c   = skimage.io.imread("c2.tif")

cmap_grn   = matplotlib.colors.LinearSegmentedColormap.from_list("", ["black", "green"])
cmap_red   = matplotlib.colors.LinearSegmentedColormap.from_list("", ["black", "red"])

greenlim = [2e3, 5e3]
redlim = [2e3, 5e3]

green_norm = (green_c-greenlim[0])/np.diff(greenlim)
red_norm = (red_c-redlim[0])/np.diff(redlim)

green_rgba = cmap_grn(green_norm)
red_rgba   = cmap_red(red_norm)

fig, ax = plt.subplots(ncols = 4, figsize = (10,5))
ax = ax.ravel()

blend = np.maximum(green_rgba, red_rgba)

ax[0].imshow(green_rgba)
ax[1].imshow(red_rgba)
ax[2].imshow(blend)
ax[3].imshow(rescale_intensity(blend, 50, 80))

titles = ["Green", "Red", "Blended", "Blended Adjusted"]

for a, t in zip(ax, titles):
    a.set_title(t)

plt.tight_layout()
plt.show()