Matplotlib histogram

1.利用 histogram 進行色彩分佈分析:

if __name__ == "__main__":
    import numpy as np
    from PIL import Image
    from matplotlib import pyplot as plt
    from matplotlib.pylab import hist  
    
    pil_img = Image.open("lena.jpg").convert("L")
    img = np.array(pil_img)
    print (img.shape, img.dtype)
    
    plt.subplot(121)
    plt.imshow(img, cmap="gray")
    plt.subplot(122)
    hist(img.flatten(),64)
    plt.show()

!!! 資料必須是一維的所以要呼叫 flatten,hist 的第二個參數決定了 bin 的數目。

運行結果如下:

留言

熱門文章