White balance is used to remove unrealistic colour casts caused by different light source. It’s easy for human eyes to adjust what we see according to the light source of the current environment but it’s hard for the camera to adjust according to the lighting condition of the room. Sometimes, we refer that as colour temperature.
Colour temperature is correlated with the objects in a room. It depends on the rate of absorbance of light of the objects in the room being photographed. A blackbody is something that absorbs all light not allowing all light to pass through, that’s also the reason why we always say black object absorbs light. On the other hand, white object reflects all light, so it’s often referred as “white light”.
Below shows an a graph representing the relative intensity vs wavelength at difference colour temperature for a Tungsten Lamp (Observe the non-linearity):
Below are the listing of colour temperature based on different environments: [source]
|Color Temperature||Light Source|
|2500-3500 K||Tungsten Bulb (household variety)|
|3000-4000 K||Sunrise/Sunset (clear sky)|
|4000-5000 K||Fluorescent Lamps|
|5000-5500 K||Electronic Flash|
|5000-6500 K||Daylight with Clear Sky (sun overhead)|
|6500-8000 K||Moderately Overcast Sky|
|9000-10000 K||Shade or Heavily Overcast Sky|
There’re few ways to implement white balance. My method is as follow:
- Obtain the histogram of the original image.
- Obtain the Cumulative Density Function (CDF) of that particular histogram.
- The histogram is then stretched based on a discard ratio which is based on the minimum and maximum of the CDF previously obtained. (I used 0.95 as my discard ratio)
- From step 3, we’re able to obtain the maximum and minimum value of our stretching range, thus we stretch our pixel value range with the following formula:
The following is the result of my implementation:
Before white balance:
After white balance:
Source code of my implementation: imgWB