Tuesday, June 29, 2010

2. Scilab Basics

Scilab makes matrix multiplications very simple. The syntax is very much like the math. With matrices, synthetic images can be generated. A matrix of 1's and 0's form a black and white image (any binary set of values would do). Using more than two values will generate gray-scaled images.

In this activity, basic images were generated from Scilab matrices. I started from the code provided in class. This generates a circular aperture. I changed the number of elements (100 to 200) to improve the resolution of the image. The range was also increased to generate a larger image. This can be used as a mask for a matrix simulation of a light source.

All of the images generated are derived from this code.

[1] nx = 200; ny = 200; //defines the number of elements along x and y
[2] x = linspace(-2,2,nx); //defines the range [3] y = linspace(-2,2,ny);
[4] [X,Y] = ndgrid(x,y); //creates two 2-D arrays of x and y coordinates

[5] r= sqrt(X.^2 + Y.^2); //note element-per-element squaring of X and Y

[6] A = zeros (nx,ny);
[7] A (find(r<0.7) =1;

circular aperture

A small modification of the code will generate a square aperture and an annulus. The white areas represented by 1's. Changing lines [5], [6] and [7] to the following will yield the square and annular apertures.

To generate a square aperture
change [5],[6],[7] to A(find(abs(Y)<1>0.5)) = 1;

To generate an annulus
A(find(r<1>0.5)) = 1;


a square apperture and an annulus

One can use the circular aperture with a simulated light source. Assuming that the beam's distribution is Gaussian, we simply multiply the matrix of the aperture and that of the Gaussian source.

we simply add the following lines of code, omitting [8]
G = exp(-2*X.^2 - 2*Y.^2); //gaussian fxnB = G.*A;
imshow(G,[]);
imshow(B,[]);


Gaussian beam and beam with circular apperture

Gratings can also be made by modifying the square aperture. Vertical gratings were made with the following conditions,

n=.20
A(find(abs(Y)>n & abs(Y)n+.80 & abs(Y)n+1.60 & abs(Y)<2))>

vertical gratings generated by imposing conditions on A along the x-axis

Trying out a sinusoid along the x-axis will generate this image.


Image of a sinusoid along the x-axis

This is generated by changing the conditions of the square aperture

A= sin(X*10);// apply sine function
A = A/max(A); //normalization
imshow(abs (A), []); //avoid negative values

For this exercise, I give myself a score of 9/10. The objectives of the activity are all in here. Minus one point for being late.

Thanks to Theiszm for the Scilab tips. Throughout the duration of the past week I have been (1) attempting to link SIP and then SIVP to Scilab 5 and (2) install SIP and SIVP from binary files to on my Linux. The binary installation took me into the trail of OpenCV and Cmake, very foreign installation requirements for me. I then gave up and took on Ma'am Jing's advise to use Scilab 4.1.2 and SIP. The linking trick sure works. For the later versions of Scilab, the problem seems to be that it can't find the file libsip.dll. I hope the SIP and SIVP link will workout in the later versions.

Wednesday, June 16, 2010

1. digital scanning: of french curves and the non-existence of automated data acquisition

Before the advent of Excel, MATLAB, Python, LabView and the whole arsenal of scientific computing software at our disposal, our predecessors plotted their data manually on paper and smoothing was done with the almighty french curves.

Applied Physics 186 or Instrumentation 2 is a course on Digital Image Processing. For our first activity, we searched for old hand drawn plots of scientific data. The aim of the activity is to reproduce the plot using a spreadsheet software.

Figure 1 is a plot of Butyl rubber's continuous and intermittent stress relaxation at 130 Centigrade and 50% elongation. The y-axis is the ratio of stretched and unstretched length. The x-axis is time in hours. For this activity, the plot of interest is the continuous stress relaxation (Fig. 2)
.


Figure 1. Hand-drawn plot of Butyl rubber's stress relaxation


Figure 2. Original plot was cleaned using MS Paint to display only the plot of continuous stress relaxation

To reproduce this plot, the numerical values of each data point was determined. As a digital image, the pixel location of each data point can be easily obtained. I used Inkscape, a vector graphics
program, to determine each pixel location.

To convert each data point to numerical values, one must determine the conversion factor for the x and y axes.

conversion factor (CF) = (Nmax-Nmin)/(Pmax-Pmin)

For the y-axis,

CF = (10-0)(340.39-109.24)


where 340.39 and 109.24 are the pixel locations corresponding to 10 and 0 along the y-axis. The same procedure is done for the x-axis.

The numerical value for a data point is simply,

N = CF x (P-Pmin)

Armed with the numerical value of each data point, a reproduction of the original plot can be easily generated using the OpenOffice Calc and applying cubic spline smoothing.


Figure 3. Original plot and reproduction. Both axes are retained to ensure that the plots are properly scaled

The superposition of the reproduction and original was easily generated with David's tip from Dr. Soriano's blog. Thanks to Dr. Soriano for the advice and the CS Lib staff for allowing me to use the journals without finishing my registration.

I give myself a 10 in this activity. I finished it on time and I like the final plot. Conversion was successful, with small deviations that can be from the image being skewed when it was photocopied or scanned. Some difficulty was encountered during the superposition of the reconstruction with the original data. One must remember to restart their OpenOffice Calc after uploading the original image as bitmap area fill.

References:
1. M. D. Stern and A. V. Tobolsky. Stress-Time-Temperature Relations in Polysulfide Rubbers. Journal of Chemical Physics. Vol. 14, No. 2. 1946.
2. E. David. Inserting Images as Background for OpenOffice Calc Graphs. 2008.