OpenCV python 方式画图像彩色直方图

# encoding:utf-8

import cv2 as cv
from matplotlib import pyplot as plt

print("test opencv start")

image = cv.imread("C:\\Users\\scott\\Pictures\\test\\IMG_0401.JPG")

channels = cv.split(image)
colors = ('b', 'g', 'r')

hist = cv.calcHist([image], [0], None, [256], [0, 256])
plt.figure()  # 新建一个图像
plt.title("RGB Histogram")  # 图像的标题
plt.xlabel("Bins")  # X轴标签
plt.ylabel("# of Pixels")  # Y轴标签

for (channels, color) in zip(channels, colors):
    hist = cv.calcHist([channels], [0], None, [256], [0, 256])
    plt.plot(hist, color=color)
    plt.xlim([0, 256])
plt.show()  # 显示图像

cv.waitKey(0)
cv.destroyAllWindows()

print("test opencv end.")

图像素材:


素材

生成的直方图如下:


素材图片的彩色直方图
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容