0

I'm trying to get the four first order histogram statistics (mean, variance, skewness and kurtosis) from a histogram.

I have this code that calculates the histogram:

import cv2
from matplotlib import pyplot as plt

img1 = 'img.jpg'
gray_img = cv2.imread(img1, cv2.IMREAD_GRAYSCALE)
plt.hist(gray_img.ravel(),256,[0,256])
plt.title('Histogram for gray scale picture')
plt.show()

How can I get that statistics?

asdas
  • 177
  • 1
  • 4
  • 16

2 Answers2

2

Based on my answer here

def mean_h(val, freq):
    return np.average(val, weights = freq)

def var_h(val, freq):
    dev = freq * (val - mean_h(val, freq)) ** 2
    return dev.sum() / freq.sum()

def moment_h(val, freq, n):
    nom = (freq * (val - mean_h(val, freq)) ** n).sum() / freq.sum()
    d = var_h(val, freq) ** (n / 2)
    return nom / d

skewness and kurtosis are just the 3rd and 4th moments

Peter
  • 3,322
  • 3
  • 27
  • 41
Daniel F
  • 13,620
  • 2
  • 29
  • 55
0

If the number of bins is reasonable, you should be able to just count the values manually, put in a vector; and calculate all those moments.

Mathew Carroll
  • 357
  • 3
  • 14