OpenCV-Python畫直方圖和累積直方圖


代碼如下:

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('C:\\Users\\admin\\Desktop\\original_img3\\testimg\\messi.jpg')
_, _, colorChannel = img.shape
color = ['B', 'G', 'R']
plt.figure()
for i in range(colorChannel):
    hist_img, _ = np.histogram(img[:, :, i], 256)
    plt.plot(range(256), hist_img, label=color[i])
    plt.legend(loc='best')
    plt.title('histogram')

plt.figure()
for i in range(colorChannel):
    hist_img, _ = np.histogram(img[:, :, i], 256)
    cdf_img = np.cumsum(hist_img)   # accumulative histogram
    plt.plot(range(256), cdf_img, label=color[i])
    plt.legend(loc='best')
    plt.title('accumulative histogram')

效果如下:

   


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM