如何在python上绘制8位图像的16,32和64 bin直方图?

问题描述 投票:1回答:1

我有一个灰度图像阵列

  Array

像这样的数组样本;

    array([[[142, 142, 142],
    [143, 143, 143],
    [142, 142, 142],
    ...,
    [147, 147, 147],
    [148, 148, 148],
    [143, 143, 143]],

   [[142, 142, 142],
    [142, 142, 142],
    [142, 142, 142],
    ...,
    [148, 148, 148],
    [150, 150, 150],
    [147, 147, 147]],

数组类型是

    Array.dtype
    dtype('uint8')

我想绘制这个数组的16,32和64-bin直方图,谁都有想法?

python python-imaging-library cv2
1个回答
1
投票

我注意到你有一个RGB图像(3个通道)。您可能希望通过每个通道(红色,绿色和蓝色)可视化其直方图。

您可以使用pandas轻松实现此目的。例如,给定与您的变量img具有相同数据结构的RGB图像数组Array,您可以通过将每个通道的直方图转换为DataFrame来绘制直方图

import pandas as pd

df = pd.DataFrame({
    'red': img[...,0].ravel(), 
    'green': img[...,1].ravel(), 
    'blue': img[...,2].ravel()
})

然后使用plot.hist绘制它

df.plot.hist(bins=n_bins, alpha=.3, xlim=[0,255], color=['red', 'green', 'blue'])

其中n_bins是垃圾箱的数量。


对于n_bins=16

enter image description here

对于n_bins=32

enter image description here

对于n_bins=64

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.