top of page
Search
jamescgonzales

Entry #3 Balance the White

Updated: Jan 30, 2021

Image Enhancements Part1 - White Balancing


As mentioned in the earlier post, thru image processing, images can be enhanced. This post is about Image Enhancement thru White Balancing Algorithms.

You may probably ask, "Why to enhance?"... cause there are times that the photo or image did not appear realistic as we wanted. When the photo appears yellowish, bluish, reddish or, any color seems to overcasts the whole picture.

An example of an image that can be enhanced is the yellowish photo below.

White balancing is the process of removing unrealistic color casts, so that objects which appear white in the real world are rendered white in photo. Below are some of the techniques in adjusting the White balance of the image.


Let's import the required libraries


import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from matplotlib.patches import Rectangle

from skimage.io import imread, imshow
from skimage import img_as_ubyte, img_as_float
from skimage.exposure import histogram, cumulative_distribution

from skimage import img_as_ubyte, img_as_float
from scipy.stats import norm
import scipy.stats as stats

First, let's load the image.

 

1. White Patch Algorithm

White is observed when the values of Red, Green, and Blue channels are maximum. In 24-bit images, it has the value of (255,255,255). The white patching algorithm applies this fact and normalizes or rescaling the image to its maximum value.


We'll it seems nothing changed. Let's see the maximum value per channel or the original image.

From the above, it can be observed that the level already has its maximum value at 255.

If each value is already at 255, why is it still has overcast? Let's check the image's histogram.


Based on the histogram, we can observe that at the 100th percentile, the sensors could have been saturated.


Let's apply white patching per percentile and see the effect on the image:



Based on the result, as set the max on the lower percentile, some pixels in the image become more white or overexposed. Selecting the best percentile is heuristics. We have to note that we could lose information or details on the whitest parts due to clipping.


 

2. Gray-world Algorithm

From the earlier algorithm, we assume that the maximum value is white, but for the Gray-world algorithm, we will assume that the pixels are gray on the average value of the channels.

For this algorithm, we will scale or normalize the image using each pixel's mean or average value.

White plates, white table napkins, But it seeps everything is dark.

Let's proceed to the next algorithm.


 

3. Ground Truth Algorithm

Using this algorithm, selected "patch" from the original image will be used as the "ground-truth" or reference, which we can use to re-scale the image's channel values, whether based on the average(mean) or the max value of the patch.

Matplotlib.patch Rectangle was used to get the patch.


Below are the selected patches to be used on our Ground Truth White Patch Algorithm.

  • patch 1 = Table Napkin

  • patch 2 = Table

  • patch 3 = Spring roll


Let's zoom-in on the patches:

Patch 1 is the lightest, while Patch 2 is the darkest.

Now, let's perform the max and mean white balance adjustment using these patches.



# Max and Mean Adjustment per patch
food_gt_max = []
food_gt_mean = []

for patch_img in patch_imgs:
    i_max = (photo / patch_img.max(axis=(0, 1))).clip(0, 1)
    i_mean = ((photo * (patch_img.mean() /
                photo.mean(axis=(0, 1)))).clip(0, 255).astype(int))
    
    food_gt_max.append(i_max)
    food_gt_mean.append(i_mean)

Max adjustments

Rescaling the image using the patch's maximum value shows over-exposure using a darker patch. This result is explainable since we are scaling the image's channel values to the whitest of the darker patch.


Mean adjustments

For the mean white balance adjustments, the darker the patch, the darker the adjusted image becomes. In contrast, the lighter the patch, the brighter the modified image.

 

White Balancing Techniques Comparison

Below are the images that I see have the best adjustments.


In summary, white balancing techniques in image processing can help enhance the images, particularly those images with color overcasts.

Rescaling the channel's value based on either the mean or max values can significantly enhance the image.

Ground truth and the image histogram can be useful in the white balancing process of the image.


42 views0 comments

Recent Posts

See All

Comentários


Post: Blog2_Post
bottom of page