1.绘制直方图

# coding=utf-8

import cv2
import numpy as np
from matplotlib import pyplot as plt img1 = cv2.imread('woman.JPEG',0)
cv2.imshow("woman",img1) #matplotlib中绘制直方图函数 matplotlib.pyplot.hist()
plt.hist(img1.ravel(),256,[0,256])
#img1.ravel() 将图像转化为数组
#BIN的数目,将256个值分成256份
#像素值范围
plt.show() cv2.waitKey(0)
cv2.destroyAllWindows()

day3 直方图-LMLPHPday3 直方图-LMLPHP

2.多通道直方图

# coding=utf-8

import cv2
import numpy as np
from matplotlib import pyplot as plt img = cv2.imread('woman.JPEG')
cv2.imshow("woman",img) color = ('b','g','r') # 对一个列表或数组既要遍历索引又要遍历元素时
# 使用内置 enumerrate 函数 for i,col in enumerate(color):
histr = cv2.calcHist([img],[i],None,[256],[0,256])
#i:通道 None:掩模图像
plt.plot(histr,color = col )
plt.xlim([0,256]) plt.show() cv2.waitKey(0)
cv2.destroyAllWindows()

day3 直方图-LMLPHP

05-18 10:09