如何使用 python 查看给定图像的 RGB 通道?

Jua*_*elo 1 python rgb image image-processing

想象一下,我有一张图像,我想分割它并查看 RGB 通道。我怎样才能使用Python来做到这一点?

sas*_*cha 5

我最喜欢的方法是使用scikit-image。它构建在 numpy/scipy 之上,图像内部存储在 numpy 数组中。

你的问题有点模糊,所以很难回答。我不知道你到底想做什么,但会向你展示一些代码。

测试图像:

我将使用这个测试图像

代码:

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

# Read
img = io.imread('Photodisc.png')

# Split
red = img[:, :, 0]
green = img[:, :, 1]
blue = img[:, :, 2]

# Plot
fig, axs = plt.subplots(2,2)

cax_00 = axs[0,0].imshow(img)
axs[0,0].xaxis.set_major_formatter(plt.NullFormatter())  # kill xlabels
axs[0,0].yaxis.set_major_formatter(plt.NullFormatter())  # kill ylabels

cax_01 = axs[0,1].imshow(red, cmap='Reds')
fig.colorbar(cax_01, ax=axs[0,1])
axs[0,1].xaxis.set_major_formatter(plt.NullFormatter())
axs[0,1].yaxis.set_major_formatter(plt.NullFormatter())

cax_10 = axs[1,0].imshow(green, cmap='Greens')
fig.colorbar(cax_10, ax=axs[1,0])
axs[1,0].xaxis.set_major_formatter(plt.NullFormatter())
axs[1,0].yaxis.set_major_formatter(plt.NullFormatter())

cax_11 = axs[1,1].imshow(blue, cmap='Blues')
fig.colorbar(cax_11, ax=axs[1,1])
axs[1,1].xaxis.set_major_formatter(plt.NullFormatter())
axs[1,1].yaxis.set_major_formatter(plt.NullFormatter())
plt.show()

# Plot histograms
fig, axs = plt.subplots(3, sharex=True, sharey=True)

axs[0].hist(red.ravel(), bins=10)
axs[0].set_title('Red')
axs[1].hist(green.ravel(), bins=10)
axs[1].set_title('Green')
axs[2].hist(blue.ravel(), bins=10)
axs[2].set_title('Blue')

plt.show()
Run Code Online (Sandbox Code Playgroud)

输出:

在此输入图像描述

在此输入图像描述

评论

  • 输出看起来不错:例如,参见黄色的花,它有很多绿色和红色,但没有太多蓝色,这与维基百科的方案兼容

在此输入图像描述