OpenCVで画像のヒストグラムをmatplotlibで出力する
Python3、OpenCV、matplotlib を使って画像のヒストグラムを出力します。まずは下の画像をグレースケールにしたもののヒストグラムを表します。
![](https://frozenset.com/mathpython/post/63/1.jpg)
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()
![](https://frozenset.com/mathpython/post/63/2.png)
手順のまとめ:
- imread で画像を読む
- そのデータを ravel で加工する
- matplotlib の hist でヒストグラムを作る
- 出力する