How Can I See The Rgb Channels Of A Given Image With Python?
Imagine a I have an image and I want to split it and see the RGB channels. How can I do it using python?
Solution 1:
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:
I will use this 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:
Post a Comment for "How Can I See The Rgb Channels Of A Given Image With Python?"