- Hands-On Image Processing with Python
- Sandipan Dey
- 91字
- 2025-02-22 06:42:31
Plotting the histograms of pixel values for the RGB channels of an image
The histogram() function can be used to compute the histogram (a table of pixel values versus frequencies) of pixels for each channel and return the concatenated output (for example, for an RGB image, the output contains 3 x 256 = 768 values):
pl = im.histogram()
plt.bar(range(256), pl[:256], color='r', alpha=0.5)
plt.bar(range(256), pl[256:2*256], color='g', alpha=0.4)
plt.bar(range(256), pl[2*256:], color='b', alpha=0.3)
plt.show()
The following figure shows the R, G, and B color histograms plotted by running the previous code: