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

Imagine a I have an image and I want to split it and see the RGB channels. How can I do it using python?

解决方案

My favorite approach is using scikit-image. It is built on top of numpy/scipy and images are internally stored within numpy-arrays.

Your question is a bit vague, so it's hard to answer. I don't know exactly what you want to do but will show you some code.

Test-image:

Code:

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

Output:

Comment

Output looks good: see for example the yellow flower, which has much green and red, but not much blue which is compatible with wikipedia's scheme from here:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值