Estimate Brightness of an image Opencv

前端 未结 4 648
被撕碎了的回忆
被撕碎了的回忆 2020-12-14 03:08

I have been trying to obtain the image brightness in Opencv, and so far I have used calcHist and considered the average of the histogram values. However, I feel this is not

4条回答
  •  被撕碎了的回忆
    2020-12-14 03:17

    I prefer Valentin's answer, but for 'yet another' way of determining average-per-pixel brightness, you can use numpy and a geometric mean instead of arithmetic. To me it has better results.

    from numpy.linalg import norm
    
    def brightness(img):
        if len(img.shape) == 3:
            # Colored RGB or BGR (*Do Not* use HSV images with this function)
            # create brightness with euclidean norm
            return np.average(norm(img, axis=2)) / np.sqrt(3)
        else:
            # Grayscale
            return np.average(img)
    

提交回复
热议问题