【问题标题】:How can I plot a color histogram with all 3 channels (red, green, blue) in the same figure in python? [closed]如何在 python 的同一图中绘制具有所有 3 个通道(红色、绿色、蓝色)的颜色直方图? [关闭]
【发布时间】:2021-07-04 04:40:39
【问题描述】:

我使用下面的代码读取图像,然后在 python 中为其生成颜色直方图。

path = r"G:\3Y_individualProject\farm_colormap1.jpg"
colormap1 = cv2.imread(path)
colormap1=cv2.cvtColor(colormap1, cv2.COLOR_BGR2RGB)
plt.imshow(colormap1)
chans=cv2.split(colormap1)
colors=("b", "g", "r")
plt.figure()
plt.title("Color histogram")
plt.xlabel("Bins")
plt.ylabel("Number of pixels")
for (chan, c) in zip(chans, colors):
    hist=cv2.calcHist([chan], [0], None, [256], [0,256])
    plt.plot(hist, color=c)
    plt.xlim([0,256])
    plt.show()

但是,它会将 3 个颜色通道(红色、绿色、蓝色)绘制成 3 个不同的图形,就像这样

那么如何将它们绘制在同一个图中?谢谢!

【问题讨论】:

  • plt.show()移出循环

标签: python opencv matplotlib histogram


【解决方案1】:

您拨打plt.show() 两次。因此,在执行循环的一部分后,它会显示,然后再次执行循环并显示第二个。

固定版本如下:

path = r"G:\3Y_individualProject\farm_colormap1.jpg"
colormap1 = cv2.imread(path)
colormap1=cv2.cvtColor(colormap1, cv2.COLOR_BGR2RGB)
plt.imshow(colormap1)
chans=cv2.split(colormap1)
colors=("b", "g", "r")
plt.figure()
plt.title("Color histogram")
plt.xlabel("Bins")
plt.ylabel("Number of pixels")
for (chan, c) in zip(chans, colors):
    hist=cv2.calcHist([chan], [0], None, [256], [0,256])
    plt.plot(hist, color=c)
    plt.xlim([0,256])
plt.show()

【讨论】:

    猜你喜欢
    • 2011-10-28
    • 2018-03-31
    • 1970-01-01
    • 2013-12-19
    • 2022-12-06
    • 1970-01-01
    • 2016-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多