显示图像的多个直方图

Show multiple histograms of images

我有 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()

我不了解你的连续 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()