CS 332

Color Lab

Due: Tuesday, December 12

When discussing the Retinex theory of color vision proposed by Edwin Land, we noted that the final reconstruction of surface reflectance from its derivative requires an additional assumption about the distribution of reflectance in the real world. One possible assumption, sometimes referred to as the White World Assumption, is that the brightest region of the image is white. A second possibility, known as the Gray World Assumption, is that the average of the colors in the scene is gray. These assumptions can be used to create simple algorithms to "correct" color images that have been taken under light sources with a skewed spectral composition, or under low lighting conditions. This process is known as white balancing. In this lab, you will first write two MATLAB functions to perform white balancing using these two assumptions, and then apply these functions to some sample images and reflect on what are the conditions under which these strategies perform well, as well as conditions in which they fail to improve image quality.

To begin, download the /home/cs332/download/colorLab folder, which contains 9 RGB color images and the start of a script named colorTest.m. Open MATLAB and set the Current Directory to this folder. View and run the colorTest.m script, which loads four images with differently colored books on a floor, taken under different lighting conditions.

(1) whiteBalance

Write a MATLAB function named whiteBalance that takes an RGB color image as input and returns a modified image that has been white balanced, by applying the White World Assumption. For each individual color component (red, green, blue), this function should determine the maximum intensity value Imax and then adjust the values within each component by multiplying these values by 255/Imax. When your whiteBalance function is complete, uncomment the code statements in the colorTest.m script, to apply this function to the four images of books and display the results.

Important Tip! Inside your whiteBalance function, first convert the input image to floating point values using the double function, and convert the result back to 8-bit values at the end, using the uint8 function.

(2) grayWorld

Write a MATLAB function named grayWorld that takes an RGB color image and an optional second input, gray, that is a number between 0 and 255. Thus function should return a modified image whose colors have been adjusted using the Gray World Assumption. This function should incorporate two possible strategies to implement this assumption:

  1. If the user supplies a value for the second input, gray, then the values within each color component (red, green, blue) should be multiplied by gray/Iavg, where Iavg is the average of all the values within this color component.
  2. If the user does not supply a value for the second input, then the values within the red and blue color components should be multiplied by avgG/Iavg, where avgG refers to the average value of the green component, and Iavg is the average of the red or blue components. In this way, the green component remains fixed and the red and blue components are adjusted to have the same average value as the green component.

Important Tips!

  1. Similar to the whiteBalance function, convert the input image to floating point values at the beginning and convert the result back to 8-bit values at the end.
  2. The nargin function (called with no inputs) returns the number of input values entered by the user when calling the function, and can be used to implement optional inputs.

(3) Testing

Add code to the codeTest.m script file to do the following (cut-and-paste code to save time!):

  1. Apply the grayWorld function to the four books images, with no second input supplied, and display the four results in a 2x2 subplot in a new figure window.
  2. Again apply the grayWorld function to the four books images, but this time, supply a value of 128 for the second input. Again display the four results in a 2x2 subplot in a new figure window.
  3. Load the four images of bushes and flowers, neutral.png, reddish.png, blueish.png, and yellowish.png, and display the four images in a 2x2 subplot in a new figure window.
  4. Apply whiteBalance to the four images of bushes and flowers and display the four results in a 2x2 subplot in a new figure window.
  5. Apply grayWorld to the four images of bushes and flowers, with no second input, and display the four results in a 2x2 subplot in a new figure window.
  6. Finally, load the keyboard.png image, apply whiteBalance, and apply the grayWorld function twice, once without a second input and then with a second input of 128, and display the original image and three results in a 2x2 subplot in a new figure window.
  7. (optional) Find images of your own that you believe will demonstrate pros or cons of the above strategies for image enhancement.

(4) Reflections

Viewing your results, answer the following questions:

  1. In which cases did the whiteBalance function work well, improving the color composition of the image? In what examples did it not improve the image quality, and why did it not work well in these cases? Are there other scenarios in which you do not think this approach would yield improvement in the color composition or overall distribution of brightness in the image?
  2. In which cases did the grayWorld function work well, improving the color composition of the image? In what examples did it not improve the image quality, and why did it not work well in these cases? Are there other scenarios in which you do not think this approach would yield improvement in the color composition or overall distribution of brightness in the image?

Submission details: Hand in a hardcopy of your code files and a document with your answers to the above questions. Drop off an electronic copy of your code files by logging into the CS file server, connecting to the colorLab folder containing your code file, and executing the following command:

submit cs332 colorLab *.m

(Note that this will only submit the code files, which have the .m file extension, and not the images or any other documents contained in your folder.)