Issue
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?
Solution
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):
n = (freq * (val - mean_h(val, freq)) ** n).sum() / freq.sum()
d = var_h(val, freq) ** (n / 2)
return n / d
skewness and kurtosis are just the 3rd and 4th moments
Answered By - Daniel F
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.