python+opencv 图像直方图(histogram)

(个人理解,对于RGB图像,每个通道的像素值在0-255之间,共256个值,所以Bin的数目是256。加入图像是14位的,则图像一共有2^{14}个像素值。故一个Bin的大小是64,所以统计得到的直方图x轴是0-255,y轴是0-64。) 

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt


def plot_demo(image):
    #  ravel把出现的频次都统计出来,范围是0-256
    plt.hist(image.ravel(), 256, [0, 256])
    plt.show()


def image_hist(image):
    color = ('blue', 'green', 'red')
    for i, color in enumerate(color):
        hist = cv.calcHist([image], [i], None, [256], [0, 256])
        plt.plot(hist, color=color)
        plt.xlim([0, 256])
    plt.show()

src = cv.imread('C:/Users/Y/Pictures/Saved Pictures/demo.png')
cv.namedWindow('input image', cv.WINDOW_AUTOSIZE)
cv.imshow('input image', src)
image_hist(src)
cv.waitKey(0)
cv.destroyAllWindows()

原图像

plt.hist()绘制的直方图

三通道的直方图

发布了70 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Acmer_future_victor/article/details/104145692