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

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()

最佳答案

您将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.histogram将为Compute the histogram of a set of data,返回:


hist:array直方图的值。 [...]
bin_edges:dtype的数组
float返回垃圾箱边缘(长度(历史)+1)。


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

10-08 03:54