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