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.
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.
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:
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.
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!
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.
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.
Add code to the codeTest.m
script file to do the following (cut-and-paste
code to save time!):
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.
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.
neutral.png
,
reddish.png
, blueish.png
, and yellowish.png
, and
display the four images in a 2x2 subplot in a new figure window.
whiteBalance
to the four images of bushes and flowers and
display the four results in a 2x2 subplot in a new figure window.
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.
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.
Viewing your results, answer the following questions:
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?
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.)