How to fast change image brightness with python + OpenCV?

前端 未结 12 909
感动是毒
感动是毒 2020-12-05 10:42

I have a sequence of images. I need to average brightness of these images.

First example (very slow):

img = cv2.imread(\'test.jpg\')         


        
12条回答
  •  生来不讨喜
    2020-12-05 11:22

    This was my solution to both increase and decrease brightness. Was having some error issues with a couple of the other answers. Function takes in a positive or negative value and alters brightness.

    example in code

    img = cv2.imread(path_to_image)
    img = change_brightness(img, value=30) #increases
    img = change_brightness(img, value=-30) #decreases
    

    function being called

    def change_brightness(img, value=30):
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
        h, s, v = cv2.split(hsv)
        v = cv2.add(v,value)
        v[v > 255] = 255
        v[v < 0] = 0
        final_hsv = cv2.merge((h, s, v))
        img = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
    return img
    

提交回复
热议问题