Search code examples
pythonnumpyhistogrambrightnessscikit-image

How to get brighness percentage level from a histogram?


I wrote a python program that used scikit-image to return the histogram of an image:

def get_hist(image):
    image = img_as_float(image)
    hs, dis = exposure.histogram(image)
        cdf = np.cumsum(hs) #cumulative distribution function
        cdf = 255 * cdf / cdf[-1] #normalize
    return hs, cdf


while True:
    # Capture frame-by-frame
    ret, frame = video_capture.read()
    hs, cdf = get_hist(frame)

    print ("Histogram: " + str(hs))

And it works fine:

Histogram: [15258  1224  1221  1040   924   973  1035  1080  1144  1201  1363  1357
1565  1721  1996  2118  2069  2385  2332  2730  2554  2380  2594  2523
2748  2667  2553  2716  3001  2962  3051  2921  2992  3213  3439  3424
3443  3400  3750  3774  3752  3502  3632  3648  3625  3665  4160  3718
3892  3578  3774  4224  4178  4400  4391  4420  4368  4293  3992  4329
4401  4315  4471  4267  4362  4373  4559  3812  3974  4015  3988  3887
3647  3616  3424  3657  3721  3660  3544  3581  3434  3382  3428  3420
3256  3204  3366  3324  3177  3221  3102  3258  3160  3195  3212  3242
3157  2974  3071  2960  2956  2925  3002  3092  2952  2961  2854  2909
3080  2942  3057  3038  2850  2912  3000  2824  2857  2877  2656  2860
2848  2838  2701  2799  2625  2646  2656  2691  2600  2696  2738  2649
2721  2563  2709  2663  2584  2546  2565  2547  2505  2641  2614  2759
2554  2746  2723  2727  2505  2599  2755  2627  2552  2603  2605  2484
2465  2393  2319  2090  2059  2028  1882  1979  1868  1940  1854  1853
1696  1781  1694  1667  1682  1643  1692  1602  1540  1488  1549  1489
1472  1411  1414  1392  1423  1302  1252  1355  1287  1268  1186  1254
1172  1155  1175  1169  1238  1164  1165  1102  1205  1135  1118  1132
1065  1036   969  1009   941  1015   946   979   964   957   997   960
906   894   948   936   882   870   860   911   926   854   870   858
813   855   850   816   793   866   805   788   815   819   801   736
824   808   806   791   805   827   826   805   828   853   845   882
836   867   862 90039]

My question is, how do I convert this array into a value of how bright/dark the image is? I've been looking up online, and the only tutorials I can find are on how to alter the actual image, when that's not what I want.


Solution

  • Well, I don't think you need a histogram for this. Also, "how bright" is not really a defined value. What you could do, is to simply look at the average pixel value.

    image = img_as_float(image)
    print(np.mean(image))
    

    Does this solve your problem?

    An example:

    >>> from skimage import data
    >>> import numpy as np
    >>> from skimage.util import img_as_float
    >>> img = img_as_float(data.moon())
    >>> img
    array([[ 0.45490196,  0.45490196,  0.47843137, ...,  0.36470588,
             0.37647059,  0.37647059],
           [ 0.45490196,  0.45490196,  0.47843137, ...,  0.36470588,
             0.37647059,  0.37647059],
           [ 0.45490196,  0.45490196,  0.47843137, ...,  0.36470588,
             0.37647059,  0.37647059],
           ...,
           [ 0.42745098,  0.42745098,  0.43921569, ...,  0.45882353,
             0.45490196,  0.45490196],
           [ 0.44705882,  0.44705882,  0.44313725, ...,  0.4627451 ,
             0.4627451 ,  0.4627451 ],
           [ 0.44705882,  0.44705882,  0.44313725, ...,  0.4627451 ,
             0.4627451 ,  0.4627451 ]])
    >>> np.mean(img)
    0.43988067028569255