OpenCVで画像のヒストグラムをmatplotlibで出力する
Python3、OpenCV、matplotlib を使って画像のヒストグラムを出力します。まずは下の画像をグレースケールにしたもののヒストグラムを表します。

import cv2
from matplotlib import pyplot as plt
filename = 'park.jpg'
img = cv2.imread(filename, 0)
plt.hist(img.ravel(), 256, [0, 256])
plt.show()

手順のまとめ:
- imread で画像を読む
- そのデータを ravel で加工する
- matplotlib の hist でヒストグラムを作る
- 出力する