【问题标题】:How to plot 3d rgb histogram of a colored image in python如何在python中绘制彩色图像的3d rgb直方图
【发布时间】:2020-05-16 19:30:31
【问题描述】:

我想绘制彩色图像的 3d 直方图,但我只能绘制 R 和 G 值。我在这里做错了什么?或者有更简单的方法吗

import numpy as np
import matplotlib.image as mpimg
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

img = mpimg.imread('model/obj4__0.png')
pixels = img.shape[0]*img.shape[1]
channels = 3
data = np.reshape(img[:, :, :channels], (pixels, channels))

histo_rgb, _ = np.histogramdd(data, bins=256)
histo_rg = np.sum(histo_rgb, 2)
levels = np.arange(256)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for g in levels:
    ax.bar(levels, histo_rg[:, g], zs=g, zdir='y', color='r')
ax.set_xlabel('Red')
ax.set_ylabel('Green')
ax.set_zlabel('Number of pixels')
plt.show()

【问题讨论】:

    标签: python-3.x histogram


    【解决方案1】:

    如果我能很好地理解你的问题,我也有同样的问题,发现了这个:https://www.bogotobogo.com/python/OpenCV_Python/python_opencv3_image_histogram_calcHist.php

    这是您问题的代码:

    import cv2
    import numpy as np
    from matplotlib import pyplot as plt
    
    img = cv2.imread('images/GoldenGateSunset.png', -1)
    cv2.imshow('GoldenGate',img)
    
    color = ('b','g','r')
    for channel,col in enumerate(color):
        histr = cv2.calcHist([img],[channel],None,[256],[0,256])
        plt.plot(histr,color = col)
        plt.xlim([0,256])
    plt.title('Histogram for color scale picture')
    plt.show()
    
    while True:
        k = cv2.waitKey(0) & 0xFF     
        if k == 27: break             # ESC key to exit 
    cv2.destroyAllWindows()
    

    请注意,这使用 cv2 函数,但您可以将其转换为与 Numpy 一起使用。 我会尝试用 numpy 弄清楚并给你一个更新。

    【讨论】:

      猜你喜欢
      • 2013-03-09
      • 2018-06-02
      • 1970-01-01
      • 1970-01-01
      • 2011-06-30
      • 1970-01-01
      • 2023-03-09
      • 2020-09-07
      • 1970-01-01
      相关资源
      最近更新 更多