title: Automatically adjusting brightness of image with OpenCV
body: <p>I want to adjust the brightness of an image to a certain value in OpenCV. For example, consider this image:</p>
  
  <p><a href="https://i.stack.imgur.com/Z9oyu.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Z9oyu.png" alt="original image"></a></p>
  
  <p>I calculate the brightness with:</p>
  
  <pre class="lang-py prettyprint-override"><code>import cv2
  img = cv2.imread(filepath)
  cols, rows = img.shape
  brightness = numpy.sum(img) / (255 * cols * rows)
  </code></pre>
  
  <p>and I get an average brightness of 35%. To bring it to 66%, for example, I do:</p>
  
  <pre class="lang-py prettyprint-override"><code>minimum_brightness = 0.66
  alpha = brightness / minimum_brightness
  bright_img = cv2.convertScaleAbs(img, alpha = alpha, beta = 255 * (1 - alpha))
  </code></pre>
  
  <p>and I get an image that seems to have a 50% transparency veil:</p>
  
  <p><a href="https://i.stack.imgur.com/ivDlH.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ivDlH.png" alt="Image with bias and contrast"></a></p>
  
  <p>I can avoid this effect by using bias only:</p>
  
  <pre class="lang-py prettyprint-override"><code>bright_img = cv2.convertScaleAbs(img, alpha = 1, beta = 128)
  </code></pre>
  
  <p>and the image also seems to have a veil:</p>
  
  <p><a href="https://i.stack.imgur.com/pJnUg.png" rel="noreferrer"><img src="https://i.stack.imgur.com/pJnUg.png" alt="Image adjusted with bias only"></a></p>
  
  <p>If I do it by hand, for example in Photoshop with a brightness adjustment at 150, the result seems alright:</p>
  
  <p><a href="https://i.stack.imgur.com/9URKv.png" rel="noreferrer"><img src="https://i.stack.imgur.com/9URKv.png" alt="Image adjusted with Photoshop"></a></p>
  
  <p>But, this is not automatic and does not give the target brightness.</p>
  
  <p>I could do it with either a gamma correction and/or histogram equalization for maybe a more natural result, but I don't see an easy way to get the target brightness other than trial-and-error.</p>
  
  <p>Has anyone succeeded in adjusting brightness automatically to a target?</p>
  
  <h2>Update</h2>
  
  <p>Kanat suggested:</p>
  
  <pre class="lang-py prettyprint-override"><code>bright_img = cv2.convertScaleAbs(img, alpha = 1, beta = 255 * (minimum_brightness - brightness))
  </code></pre>
  
  <p>and the result is better but still has a veil:</p>
  
  <p><a href="https://i.stack.imgur.com/Ah6Th.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Ah6Th.png" alt="Image with adjustment suggested by Kanat"></a></p>
  
  <p>Yves Daoust suggested keeping <code>beta = 0</code>, so I adjusted <code>alpha = minimum_brightness / brightness</code> to get the target brightness:</p>
  
  <pre class="lang-py prettyprint-override"><code>ratio = brightness / minimum_brightness
  if ratio &gt;= 1:
      print("Image already bright enough")
      return img
  
  # Otherwise, adjust brightness to get the target brightness
  return cv2.convertScaleAbs(img, alpha = 1 / ratio, beta = 0)
  </code></pre>
  
  <p>and the result is good:</p>
  
  <p><a href="https://i.stack.imgur.com/LeIP4.png" rel="noreferrer"><img src="https://i.stack.imgur.com/LeIP4.png" alt="Image with adjustment suggested by Yves Daoust"></a></p>