【问题标题】:How to get brighness percentage level from a histogram?如何从直方图中获取亮度百分比水平?
【发布时间】:2016-06-17 15:59:40
【问题描述】:

我写了一个 python 程序,使用 scikit-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))

而且效果很好:

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]

我的问题是,如何将此数组转换为图像的亮/暗值?我一直在网上查找,我能找到的唯一教程是关于如何更改实际图像,而这不是我想要的。

【问题讨论】:

    标签: python numpy histogram brightness scikit-image


    【解决方案1】:

    好吧,我认为您不需要直方图。此外,“多么明亮”并不是一个真正的定义值。您可以做的就是简单地查看平均像素值。

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

    这能解决您的问题吗?

    一个例子:

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

    【讨论】:

    • 它只是给了我一个类型错误:TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('S32') dtype('S32') dtype('S32')
    • 另外,我正在实施一篇论文:我需要它尽可能准确,这就是我使用直方图的原因。
    • @Rich:完美。我添加了一个例子。为了准确起见,您必须定义什么是亮度。平均像素值可以是这样的定义,但我敢肯定存在很多
    • 来自网络摄像头:[imgur.com/sUiSdh2].我正在尝试尽可能密切地跟踪昼夜循环。
    • 然后我会找到一天中不同时间的平均值,然后比较结果。你可以找到一个很好的阈值。当然,直方图也可以这样做,但我会查看数据,看看哪种方法给出的结果最一致。
    猜你喜欢
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    相关资源
    最近更新 更多