1、颜色直方图(单张图像 绘制颜色直方图)
#-------------------------------单张图像 绘制颜色直方图-三通道直方图-----
import cv2
import numpy as np
import matplotlib.pyplot as plt
def plot_demo(image):
plt.hist(image.ravel(),256,[0,256])
plt.show("直方图")
def image_hist(image):
color=('blue','green','red')
for i,color in enumerate(color):
hist=cv2.calcHist([image],[i],None,[256],[0,256])
plt.plot(hist,color=color)
plt.show()
if __name__ == '__main__':
image=cv2.imread('../opencv-python-img/lena.png')
#plot_demo(image)
image_hist(image)
2.批量绘制三通道(RGB)直方图
import cv2
import numpy as np
import os
def ImageHist(image,type):
color=(255,255,255)
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'
#1 image 2 通道 3 mask None 4 256种灰度值 5 0-255
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
if __name__=="__main__":
path='../data/person/img/'
for file in os.listdir(path):
file_path=os.path.join(path,file)
img=cv2.imread(file_path,1)
cv2.imshow('img',img)
channels=cv2.split(