在网格中并排绘制多个RGB图像和直方图

2024-04-26 17:22:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有10个RGB图像,我计算了它的各个通道的直方图。我想在一个10 x 2的网格上绘制图像及其RGB直方图,第0列表示所有图像,第1列表示它们各自的直方图(R,G,B)连在一起。什么像这样:

直方图

图片来源:https://lmcaraig.com/image-histograms-histograms-equalization-and-histograms-comparison/#2dhistogram

我已经在网上查了好几个选项,但还是觉得我遗漏了一些东西。我的困惑源于对plt.绘图与plt.imshow公司. 在

我正在使用matplotlib和opencv

我已经试过好几种选择了。这是我的密码

from matplotlib import pyplot as plt

w=10
h=10
fig=plt.figure(figsize=(8, 8))
columns = 2
rows = 10
color = ('r', 'g', 'b')
for ii in range(1, rows +1):
    fig.add_subplot(rows, columns, ii)
    img = bad_images_numpy[ii+400,:,:,:] #bad_images_numpy shape-> (4064,64,64,3)
    plt.imshow(img)
    fig.add_subplot(rows, columns, ii+1)
    for i,col in enumerate(color):
        histr = cv2.calcHist([img],[i],None,[256],[0,256])
        plt.plot(histr,color = col)
        plt.xlim([0,256])
plt.show()

目前,我的图像和直方图相互重叠,我不知道如何有效地绘制它们


Tags: columns图像imgformatplotlibfig绘制plt
1条回答
网友
1楼 · 发布于 2024-04-26 17:22:24

您应该检查plt.subplots()plt.subplot(),然后在轴上呈现直方图/图像,而不是plt.imshow(),或{}。在

这是一个类似的函数,我不久前写的,也是为了同样的目的。左边是直方图,右边是图像。我相信你可以把它换成别的样子。在

def histogram_img(img, title=None):
    plt.figure(figsize=(16,6))
    ax1 = plt.subplot(1, 2, 1)
    ax2 = plt.subplot(1, 2, 2)

    colors = ('b','g','r')
    histograms = []

    for i in range(3):
        hist = cv2.calcHist([img], [i], None, [256], [0,255])
        histograms.append(hist)

        ax1.plot(hist, color=colors[i])


    tmp_img = cv2.bitwise_and(img, img, mask=mask)
    ax2.imshow(cv2.cvtColor(tmp_img,cv2.COLOR_BGR2RGB))
    ax2.grid(False)
    ax2.axis('off')    

    if title is not None:
        plt.title =  title

    plt.show()

相关问题 更多 >