#直方图绘制
#思路  
#1引入模块  2定义方法  3获取直方图属性  4画布构建  5归一化直方图高度 6绘制line
import cv2
import numpy as np
def imageHist(image,type):
    color = (255,255,255)
    windowName = 'Gray'
    if type == 31:
        color = (255,0,0)
        windowName = 'bHist'
    elif type ==32:
        color = (0,255,0)
        windowName = 'gHist'
    elif type ==33:
        color = (0,0,255)
        windowName = 'rHist'
#直方图属性 参数1 image 参数2 通道,0表示灰度通道 参数3 mask模板 参数4 0-255 参数5 灰度值
    hist = cv2.calcHist([image],[0],None,[256],[0.0,255.0])
#绘图,确定hist最大值最小值和下标
    minV,maxV,minL,maxL = cv2.minMaxLoc(hist)
#创建一个画布,三维
    histImage = np.zeros([256,256,3],np.uint8)
    for h in range(0,256):
    #直方图对应像素值数目的归一化,0-256
        intenNormal = int(hist[h]*256/maxV)
    #注意顺序
        cv2.line(histImage,(h,256),(h,256-intenNormal),color)
    cv2.imshow(windowName,histImage)
    return histImage
img = cv2.imread('ruonan.jpg',1)
channal = cv2.split(img)
for i in range(0,3):
    imageHist(channal[i],31+i)
cv2.waitKey(0)