tfirinci
tfirinci

Reputation: 159

How can I plot the 16,32 and 64 bin histogram of an image that is 8 bit on python?

I have an array of a gray scale image

  Array

sample of array like this;

    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]],

And array type is

    Array.dtype
    dtype('uint8')

I want to plot 16, 32 and 64-bin-histograms of this array, anyone have an idea?

Upvotes: 1

Views: 1271

Answers (1)

fabda01
fabda01

Reputation: 3763

I noticed that you have an RGB image (3 channels). You likely would want to visualize its histogram by each channel (red, green and blue).

You can easily achieve this by using pandas. For example, given an RGB Image array img with the same data structure as your variable Array, you can plot the histogram for each channel by converting it to DataFrame

import pandas as pd

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

And then plotting it using plot.hist

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

Where n_bins is the number of bins.


For n_bins=16

enter image description here

For n_bins=32

enter image description here

For n_bins=64

enter image description here

Upvotes: 1

Related Questions