计数直方图
在Opencv
中已经集成了关于彩色图像直方图计算的函数,形式如下:
import cv2
cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]])
参数
images
:array-like
类型,要计算的图像(单张或多张);channels
:要计算的通道索引;mask
:图像掩膜;histSize
:直方图的柱子数量,即将数据分布在多少个区间上计数;ranges
:为直方图取值范围;hist
:为返回值,不用填;accumulate
:多张图的时候是否叠加。
实例
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('D:\\DataSet\\images\\test\\lena.jpg')
color = ('b','g','r') #稍微调整显示颜色,提高可视化效果
for id,bgrcolor in enumerate(color):
hist = cv2.calcHist([img],[id], None, [256], [0.0,255.0]) #彩色图有三个通道,通道索引依次是b:0,g:1,r:2
plt.plot(hist, color=bgrcolor)
plt.title('Histrogram of Color image')
plt.show()
概率密度直方图
示例1
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('D:\\DataSet\\images\\test\\lena.jpg')
color = ('b','g','r') #稍微调整显示颜色,提高可视化效果
for id,bgrcolor in enumerate(color):
plt.hist(img[:,:,id].flatten(),bins=256, density=True,color=bgrcolor,alpha=.7)
plt.title('Histrogram of Color image')
plt.show()
示例2
import cv2
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
img = cv2.imread('D:\\DataSet\\images\\test\\lena.jpg')
color = ('b','g','r') #稍微调整显示颜色,提高可视化效果
for id,bgrcolor in enumerate(color):
sns.kdeplot(img[:,:,id].flatten(), shade=True, color=bgrcolor, label=bgrcolor, alpha=.7)
plt.title('Histrogram of Color image')
plt.show()