彩色图片直方图绘制

代码如下:

#API调用方式
import cv2
import numpy as np
def ImageHist(image,type):
    color = (255,255,255)
    windowName = 'Gray'
    if type == 31:
        color = (255,0,0)
        windowName = 'B Hist'
    elif type == 32:
        color = (0,255,0)
        windowName = 'G Hist'
    elif type == 33:
        color = (0,0,255)
        windowName = 'R Hist'
    #图片的数据 计算直方图通道(灰度) mask蒙版  直方图的柱状  像素值
    hist = cv2.calcHist([image],[0],None,[256],[0.0,255.0])
    #最大值最小值坐标及值
    minV,maxV,minL,maxL = cv2.minMaxLoc(hist)
    #创建画布
    histImg = np.zeros([256,256,3],np.uint8)
    for h in range(256):
        intenNormal = int(hist[h]*256/maxV)
        cv2.line(histImg,(h,256),(h,256-intenNormal),color)
    cv2.imshow(windowName,histImg)
    return histImg
img = cv2.imread('image1.jpg',1)
channels = cv2.split(img)#RGB图像分解
for i in range(0,3):
    ImageHist(channels[i],31+i)
cv2.waitKey(0)

实现如图:
彩色图片直方图绘制_第1张图片

你可能感兴趣的:(图形识别,opencv,python)