(個人理解,對於RGB圖像,每個通道的像素值在0-255之間,共256個值,所以Bin的數目是256。加入圖像是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()繪製的直方圖
三通道的直方圖