Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fast change image brightness with python + OpenCV?

Tags:

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

First example (very slow):

img = cv2.imread('test.jpg') #load rgb image hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) #convert it to hsv  for x in range(0, len(hsv)):     for y in range(0, len(hsv[0])):         hsv[x, y][2] += value  img = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) cv2.imwrite("image_processed.jpg", img) 

Second example (quickly)

hsv += value 

This example very fast but it changes all values HSV (I need to change only V (brightness))

like image 533
Pavel Avatar asked Sep 16 '15 12:09

Pavel


1 Answers

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) 
like image 196
Bill Grates Avatar answered Oct 18 '22 20:10

Bill Grates