首页 > 解决方案 > python - 如何获得沿图像线的平均像素强度并将它们绘制在图表上?

问题描述

我有一个灰度图像。我想生成一个直方图,该直方图对应于沿 x 和 y 轴的每条线的平均像素强度。

例如,此图像应生成两个看起来像钟形曲线的直方图

标签: python

解决方案


我会使用 PIL/pillow、numpy 和 matplotlib

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt

# load Image as Grayscale
i = Image.open("QWiTL.png").convert("L")
# convert to numpy array
n = np.array(i)

# average columns and rows
# left to right
cols = n.mean(axis=0)
# bottom to top
rows = n.mean(axis=1)

# plot histograms
f, ax = plt.subplots(2, 1)
ax[0].plot(cols)
ax[1].plot(rows)
f.show()

推荐阅读