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.
Code is as follow
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)
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.
Whole Code is as follow
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 < 0 else maxInd-5)
indexN = int(maxVal+5)
percentAtDark = (maxVal / totalPixels )*100
darkSum = np.sum(histr[indP:indexN])
percentDark = (darkSum / totalPixels )*100
if (percentDark > dartThreshold) and (maxInd < 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)
dartThreshold can be set 60 or 70.
For gamma correction you can look at the end of this page
Can anyone suggest any better method or improvement in this code? Any suggestions are more than welcome.