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!

No comments:

Post a Comment