我正在尝试找出一种方法来查找灰度图像的直方图。所以首先我找到了图像的平均权重,然后将其输出为灰度,很好。我只是对将灰色图像绘制成直方图感到困惑。有人可以帮我弄清楚如何将灰度图像绘制成直方图吗?
import numpy as np
import matplotlib.pyplot as py
from PIL import Image as im
from scipy import misc
img = misc.imread("Zpicture.jpg")
def weightedAverage(pixel): ## function to calculate
return 0.299*pixel[0] + 0.587*pixel[1] + 0.114*pixel[2]
grey = np.zeros((img.shape[0], img.shape[1])) # init 2D numpy array
# get row number
for rownum in range(len(img)):
for colnum in range(len(img[rownum])):
grey[rownum][colnum] = weightedAverage(img[rownum][colnum])
py.imshow(grey, cmap = py.matplotlib.cm.Greys_r)
py.show()
最佳答案
我认为您正在寻找plt.hist(grey)
。请注意,通常的约定是 import matplotlib.pyplot
as plt
。坚持常用的样式是件好事!
最后,您的图像转换代码可以简化为:
grey = 0.299*img[:,:,0] + 0.587*img[:,:,0] + 0.114*img[:,:,0]
关于python - 绘制灰度图像的直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42639839/