Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I adjust brightness, contrast and vibrance with opencv python?

I am new to image processing. I program in Python3 and uses the OpenCV image processing library.I want to adjust the following attributes.

  1. Brightness
  2. Contrast
  3. Vibrance
  4. Hue
  5. Saturation
  6. Lightness

For 4, 5, 6. I am using the following code to convert to HSV space.

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
h += value # 4
s += value # 5
v += value # 6
final_hsv = cv2.merge((h, s, v))
img = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)

The only tutorial I found for 1 and 2 is here. The tutorial uses C++, but I program in Python. Also, I do not know how to adjust 3. vibrance. I would very much appreciate the help, thanks!.

like image 503
Antonie Lin Avatar asked Dec 24 '22 07:12

Antonie Lin


1 Answers

Thanks to @MarkSetchell for providing the link. In short, the answers uses numpy only and the formula can be presented as in below.

new_image = (old_image) × (contrast/127 + 1) - contrast + brightness

Here contrast and brightness are integers in the range [-127,127]. The scalar 127 is used for this range. Also, below is the code I used.

brightness = 50
contrast = 30
img = np.int16(img)
img = img * (contrast/127+1) - contrast + brightness
img = np.clip(img, 0, 255)
img = np.uint8(img)
like image 54
Antonie Lin Avatar answered Apr 26 '23 19:04

Antonie Lin