【问题标题】:Show multiple histograms of images显示多个图像直方图
【发布时间】:2019-12-10 11:44:16
【问题描述】:

我有 4 张图像,我试图在一个带有 4 个子图的图中显示它们的直方图。

我已经对其进行了调试,并且分配的 img 值总是发生变化,我不确定为什么第一张图像的直方图会覆盖所有其他直方图。

最终结果是它向我显示了相同的直方图 4 次。

img_path_fldr = '/home/some_folder'
files_lst = os.listdir(img_path_fldr)
img_lst = [x for x in files_lst if x.endswith('jpg') or x.endswith('.png')]

fig, ax = plt.subplots(nrows=2, ncols=2)

for row in ax:
    for col in row:
        color = ('b')
        for j, col1 in enumerate(color):
            for i in img_lst:
                img = cv.imread(os.path.join(img_path_fldr, i))
                histr = cv.calcHist([img], [j], None, [256], [0, 256])
                col.plot(histr, color=col1)

plt.show()

【问题讨论】:

  • 循环的方式,在每一个轴上,你都在做同样的事情(读取图像并计算直方图)。因此,在每个子图中,您都在绘制相同的东西,即最后一张图像的直方图。你需要重写你的循环。

标签: python matplotlib computer-vision histogram


【解决方案1】:

我不遵循您的 for 循环继承。在我看来,下面的代码可以满足您的需求

fig, axes = plt.subplots(nrows=2, ncols=2)

for ax, file in zip(axes.flat, img_lst):
    img = cv.imread(os.path.join(img_path_fldr, file))
    histr = cv.calcHist([img], [0], None, [256], [0, 256])
    ax.plot(histr, color=col1)

plt.show()

【讨论】:

    猜你喜欢
    • 2015-04-15
    • 2012-01-02
    • 2018-02-08
    • 1970-01-01
    • 2013-08-08
    • 2020-11-13
    • 2015-03-03
    • 1970-01-01
    • 2013-01-18
    相关资源
    最近更新 更多