Tuesday, June 22, 2010

Activity 2: Basic Scilab and Images

In this activity, we used scilab as the programming language to be used in the class. Basic matrix operations in scilab, namely Addition, Multiplication, Element per Element multiplcation, were studied and used for creating synthetic images.

Initializations were done using the following codes:

nx = 500; ny = 500; //defines the number of elements along x and y

x = linspace(-1,1,nx); //defines the range

y = linspace(-1,1,ny);

[X,Y] = ndgrid(x,y); //creates two 2-D arrays of x and y coordinates

In figure 1, a centered circular image and a centered square image were created to simulate an aperture. The source code used is as follows:

//centered circle aperture

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

A = zeros(nx,ny);

A(find(r<0.7))>

//scf(1); imshow(A,[]);

imwrite(A,'C:\Users\Micent\Documents\Applied Physics\acad 2010-2011 1st sem\App Phy 186 - Instrumentation Physics II\Activity 2\centered_circle_aperture.png');

//centered square aperture

A = zeros(nx,ny);

A(find(((abs(X)<0.7))&(abs(y)<0.7)))>

//scf(2); imshow(A,[]);

imwrite(A, 'C:\Users\Micent\Documents\Applied Physics\acad 2010-2011 1st sem\App Phy 186 - Instrumentation Physics II\Activity 2\centered_square_aperture.png');




Circular Aperture

Square Aperture
Figure 1. Simulated Apertures



In figure 2, a corrugated roof image was created using a sinusoid along the x-direction. A translation in the function was done to remove negative values in the matrix. In figure 2.a, observe that the image does not show a smooth transition of colors. That is why, normalization was done to produce a smoother image.

//sinusoid along the x-direction

A = zeros(nx,ny);

A = 2*sin(10*X);

A = A + abs(min(A)); //offset

A = A/max(A); //normalize

//scf(3); imshow(A,[]);

imwrite(A, 'C:\Users\Micent\Documents\Applied Physics\acad 2010-2011 1st sem\App Phy 186 - Instrumentation Physics II\Activity 2\sinusoid_along_X.png');



(a)
(b)
Figure 2. Simulated sinusoid along x-direction


Figure 3 shows an image of grating in the x-direction and an annulus. The source code used is written below:

//grating along the x-direction

A = zeros(nx,ny);

A(find(abs(X)<0.9))>

A(find(abs(X)<0.7))>

A(find(abs(X)<0.5))>

A(find(abs(X)<0.3))>

A(find(abs(X)<0.1))>

//scf(4); imshow(A,[]);

imwrite(A, 'C:\Users\Micent\Documents\Applied Physics\acad 2010-2011 1st sem\App Phy 186 - Instrumentation Physics II\Activity 2\grating_along_X.png');

//annulus

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

A = zeros(nx,ny);

A(find(r<0.7))>

A(find(r<0.4))>

//scf(5); imshow(A,[]);

imwrite(A, 'C:\Users\Micent\Documents\Applied Physics\acad 2010-2011 1st sem\App Phy 186 - Instrumentation Physics II\Activity 2\annulus.png');



(a) grating along x-direction

(b) annulus
Figure 3



In figure 4, a circular image with graded transparency was created. A 2-D Gaussian function f(x,y) = a*exp(-(x^2+y^2)/2*c^2), where a, b, c>0 and a = 1/((std dev)*sqrt(2pi)), b = mean, and c = std dev. Observe that the gradation in figure 4.a is not observable. Again, a normalization was done to show the gaussian transparency and its peak.

//circular aperture with graded transparency (gaussian transparency)

u = 0; //mean

v = 10; //variance

a = 1/sqrt(v*2*%pi);

b = u;

c = sqrt(v);

//A = zeros(nx,ny);

r = (X-b).^2 + (Y-b).^2;

A = a*exp(-r/2*c^2); //equation taken from: http://en.wikipedia.org/wiki/Gaussian_function

A = A/max(A); //normalize

//scf(6); imshow(A,[]);

imwrite(A, 'C:\Users\Micent\Documents\Applied Physics\acad 2010-2011 1st sem\App Phy 186 - Instrumentation Physics II\Activity 2\circle_aperture_gaussian.png');


(a)
(b)
Figure 4. Circular Aperture with
graded Gaussian transparency


In this activity, I give myself 10 points since I was able to produce all the required images. Also, I was able to do some additional computations that would improve the quality of the images produced. Thanks to Cheryl Abundo for clarifying some points in constructing the sinusoid and to Bernard Racoma for explaining the need to do the offset in the graph.














No comments:

Post a Comment