How to fast change image brightness with python + OpenCV?

前端 未结 12 690
感动是毒
感动是毒 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:19

    I know this question is a bit old, but I thought I might post the complete solution that worked for me (takes care of the overflow situation by saturating at 255):

    def increase_brightness(img, value=30):
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
        h, s, v = cv2.split(hsv)
    
        lim = 255 - value
        v[v > lim] = 255
        v[v <= lim] += value
    
        final_hsv = cv2.merge((h, s, v))
        img = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
        return img
    

    This can be used as follows:

    frame = increase_brightness(frame, value=20)
    
    0 讨论(0)
  • 2020-12-05 11:20

    The other answers suggest doing the saturation "by hand" using all kinds of numpy magic, but you can also use cv2.add() and let OpenCV handle that for you:

    import cv2
    import numpy as np
    
    image = cv2.read('image.png')
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    value = 42 #whatever value you want to add
    cv2.add(hsv[:,:,2], value, hsv[:,:,2])
    image = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
    cv2.imwrite('out.png', image)
    
    0 讨论(0)
  • 2020-12-05 11:21

    Slice to select just the third channel and then modify those elements -

    hsv[:,:,2] += value
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-05 11:24

    Iterating over the whole image to make changes is not a very scalable option in opencv, Opencv provides a lot of methods and functions to perform the arithmetic operations on the given image.

    You may simply split the converted HSV image in the individual channels and then process the V channel accordingly as:

    img = cv2.imread('test.jpg') #load rgb image
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) #convert it to hsv
    
    h, s, v = cv2.split(hsv)
    v += 255
    final_hsv = cv2.merge((h, s, v))
    
    img = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
    cv2.imwrite("image_processed.jpg", img)
    
    0 讨论(0)
  • 2020-12-05 11:28
    def change_brightness(img, alpha, beta):
       return cv2.addWeighted(img, alpha, np.zeros(img.shape, img.dtype),0, beta)
    

    Here alpha & beta are input parameters. Each pixel of the input image will change according to this formula.

     alpha(pixel_value) + beta.
    

    Lower value of alpha like 2 or 3 is good

    0 讨论(0)
提交回复
热议问题