@Charles Ma has suggested to use the Gimp
white balance algorithm. In python
and numpy
this could look like this:
# white balance for every channel independently
def wb(channel, perc = 0.05):
mi, ma = (np.percentile(channel, perc), np.percentile(channel,100.0-perc))
channel = np.uint8(np.clip((channel-mi)*255.0/(ma-mi), 0, 255))
return channel
image = cv2.imread("foo.jpg", 1) # load color
imWB = np.dstack([wb(channel, 0.05) for channel in cv2.split(img)] )
it's fast, simple and provides pretty decent results