Tuesday, June 22, 2010

Activity 2 : Scilab Basics

Scilab is a high-level scientific programming language that can be used instead of Matlab. Most image processing functions of Matlab is available in Scilab. And the best thing about it is that it's free! No need to break the law and download a crack for the very expensive Matlab!

The first time I got acquainted with Scilab was during our Physics 165 class. I eventually learned some of the Scilab basics like matrix operations through the sample codes we used in class and with google's help. The few Scilab skills that I learned proved to be useful for the research I'm doing on roof identification and tagging. And it is also because of this work that I'm extra enthusiastic to learn more about image processing and how to do it with Scilab.

I first installed the older (4.1.1) version of Scilab but I keep getting an error with the libsip.dll path everytime I use the imwrite() function. I then tried installing the newest (5.2.2) Scilab version, but again I get an error involving the libsip.dll path whenever I try to load the SIP toolbox. I tried looking around the net for some solution but none worked. So I settled with the Scilab 4.1.2 version linked with the 0.4.0 SIP toolbox worked. I haven't encountered any error with it so far.

To practice coding in Scilab, we generated some synthetic images in this activity. As an example, the code for a centered circle was given as shown below.
nx = 1000; ny = 1000; //defines the number of elements
x = linspace(-1,1,nx); //defines the range
y = linspace(-1,1,ny);
[X,Y] = ndgrid(x,y); //creates two 2-D arrays
//centered circle
A = zeros(nx,ny);
r= sqrt(X.^2+Y.^2); //note element-per-element squaring
A(find(r<0.7))>
//scf(1);
//imshow(A, []);
imwrite(A,'centered_circle.jpg');

The resulting centered circle image is shown below. When saving images, the imwrite() function provides an image with a better quality than when we export the result of the imshow() function.

Figure 1. Centered Circle


We then created the following synthetic images:
a. centered square aperture
b. sinusoid along the x-direction (corrugated roof)
c. grating along the x-direction
d. annulus
e. circular aperture with graded transparency (gaussian transparency).

The codes for these and the resulting images are shown below.

For the centered square aperture:
//a. centered square aperture
A = zeros(nx,ny);
A(find((X >=-0.7 & X <=0.7)& (Y >=-0.7 & Y <=0.7)))=1;
//scf(2);
//imshow (A, []);
imwrite(A,'centered_square.jpg');

Figure 2. Centered Square Aperture


For the sinusoid along the x-direction (corrugated roof):
//b. sinusoid along the x-direction (corrugated roof)
S = sin(X*20.0);
Stransposed = S + abs(min(S));
Sscaled = Stransposed./max(Stransposed);
//scf(3);
//imshow (S, []);
imwrite(Sscaled,'sinusoid_x.jpg');

To avoid clipping, the negative values was transposed and all the values were subsequently scaled.

Figure 3. S
inusoid along the x-direction (corrugated roof)


For the grating along the x-direction:
//c. grating along the x-direction
A = zeros(nx,ny);
maxima = [1:nx/10 2*nx/10:3*nx/10 4*nx/10:5*nx/10
6*nx/10:7*nx/10 8*nx/10:9*nx/10];
A(maxima,1:ny) = 1;
//scf(4);
//imshow (A, []);
imwrite(A,'grating_x.jpg');

Figure 4. Grating along the x-direction


For the annulus:
//d. annulus
ran = sqrt(X.^2 + Y.^2);
A = zeros(nx,ny);
A(find((ran <= 0.7) & (ran >= 0.5))) = 1.0;
//scf(5);
//imshow (A, []);
imwrite(A,'annulus.jpg');

Figure 5. Annulus


For the circular aperture with graded transparency (gaussian transparency):
//e. circular aperture with graded transparency
Z = exp(-3*(X.^2 + Y.^2));
Ztransposed = Z + abs(min(Z));
Zscaled = Ztransposed./max(Ztransposed);
//scf(6);
//imshow (Z, []);
imwrite(Zscaled,'circular_gaussian.jpg');

Figure 6. Circular aperture with graded transparency (gaussian transparency)


For these activity, I give myself a grade of 10/10 since I was able to install Scilab, link it with the SIP toolbox and create the asked synthetic images.

I thank Ma'am Jing for giving the code on how to access several rows of an array and BA for the technique on how to avoid clipping of values in the saved images. Danke!

Wednesday, June 16, 2010

Activity 1 : Digital Scanning

There are instances that requires the use of hand-drawn plots. In these cases, only a number of computationally or experimentally determined data points are provided. But what if we need to find values other than these points?

Using our ever reliable friends, Gimp, Paint and Excel, we can reconstruct the digitally scanned hand-drawn plot. By locating the pixel points of the given data and determining the appropriate bias and scaling values, we can now know what the "missing" values are.

The original graph I used as shown below was taken from Physical Review Jul-Dec 1922 p.620. Since the scanned copy was a bit rotated, I first used Gimp to straighten the axis.

Figure 1. Ratio Curve for R (on) / I (on). Original plot taken from Physical Review Jul-Dec 1922 p.20.

To determine the pixel/unit scaling factor to use, I looked for the pixel location of each tick mark in the graph. I then computed for the average pixel length of every one unit in the plot which I then used as the scaling factor. Since the origin of the plot is not exactly at a (0,0) pixel location, I also added a bias in my computation of the reconstructed values.

The bias, scaling factors and equations I used in my reconstruction are:

Bias along x : 21 pixels
Bias along y : 379 pixels
Scaling factor for x : 66.28571 pixels/unit
Scaling factor for y : 64.2 pixels/unit

x_value = [(data_x - bias_x) / scaling_x ] + 9
y_value = [(bias_y - data_y) / scaling_y] + 1

I added 9 and1 in the x_value and y_value respectively since the origin in the original plot is at (9,1).

The reconstructed plot was superimposed on the original graph using Excel as shown below.

Figure 2. Reconstructed plot superimposed on the original graph.

As can be seen in the figure above, I was able to superimpose the reconstructed data points exactly on the location of the original data points. Some parts of the smoothing curve in the reconstructed plot, however, do not perfectly match those in the original graph. But this may have just been due to the differences in the method used in connecting the data points.

I give myself 10 in this activity since there is good correspondence between my reconstructed plot and the original graph.

Special thanks to Millicent Singson for providing me with the hand-drawn graph I used here and for teaching me how to make a picture, the background of a graph in Excel. Danke Milli!!!