How to quickly change image brightness using python + OpenCV?

I have a sequence of images. I need to average the 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 (fast)

 hsv += value 

This example is very quick, but it changes all HSV values โ€‹โ€‹(I need to change only V (brightness))

+14
python numpy image-processing opencv
source share
10 answers

Slice to select only the third channel, and then change these elements -

 hsv[:,:,2] += value 
+17
source share

I know this question is a bit old, but I thought I could post a complete solution that worked for me (takes care of the overflow situation, saturating 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) 
+20
source share

Other answers suggest doing the saturation โ€œmanuallyโ€ using all kinds of magic, but you can also use cv2.add () and let OpenCV handle this 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) 
+8
source share

Iterating over the entire image to make changes is not a very scalable option in opencv; Opencv provides many methods and functions for performing arithmetic operations on a given image.

You can simply split the converted HSV image into separate channels and then process the V-channel accordingly as follows:

 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) 
+7
source share
 import cv2 import numpy as np image = cv2.imread('image.jpg') image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) increase = 100 v = image[:, :, 2] v = np.where(v <= 255 - increase, v + increase, 255) image[:, :, 2] = v image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR) cv2.imshow('Brightness', image) cv2.waitKey(0) cv2.destroyAllWindows() 
+1
source share

I hope this is useful to someone

@Divakar answer Python, OpenCV: increase image brightness without overflowing the UINT8 array

 mImage = cv2.imread('image1.jpg') hsvImg = cv2.cvtColor(mImage,cv2.COLOR_BGR2HSV) value = 0 vValue = hsvImg[...,2] hsvImg[...,2] = np.where((255-vValue)<value,255,vValue+value) plt.subplot(111), plt.imshow(cv2.cvtColor(hsvImg,cv2.COLOR_HSV2RGB)) plt.title('brightened image'), plt.xticks([]), plt.yticks([]) plt.show() 

Dim the brightness

 mImage = cv2.imread('image1.jpg') hsvImg = cv2.cvtColor(mImage,cv2.COLOR_BGR2HSV) # decreasing the V channel by a factor from the original hsvImg[...,2] = hsvImg[...,2]*0.6 plt.subplot(111), plt.imshow(cv2.cvtColor(hsvImg,cv2.COLOR_HSV2RGB)) plt.title('brightened image'), plt.xticks([]), plt.yticks([]) plt.show() 
+1
source share
 def change_brightness(img, alpha, beta): return cv2.addWeighted(img, alpha, np.zeros(img.shape, img.dtype),0, beta) 

Here alpha and beta are input parameters. Each pixel of the input image will change in accordance with this formula.

  alpha(pixel_value) + beta. 

Good alpha value, e.g. 2 or 3

+1
source share

Maybe too old, but I use cv.covertTo, which works for me

 Mat resultBrightImage; origImage.convertTo(resultBrightImage, -1, 1, percent); // Where percent = (int)(percent_val/100)*255, eg, percent = 50 to increase brightness by 50% 

convertTo uses saturate_cast at the end to avoid overflow. I do not use Python, and the above is in C ++, but I hope that it can be easily converted to Python, and I hope this helps

0
source share

You can use this function to change the desired brightness or contrast using C ++ in the same way as you do it in Photoshop or other similar photo editing software.

 def apply_brightness_contrast(input_img, brightness = 255, contrast = 127): brightness = map(brightness, 0, 510, -255, 255) contrast = map(contrast, 0, 254, -127, 127) if brightness != 0: if brightness > 0: shadow = brightness highlight = 255 else: shadow = 0 highlight = 255 + brightness alpha_b = (highlight - shadow)/255 gamma_b = shadow buf = cv2.addWeighted(input_img, alpha_b, input_img, 0, gamma_b) else: buf = input_img.copy() if contrast != 0: f = float(131 * (contrast + 127)) / (127 * (131 - contrast)) alpha_c = f gamma_c = 127*(1-f) buf = cv2.addWeighted(buf, alpha_c, buf, 0, gamma_c) cv2.putText(buf,'B:{},C:{}'.format(brightness,contrast),(10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) return buf def map(x, in_min, in_max, out_min, out_max): return int((x-in_min) * (out_max-out_min) / (in_max-in_min) + out_min) 

After that, you need to call the functions by creating a trackbar using cv2.createTrackbar() and call the above functions with the appropriate parameters. To display brightness values โ€‹โ€‹ranging from -255 to +255 and contrast values โ€‹โ€‹of -127 to +127, you can use this map() function. You can check out the complete Python implementation information here .

0
source share

I know that it should not be so difficult and there to adjust the brightness of the image. In addition, there are already many good answers. I would like to improve @BillGrates answer so that it works on grayscale images and when brightness decreases: value = -255 creates a black image, and value = 255 - white.

 def adjust_brightness(img, value): num_channels = 1 if len(img.shape) < 3 else 1 if img.shape[-1] == 1 else 3 img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) if num_channels == 1 else img hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) h, s, v = cv2.split(hsv) if value >= 0: lim = 255 - value v[v > lim] = 255 v[v <= lim] += value else: value = int(-value) lim = 0 + value v[v < lim] = 0 v[v >= lim] -= value final_hsv = cv2.merge((h, s, v)) img = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR) img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) if num_channels == 1 else img return img 
0
source share

All Articles