【问题标题】:How can I see the RGB channels of a given image with python?如何使用 python 查看给定图像的 RGB 通道?
【发布时间】:2017-02-14 13:26:56
【问题描述】:

想象一下,我有一张图像,我想分割它并查看 RGB 通道。我如何使用 python 做到这一点?

【问题讨论】:

  • 到目前为止你有没有做过任何研究?

标签: python image image-processing rgb


【解决方案1】:

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

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

测试图像:

我将使用this test image

代码:

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()

输出:

评论

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

【讨论】:

  • 哇,谢谢,不是我需要的,但这真的很有趣。抱歉含糊不清,我的意思是我想为每个频道做一个直方图
  • @JuanTelo 你本可以提到这个......嗯......我将它添加到代码中。
  • 我也有类似的问题。你能调查一下吗? stackoverflow.com/questions/64145300/…
猜你喜欢
  • 2015-03-16
  • 1970-01-01
  • 2017-09-17
  • 1970-01-01
  • 2019-06-12
  • 2020-05-07
  • 1970-01-01
  • 2019-09-26
  • 2015-06-25
相关资源
最近更新 更多