Fun note: A children's guide to comparametric equations (simple experiments one can do at home to understand comparametric equations) is included in this directory, in the file, photocell.pdf (requires a cheap photocell, ohm meter, and lamp). It is not necessary to read this paper, to do the lab, but it may help by providing a fun and creative interpretation of comparametric equations.
You may submit this assignment as a gzipped tar file which contains a .tex file and the associated .eps files. As long as the document compiles, this will most likely make the T.A. marking this assignment most happy. Otherwise, a postscript file which may be read with gv is acceptable, or a .pdf file which is viewable using xpdf is also acceptable. Send these files to corey@eyetap.org.
For this lab, the following two images [1][2] will be used:
The first image has an exposure time of 1/16th of a second, the second has an exposure time of 1/8th of a second. If you click on the images, you will get the full-sized versions. Since the full size versions contain more data, it is preferable to use them. (Better statistics result from the use of more data.)
Once you have the full sized images, you may want to decompress the jpeg images to get uncompressed ppms using a program such as djpeg. For now, you may also want to convert the images to greyscale, or possibly only use the green channel. To understand why comparametric research is important in image processing, consider the first question on this assignment:
Be creative about this. Describe the intent of your method in your report, as well, hand in the octave code to carry out the method. Include the resulting image as well as a difference image and the sum of squares error. To do this effectively, what who need to devise is some function which will take pixels from the first image and make them approximate the corresponding pixels in the second image. The function may be as simple as a scalar multiplication. However, you are free to make the function any location independent function you want to bring the pixels into a better approximation of the second. The location independence restriction requires that the function act only on pixel values, irrespective of where they are located. This is just to prevent doing something like merely selecting the second image as your result. Therefore your algorithm must work on other image pairs that you have not yet seen. The TA will test your algorithm on another pair of images (taken with the same camera, one at 1/16th of a second, the other at 1/8 of a second) that you have not yet seen. The function should work on any two images taken from the same camera, as long as the exposure difference is the same as the two in this assignment.
Don't spend too much time on this question, it's meant to simply outline the nature of the problem. The later questions will take more work, time, and thought.
Consider this really simple example, using a twelve pixel camera (four pixels across and three down). We have the following 3x4 images (shown in pixel values). The second one is from a 12 pixel camera having a doubled exposure time to the same subject matter:
image 1:
| 1 | 3 | 2 | 3 |
| 3 | 2 | 1 | 2 |
| 0 | 0 | 2 | 0 |
image 2:
| 2 | 3 | 3 | 3 |
| 2 | 2 | 2 | 2 |
| 0 | 1 | 3 | 0 |
To understand what a comparagram is, consider the following example:
A comparagram is formed by first creating an N by N matrix where N is the number of possible greyscale values in the image (usually this is 256 however, in this toy example, it will only be 4). First, the matrix is filled with 0's. Now consider the first pixel in the first image, in location (1,1). In the first image the value of this pixel is 1. In the second image with the doubled exposure, the value of the pixel in the same location is 2. Therefore, in the comparagram's (1,2) entry, we add 1. We continue in this way, accumulating entries, for all the pixels in the images, giving the following comparagram:
| 2 | 1 | 0 | 0 |
| 0 | 0 | 2 | 0 |
| 0 | 0 | 2 | 2 |
| 0 | 0 | 1 | 2 |
From question 2, you should see that the data falls along a ridge. This fuzzy ridge defines an underlying "comparametric function". The fatness of the ridge is due to noise and errors which make understanding the relationship between the two exposures more difficult. The noise and errors form a cloud of data around this ideal function. If there were no noise, quantization error, pixel co-dependence, etc. then theoretically, all we would need, would be narrow ridge, or simple functional relationship between the two exposures.
As the comparagram is now with the noise around the underlying function, processing the comparagram becomes quite difficult. For this reason, we need to recover the underlying function. Some reasonable method to recover this function from the comparagram is needed. To make the later computations easier, each row of the comparagram may be slenderized into single points. This transforms the comparagram into a "comparametric function", which is also sometimes called a "comparagraph". If the above comparagram was slenderized, we might get the following comparagraph:
| 1 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 |
| 0 | 0 | 1 | 0 |
| 0 | 0 | 0 | 1 |
Use some reasonable method to slim your comparagram to convert it to a comparagraph. There exist many statistical methods to solve this problem. Through experimentation, it has been shown that marginalization is the most effective method to recover this function. Marginalization of a comparagram is covered in a paper which you may download here. Unfortunately, marginalization can be a difficult process to understand quickly. Easier methods include simply selecting the biggest entry in each row (or first moments along each row, which is a little better but not as good as marginalization). You do not have to use marginalization, unless you want to impress your TA.
Also, the matrix form which has been used up to now can be a confusing manner in which to view the comparagram. Using octave, rotate the matrix such that the top left corner of the matrix is in the bottom left corner. If you view the comparagram in this manner, it's easier to see the comparagram as a function, where the origin is in the bottom left corner (just like what you are probably used to). Now, look at individual columns of the comparagram. Before our rotation, these were matrix rows, now they are columns. We want to reduce each column to a single value. Note that even though the values represented in the comparagram are integers, our final resulting value need not be an integer. For example, if you were to use a method of first moments on each column, the number you would get would not be an integer, but this is fine.
Print out your rotated comparagram (once again compressing it to pixel values). Also, print out your comparagraph (slenderized comparagram). Ideally, you want to show the comparagraph on top of the comparagram. This will show how well your algorithm slenderized the data. Also submit your slenderization code and a small explanation of the intent of the algorithm (a few sentences).
At this point, you may be wondering what question 1 has to do with comparagrams. Consider what is happening with the camera, light, and our data. Pixels are non-linear. If we could find a way to linearize the pixels, they would be easier to deal with.
Lets now consider a simple model for a digital camera. Incoming light passes through one or a series of lenses and strikes a sensor array. We won't worry about the function of the lenses. For now all we care about is the sensor array.
Consider first one sensor in the sensor array of a camera, and assume that it represents 1 pixel. If no light falls on the sensor, we expect that the resulting pixel value will be zero. We also assume that if much light is collected by the sensor, we will get a value of 255. Unfortunately, the sensors used in cameras do not respond exactly like our eyes (i.e. are not photometric) or respond uniformly to all wavelengths of light (i.e. are not radiometric). This means the data collected by the sensors needs a new unit of measure which shall be referred to as a photoquantity. It is known that photoquantities (q) are linear. After the data is collected by the sensor, a camera response function is applied to the photoquantities to compress the dynamic range. The value is then quantized to form a pixel value.
It is logical to think that the camera response function is monotonically increasing. For example, if the amount of light striking a particular sensor is increased, we do not expect the resulting pixel value to lower. For this reason, the camera response function (f) must have an inverse. We could apply this function f inverse to the pixel values of the image to get photoquantities. Such an image which has had f inverse applied to it is called a portable lightspace map. A portable lightspace map (PLM) is usually represented as IEEE double-precision values, and as mentioned, these values are linear in the photographic quantity, q.
Consider what a comparagram is. From the viewpoint of the sensors, some amount of light has struck a particular sensor. The camera response function is applied to get the imagespace picture (the jpeg image). If we call the camera response function, f, and the vector of photoquantities from the sensor q, then the first uncompressed image (our ppm image) is f(q). The second picture is of the same subject matter, but the exposure time is twice as long. Thus, the photoquantimetric values from the first image have been doubled or we have multiplied the original vector of photoquantities by 2 to form 2q, then the camera response function has been applied. The second image is therefore f(2q). This means that the comparagraph (slimmed comparagram) which was computed previously is actually a "meta-function" of f(q) against f(2q). Such a functional relationship between a function f(q) and a function f(kq), for some scalar constant, k, where the relationship does not depend on q, is called a comparametric equation. For example, the straight line of slope sqrt(2) given by g(f) = sqrt(2) f (where g(f)=f(2q)) is a comparametric equation with a solution given by f = sqrt(q).
Solve the comparametric equation g=af+b, for k=2. Verify that your answer is a solution to this comparametric equation. Is the solution unique? Explain.
(What this question means is that a plot of f(2q) as a function of f(q), is a straight line that does not necessarily pass through the origin, so it's a slight generalization of the above example.)
(Straight lines are often used in comparametric theory, e.g. Google search "comparametric function" returns paper http://iul.eng.fiu.edu/candocia/Publications/Barros_ICASSP2002.pdf which uses various piecewise straight lines to approximate comparagrams, but the above question in this lab is a lot easier because you're just using one straight line for the entire comparagram, so don't get too distracted by the complicated nature of Barros_ICASSP2002.pdf).
This sort of function composed of functions for which the original function f is unknown is termed a first order function equation, or in this context a comparametric equation. Finding closed form solutions for f can be very difficult. Also, just as in solving differential equations of integrals, usually a family of functions is found. Interestingly, the same proof using measure spaces, measure theory, connected sets, etc. which shows the existence of solutions to differential equations may be applied to show the existence of solutions to comparametric equations. However, this is not needed for our problem. For the purpose of bringing pixels to photoquantimetric values, all we need is a numerical solution.
Right now, the comparagraph which you have derived is only defined at discrete points. For deriving a solution for f, it is helpful to define f on all values in the range [0,255]. You may use a spline using the the 255 data points you have in the comparagraph. Warning, the next question is quite difficult and is therefore a bonus question. You will only need to use splines for interpolation if you are going to attempt the next question. If you are indeed going to try the next section, you will want to become familiar with the octave-forge functions spline and ppval.
What is needed is to somehow recover f and f inverse from the comparagram. We assume that f(0)=0. Now, since photoquantities are linear and we have only not assigned what one photoquantigraphic unit shall be, pick some distance along the x-axis of the comparagram and assume that it is one photoquantigraphic unit. If you have interpolated the comparagram, then it makes sense to use machine epsilon, however, in practice larger values such as x=1 have given smaller overall errors. Thus f(1) is this value you choose along the x-axis. If we look at the corresponding y-value, this must be f(2). This is simply because the comparagram is a plot of f(q) against f(2q). Now, we may also find f(4). This is done by finding f(2) along the x-axis and finding the corresponding value along the y-axis. We continue in this manner to get f(8), f(16), .... Note that this procedure will actually give you f inverse as we are starting with some initial value of q. Chapter 4 of the textbook which is on reserve in the engineering library (the library on the second floor of Sandford Fleming building), provides much more information on unrolling.
Write an octave script to unroll the comparagraph from question 3. Since often you may want intermediate values, you may want a spline which interpolates the comparagraph. Also, once you have unrolled enough values to have f(x)=256, use an interpolating spline to get intermediate values. From this interpolating spline, get values from the spline using a simple binary search such that you have a lookup table for each possible pixel value.
The lookup table should look something like:
| |pixel values| | photoquantimetric value| |
| 0 | 0 |
| 1 | q_1 |
| 2 | q_2 |
| 3 | q_3 |
| 254 | q_254 |
| 255 | q_255 |
Submit this table as well as the octave code you used to unroll the comparagraph.
If the unrolling of the comparagraph is too difficult, you may simply use this pre-calculated lookup table, available here
Now that the lookup table for f inverse is available, we may use it to convert the first image from pixels (imagespace) to photoquantigraphic values (photographic values). This transformation is referred to as taking imagespace values into lightspace values. If we apply this transformation, we may use the photoquantities to retroactively change the exposure of the camera.
Use the lookup table derived in the previous question to bring the first image into lightspace. Multiply these values by 2 and apply f to the photoquantimetric values by doing a binary search (or some kind of search) on the lookup table. This should give you an image which accurately approximates the second image. Compute the difference image of the newly generated image and the second image, and the sum of squares error, as was done in question 1.
Assume there exists two images similar to the two used in this lab, each of which the exposure time is known or has been derived. In one image, the exposure is low and shows bright areas such as the sky with much detail. In the other image, the exposure time is long and shows dark areas with great detail. Propose a method to combine ("cement") these two images together. This is a generalization of the signal averaging we learned about in lab1, because now we are averaging differently exposed signals. Implement the method, and show your results.