代码如下(示例):
import numpy as np
import pylab as pl
import cv2
import matplotlib.pyplot as plt
代码如下(示例):
img = cv2.imread('./MyPic.png', cv2.IMREAD_COLOR)
cv2.imshow('GRAYSCALE+Color', img)
cv2.waitKey(10000)
cv2.destroyWindow('GRAYSCALE+Color')
R_img = img[:, :, 0]
G_img = img[:, :, 1]
B_img = img[:, :, 2]
R_l = R_img.flatten()
G_l = G_img.flatten()
B_l = B_img.flatten()
[MyPic](https://img-blog.csdnimg.cn/7720e104ea8745d3810bca3c7d814e71.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LiA6KeB5aaC5pWF55qE5oiR,size_13,color_FFFFFF,t_70,g_se,x_16
def Hist_draw(l1,l2,l3):
data1 = l1
data2 = l2
data3 = l3
bins = []
lines = np.linspace(0, 257, 258)
for line in lines:
line = int(line)
bins.append(line)
bins = np.array(bins)
plt.figure()
plt.subplot(2, 2, 1)
pl.hist(data1, bins, label='像素数')
plt.legend()
# plt.xlim(0, 257)
# plt.ylim(0, 20000)
pl.xlabel('R通道级')
pl.ylabel('像素个数')
pl.title('图像R通道直方图')
plt.subplot(2, 2, 2)
pl.hist(data2, bins, label='像素数')
plt.legend()
# plt.xlim(0, 257)
# plt.ylim(0, 20000)
pl.xlabel('G通道级')
pl.ylabel('像素个数')
pl.title('图像G通道直方图')
plt.subplot(2, 2, 3)
pl.hist(data3, bins, label='像素数')
plt.legend()
#plt.xlim(0, 257)
#plt.ylim(0, 20000)
pl.xlabel('B通道级')
pl.ylabel('像素个数')
pl.title('图像B通道直方图')
pl.show()
Hist_draw(R_l,G_l,B_l)
本文通过对绘图库matplotlib.pyplot和pylab的使用对RGB通道形式的图片的三种通道以直方图的形式进行更为直观的展示,而 numpy 提供了大量能使我们快速便捷地处理数据的函数和方法。