【问题标题】:how to find the histogram of an input image using python?如何使用python找到输入图像的直方图?
【发布时间】:2017-03-11 19:11:35
【问题描述】:

我正在尝试查找输入图像的直方图。但是代码没有看到直方图,而是运行然后停止而不显示任何内容。有人可以向我指出为什么会这样吗?

import pylab as plt
import matplotlib.image as mpimg
import numpy as np

img = np.uint8(mpimg.imread('jack.jpg'))
# convert to grayscale
# do for individual channels R, G, B, A for nongrayscale images

img = np.uint8((0.2126* img[:,:,0]) + \
    np.uint8(0.7152 * img[:,:,1]) +\
         np.uint8(0.0722 * img[:,:,2]))


plt.histogram(img,10)
plt.show()

【问题讨论】:

    标签: python histogram


    【解决方案1】:

    您将histogramhist 混淆了。是的,plt.histogram 是对numpy.histogram 的调用。

    试试这个:

    import pylab as plt
    import matplotlib.image as mpimg
    import numpy as np
    
    img = np.uint8(mpimg.imread('jack.jpg'))
    # convert to grayscale
    # do for individual channels R, G, B, A for nongrayscale images
    
    img = np.uint8(0.2126 * img[:,:,0]) +\
          np.uint8(0.7152 * img[:,:,1]) +\
          np.uint8(0.0722 * img[:,:,2])
    
    plt.hist(img,10)
    plt.show()
    

    [编辑回复评论]

    根据文档(函数名称上面的链接),np.histogramCompute the histogram of a set of data,返回:

    • hist : array 直方图的值。 [...]
    • bin_edges : dtype 数组 float 返回 bin 边缘 (length(hist)+1)。

    plt.histCompute and draw the histogram of x,返回一个元组(n, bins, patches)

    【讨论】:

    • 谢谢你。但我很困惑。两者不一样吗?有什么区别?
    猜你喜欢
    • 2019-12-29
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    相关资源
    最近更新 更多