如何使用python找到输入图像的直方图?

时间:2017-03-11 00:57:26

标签: python 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.histogram(img,10)
plt.show()

1 个答案:

答案 0 :(得分:2)

您将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)