Sunday, October 11, 2009

終わりだ (Activity # 19)


Activity # 19 - Restoration of Blurred Images.. this is the last of the AP 186 activities which means this sem is finally over.. well, not quite but at least it's almost done. xD

In this activity, our objective would be to restore an image corrupted by a blurring function and some noise. This entire activity can be summarized in the form of a flowchart:

In simple terms, just as the objective states, we start by taking an image, then we give it a blur by introducing a blurring function and adding a noise in the Fourier domain, and then finally restoring it using a Minimum Square Error Filter.

Since the equation for a blurred image with T as the length of exposure is defined by the equation:

for adding a blur, the degradation function would then be expressed as (also in terms of T):

As for the noise, we make use of the Gaussian Noise that was already discussed in the previous activity. The operations are performed in the Fourier domain and is described by the following equation:

The filter used for the activity is the Weiner Filter or the minimum mean square error filter given by either this

or this

where K is a constant (alternative when Sn and Sf are unknown).

So that's practically all the physics you'll ever need for the activity--what we need to do now is apply those equations to a test image. For this activity, the I grabbed a photo of Eric Clapton with 'The Fool' guitar from Gibson's site:

The blurred image generated by the degradation function looks like this:

Now, since there are two equations for the filter, we therefore have two reconstructions for the image: one using Sn and Sf (Left) and another one using K=0.01 (Right).
We can see that we have a better image using the Sn and Sf reconstruction. However, we can observe the following trend for various K's (K=1, K=0.1, K=0.01 and K=0.001 respectively:

From the set, we can infer that K approaching integer values is biased towards the blur, while small values of K are biased towards the noise. On a side note, the optimal value is hence in between K=0.1 and K=0.001.


This activity was rather easy compared to the other activities, so I'll give myself a 10. I'd like to acknowledge Neil for his help in this activity.

AP186のアクチヴィチ13が、完了した!! (Activity # 13)

This is probably the most difficult (if not tedious) activity in AP 186. This activity (Activity # 13) is entitled Correcting Geometric Distortions.

Since a camera uses a lens to focus an image to its sensor, lens distortions can be observed. For spherical lenses, there are two common types: Barrel Distortion and Pin Cushion Distortion. Here are the examples for each kind (c/o imagetrendsinc.com):
In order to correct the distortion, we need a reference image with a regularly occuring pattern and visuallize the distortion across the image frame. If we let f(x,y) be the correct ideal image, and g(x,y) be the distorted image, we can assume that there exists a transformation function that mapped f(x,y) to g(x',y'):

The aim would be to invert the transformation in order to obtain our ideal image. Two image properties will be corrected in the process: the pixel locations, and their respective graylevel values.

We start by expressing x'=r(x,y) and y'=s(x,y), where r and s are the transformation functions used to map the ideal image to the distorted image frame. It is good to assume that these functions take the form of bilinear functions, r(x,y) = c1*x+c2*y+c3*xy+c4 and s(x,y) = c5*x+c6*y+c7*xy+c8.

That's basically the physics of this activity as the rest of the steps would make use of matrix manipulations. First, I obtained a barrel-distorted image from imatest.com and cropped it to get this image:

It's cropped for two reasons: one, this activity aims to demonstrate the concepts used in geometric distortion correction and thus a large grid is not advisable; two, since Scilab works faster with small matrices or images. Notice that the centered square on the grid features the least amount of distortion and our ideal grid was generated from the dimensions of that square. Here are the pixel locations of the points of intersection of the distorted and ideal grids respectively:
After obtaining the pixel coordinates for the ideal image, we then solve for the coefficients c1 to c8 by setting up the following matrices for each square (thus making the procedure similar to a moving averages run):

and solving for the coefficients using the following equations:

Once the coefficients are obtained, we get the corresponding pixel locations from our distorted image using the set of transformation equations listed above (x' = r(x,y) = c1*x+c2*y+c3*xy+c4 and y' = s(x,y) = c5*x+c6*y+c7*xy+c8) and map out these pixel coordinates to the ideal image map. For the points in the ideal image plane with no corresponding point from the distorted image, we use interpolation methods (bilinear or nearest neighbor) to obtain their grayscale values. Since I practically patterned out my interpolation routine from Gilbert's source (by the way, Gilbert helped me to get this thing working after we consulted Mandy several times), please refer to his source for the interpolation part.

Here are the results I obtained from this activity:

The images above are for the original image, the bilinearly-interpolated image and the image for nearest neighbor interpolation. Although the image was not fully restored, the bilinearly-interpolated image has better quality than the nearest neighbor-interpolated image.


Once again, I'd like to acknowledge Gilbert and Mandy for helping me in getting my version of the source to work. I'm getting a 9 for this activity.. minus one for the delay but 9 for finishing it anyway.