Automatic Color Balancing For A PC-Based Video Camera

 Yingmei Lavin    Alice Liu


Introduction

In this project, we investigate the methods of automatic color balancing of an image taken by a digital camera made by a local startup company Silicon Vision. The camera's CCD array has 512 X 480 pixels, and its color mosaic consists of cyan, yellow, magenta, and green. Under normal operations, controls such as exposure, saturation, gamma value can be externally controlled, and color balancing is accomplished by choosing the lighting condition to be home, office, or natural. Since the objective of the project is to automatically color balance images, raw pixel values of the CCD were obtained. The R, G, and B values of each pixel were extracted from linear combinations of the cyan, yellow, and magenta values. This step was performed assuming a linear response of the raw pixel values with incident light intensity. After the reconstruction of the R, G, and B channels from the raw pixels, we checked for the proper exposure and filtered out noise. An algorithm was used to determine the proper scaling of the R, G, and B values for automatic color balancing. The balanced image undergone the above processes still appears to be not completely white balanced. The reason was found to be the very nonlinear response between the raw pixel values and the incident light intensity.


Raw image extraction

Task: Obtain raw pixel values directly from the CCD array.

Difficulties:

  • Images are taken from an 16-bit iVision video camera with only 8-bit useful information.
  • No information about whether the data is stored in the higher or lower 8-bit in a 16-bit data entry.
  • Raw images are saved in 16-bit bmp file format with unknown number of bytes for header information.
  • Solutions:

  • Find the total number of bytes contained in the raw image.
  • Find the width and height information.
  • Find the number of bytes devoted for data entries using 2*width*height
  • Find the length of header information by subtracting the number of bytes for data entries from the total number of bytes in the file.
  • Read in the entire data entries and mask the higher and lower 8-bit individually.
  • View the corresponding image to decide which is the true image.

  • Color matrix reconstruction

    The color table below is the color filter layout in front of the CCD array:

    figure 1.

    In order to view the color image on the computer display, we need to recover the r, g, b matricies. The raw image from the camera represents the gray level for each pixel. So to get the red, green and blue values for each pixel, we need to use the cyan, yellow and magenta values from the neighboring pixels. The formulas for convertion from Cy, Ye and Mg to R, G and B are given below:

    R = Ye + Mg - Cy

    G = Ye + Cy - Mg

    B = Cy + Mg - Ye

    Problem:
    This method will cause problems when the intensity signals from CCD array are not really linear.


    Automatic color balancing algorithm

    Each pixel in the raw image contains the calculated RGB values without gamma correction. Before any color balancing can be done, it is necessary to check if the image is underexposed or overexposed, and to reduce false peak values introduced by noise during the reconstruction of the raw RGB image. We make sure that the image contains less than 1% of saturated pixel values to prevent oversaturation, and that it contains less than 90% of the pixel values below 128 to prevent undersaturation. Due to the calculations of the raw RGB values, some noise peaks are introduced into the image. To reduce the effect of the noise peaks, we compare each pixel value with the mean value of its nearest 64 pixels. If the peak pixel value in the 64 pixel window is much greater than the mean, it is recognized as a noise pixel and the value is replaced with the mean value. Automatic color balancing is accomplished using an algorithm developed in US Patent #5504524, as shown in figure 2. We construct an 8X8 pixel window and convolve with the entire image, so that peak average values within the 64 pixel window and the window locations can be found. The peak average green value is denoted G(p), global peak average red value R(pw), and global peak average blue value B(pw). Since there is frequently a relatively high level of green component occurring in scenes under more common light sources such as natural, tungsten, and fluorescent lights, we choose to search within the peak average green window to find the peak red value R(p) and peak blue value B(p). We then compare the red and blue components with the green component. The objects inside the peak average green window can be either white, mostly green, mostly yellow, or mostly cyan. Inside the peak average green window, if the peak red value R(p) is higher than G(p), then it is decided that there is an excess of red, and that the image is imbalanced. The red component is then scaled down by a factor determined by the ratio of G(p)/R(p). If the peak red value R(p) is less than G(p), then the global peak average red value R(pw) is compared with the G(p) value, since the peak red value inside the peak average green window may not accurately represent the real peak value in the image. The lower red peak value inside the peak green window can be caused by either color imbalance or a shortage of red in the scene (for example, a cyan panel). Assuming the objects in the the entire image are diverse in terms of color reflection and emission, then if the global red peak R(pw) is also lower than G(p), then it is decided that the object within the peak green window is white, and the image is imbalanced. The red component is then scaled up according to a factor determined by the ratio of G(p)/R(wp). If the global red peak R(pw) is not lower than G(p), then it is decided that the object within the peak green window is mostly green or cyan, which have low red reflection. In this case, the image is judged to be balanced in red. The treatment of the blue component is exactly the same as that of the red component. In the case where the entire image is lacking substantial green, another more dominating color should be chosen, so that the green component and the third color component can be compared to this chosen color. After the image is properly balanced, gamma correction is added.

    figure 2.



    Cases of color failure:
    The algorithm should work well under most situations with various lighting conditions, but like all color balancing algorithms, it is not failure proof. In our case, color failure occurs when the image consists of mostly objects that are monochromatic. For example, an image consisting of only a completely white object under green lighting is mistaken to be balanced, because the algorithm assumes that the object is green. This is a difficult case to treat, because under similar circumstances even the human visual system can be tricked. Another possibility for color failure is mixed lighting conditions in the same image. This problem can be potentially avoided if color balancing is performed separately in different subsections of the image. However, the time required for this process may be longer.


    Implementation

    Source codes of the image extraction, color matrix reconstruction and color balancing are listed as following:

    A list of c++ files:

  • main.cpp - Interface to extract raw images and reconstruct the rgb-color matrix.
  • pgm_io.cpp - I/O file to read in and write out ppm and pgm files.
  • bmp_read.cpp - I/O file to read in bmp file.
  • get_rgb.cpp - Reconstruction of rgb-color matrix

    A list of header files:
  • pgm_io.h
  • bmp_read.h
  • get_rgb.h

    A list of matlab function files and scripts:
  • pnmread.m - A function to read in pgm files.
  • readnumber.m - A function needed in pnmread
  • readstring.m - A function needed in pnmread

  • findPeak.m - A script to read in color matrices and find the averaged peaks for red, green and blue. Also red and blue peaks within the high green peak area.
  • rbgain.m - A script to compare the red and blue peaks within the green peak area to decide whether it's necessary to decrease their gains; also compare the red and blue peaks in the whole image to the green peak to decide whether it's necessary to increase their gains.
    Results:

    gray pixel values

    red image

    green image

    blue image

    reconstructed color image

    color balanced image


    Discussion:

    Discussions As shown in the calculated R, G, and B images, there are some noise pixels having abnormally high values. These noise pixels were introduced by overflowing and underflowing problems during the calculations of the RGB values. By convolving the R, G and B images with a 8X8 filter first, then looking for the peak values in the smoothed images, we eliminate the accidental high values caused by noises. The color balanced image shown above still has too much red in the backgound. The color balancing algorithm has found the peak average green area to be in the bright part of the white sweater with G(p) > R(p) and G(p) > B(p). In addition, G(p) > R(pw) and G(p) < B(pw). According to the algorithm, the following decisions were made: red is imbalanced, and is increased according to the ratio of G(p)/R(pw); blue is unchanged. However, since we know that the peak green average area is white, it is not physically possible to have blue peak levels higher than the green peak level! So we scaled the blue component to make a white surface. The entire image was then scaled to bring up the brightness. However, the resulting image still does not look right! Because of the impossible behavior of the blue peak levels compared with the green peak level, we suspected that our raw pixel values were not linear with respect to the incident light. If this were true, then our method of reconstructing the RGB values from the raw data would not be valid. We placed a neutral density filter in front of the camera and compared the values at the same pixel locations before and after the insertion of the filter. Below is an example:

    b(100:104,100:104)
    ans =
    33 33 37 37 33
    33 37 37 29 29
    33 33 33 29 29
    37 33 33 29 29
    37 37 33 33 37

    a(100:104,100:104) ans =
    25 26 33 33 29
    29 25 29 36 35
    26 26 34 37 35
    29 29 33 33 32
    30 34 38 34 33

    c=b(100:104,100:104)./a(100:104,100:104)

    c =
    1.3200 1.2692 1.1212 1.1212 1.1379
    1.1379 1.4800 1.2759 0.8056 0.8286
    1.2692 1.2692 0.9706 0.7838 0.8286
    1.2759 1.1379 1.0000 0.8788 0.9062
    1.2333 1.0882 0.8684 0.9706 1.1212

    where b is the image without the neutral density filter and a is the image with the neutral density filter. The ratio of b/a is displayed in c. The filter should cut off 3 times the light intensity, and the ratios of pixels in b and a should be around 3, but c clearly shows this not to be the case! Therefore, we have concluded that the camera response is nonlinear. Because of this, the calculated RGB raw values are no longer valid, and the subsequent color balancing cannot work properly to correct this problem.


    Last modified: Thu Mar 12 17:00:17 PST 1998