title: Auto selection of Gamma value for image brightness
body: <p>I have a sequence of images or live video and I want to check if the image is darker or brighter, then adjust the brightness using gamma correction i.e. Image ^ gamma. gamma = 1 do nothing and smaller than 1  value of gamma, bright the image and greater than 1 makes it dark. I have to give the gamma value manual for different types of images or videos.</p>
  <p>Code is as follow</p>
  <pre><code>lookUpTable = np.empty((1,256), np.uint8)
  for i in range(256):
      lookUpTable[0,i] = np.clip(pow(i / 255.0, gamma) * 255.0, 0, 255)
  res = cv2.LUT(image, lookUpTable)
  </code></pre>
  <p>I want to find gamma value automatically checking the image. I tried to find gamma value to brightness using histogram but it seems not good.</p>
  <p>Whole Code is as follow</p>
  <pre><code>        histr = cv2.calcHist([image],[0],None,[256],[0,256]) 
              
      totalPixels = image.shape[0]*image.shape[1]
  
      maxInd = np.argmax(histr) 
      maxVal = histr[maxInd]
      indP= int(0 if maxInd-5 &lt; 0 else maxInd-5)
      indexN = int(maxVal+5)
      percentAtDark = (maxVal / totalPixels )*100
      darkSum = np.sum(histr[indP:indexN])
      percentDark = (darkSum / totalPixels )*100
      
      if (percentDark &gt; dartThreshold) and (maxInd &lt; 127): 
          gammaList = np.arange(0.01,0.9,0.02)
          gamma=gammaList[maxInd]
      else:
          gamma = 1
     
      lookUpTable = np.empty((1,256), np.uint8)
      for i in range(256):
          lookUpTable[0,i] = np.clip(pow(i / 255.0, gamma) * 255.0, 0, 255)
      res = cv2.LUT(image, lookUpTable)
  </code></pre>
  <p>dartThreshold can be set 60 or 70.</p>
  <p>For gamma correction you can look at the end of <a href="https://docs.opencv.org/master/d3/dc1/tutorial_basic_linear_transform.html" rel="nofollow noreferrer">this page</a></p>
  <p>Can anyone suggest any better method or improvement in this code? Any suggestions are more than welcome.</p>