我试图找到输入图像的直方图。但是,代码不会看到直方图,而是在没有显示任何内容的情况下停止运行。有人能指出我为什么会这样吗?
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()
答案 0 :(得分:2)
您将histogram
与hist
混淆。是的,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
,返回:
plt.hist
将Compute and draw the histogram of x
,返回元组(n, bins, patches)
。