Calculating kurtosis from an image

Viewed 1423

I have this code:

import cv2
from scipy.stats import kurtosis, skew

def main():
    img1 = 'lenna.jpg'
    gray_img = cv2.imread(img1, cv2.IMREAD_GRAYSCALE)

    print(f'Kurtosis: {kurtosis(gray_img)}')

I want to calculate the kurtosis for an given image, but when I run this code, it shows like this:

enter image description here

Is it right to return all those values? In my thoughts it supposedly return a single value, because when I run kurtosis in matlab, it returns 1.8561

1 Answers

You are calling kurtosis on axis=0 by default (see docs), but it seems like you're trying to run it on the whole array. So you can try axis=None:

kurtosis(gray_img, axis=None)

Example:

im = np.random.randint(0,256,(5,5))

>>> im
array([[104, 125,  26, 194,  47],
       [198,  46, 105,  38, 241],
       [211, 115,  62, 196, 169],
       [ 92, 184, 212, 100, 117],
       [212,  35, 208,  52,   3]])

>>> kurtosis(im)
array([-1.78890733, -1.32154862, -1.66900354, -1.74522902, -1.30434102])

>>> kurtosis(im, axis=None)
-1.3968689968948007

[EDIT]: From your comments, you are trying to calculate skewness, not kurtosis. For this, use:

from scipy.stats import kurtosis,skew
skew(gray_img, axis=None)

In matlab, on the array above, your code (taken directly from your comments) gives:

im =

   104   125    26   194    47
   198    46   105    38   241
   211   115    62   196   169
    92   184   212   100   117
   212    35   208    52     3

>> I2 = im2double(im); 
>> s=skewness(I2(:))

s =

    0.0118

In scipy, it gives:

>>> skew(im,axis=None)
0.011819746815198935

[Edit #2]: It seems MATLAB's default kurtosis definition is Pearson's, whereas Scipy's is Fisher's. +1 to scipy for being more flexible than MATLAB! So you can use:

kurtosis(im, None, fisher=False)

To get the same results:

# Scipy:
>>> kurtosis(im,None, fisher=False)
1.6031310031051993
# Matlab:
>>  s=kurtosis(I2(:))

s =

    1.6031
Related