AP 186 Activity 2: Scilab Basics

Scilab is an open-source software which is effective for array and matrix operations [1,2]. Therefore, it is also an effective programming language for image processing like Matlab.

For this activity, we were tasked to install Scilab 5.3.3 from http://www.scilab.org and the Signal Image and Video Processing (SIVP) module from http://www.scilab.org/products/modules/scm. Primarily, Scilab was installed in our computers. To check whether it was successfully installed, a simple snippet was executed:

If this snippet works, then the next step was to install the SIVP module. For my case, the downloaded SIVP_0.5.3.1 folder was copied to C:\Program Files\scilab-5.3.3\contrib  since my Operating System is 32-bit Windows 7 Starter. The Module Manager – ATOMS button in the standard toolbar was clicked followed by the SIVP_0.5.3.1 in the Toolboxes tab of the Scilab console.

To check whether SIVP works, the imshow command should be working. The algorithm which simulates a centered circular aperture or a centered circle in 2-dimensions was given. Once this algorithm displayed the corresponding image, then the SIVP module was properly installed. The Scilab code is shown below and the corresponding output image is shown in Figure 1.

//centered circular aperture
nx = 100; ny = 100; //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
r= sqrt(X.^2 + Y.^2); //note element-per-element squaring of X and Y
A = zeros (nx,ny);
A (find(r<0.7) ) = 1;
imshow (A);

Figure 1. Simulated circular aperture

The next procedure is to simulate other algorithms which demonstrate optical phenomena in imaging. The first is a synthetic image of a centered square aperture. The Scilab code for this image was actually a modified version of the code for the centered circular aperture. At first, I was thinking of using for loop but when I heard from Ma’am Jing that Scilab is a good programming language because it can execute algorithms shorter than other language, the abs function came to my mind. From that time on, I thought of ways on how to optimize my codes. The code and the image for the square aperture is shown below. A line preceded by a double slash denotes a comment.

//centered square aperture
nx = 100; ny = 100; //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
r= sqrt(X.^2 + Y.^2); //note element-per-element squaring of X and Y
A = zeros (nx,ny);
A(find(abs(X)<0.3 & abs(Y)<0.3)) = 1;
imshow (A);

Figure 2. Centered square aperture.

A synthetic image of a sinusoid along the x-direction was also simulated. It is shown in Figure 3.

Figure 3. 2-dimensional sinusoid along the x-direction

The code for this image is shown below. Another way to display the sinusoid is by using the mesh plot shown in Figure 4. Its shape is similar to a corrugated or rippled roof. I chose sin(3*pi*X) because I want a bigger wavelength.

//sinusoid along the x-direction (corrugated roof)
nx = 100; ny = 100; //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
A = zeros (nx,ny);
A = sin(3*%pi*X);
mesh (A); //if 3-dimensional
//imshow (A); //if 2-dimensional

I want to thank Rommel Bartolome for telling me that the mesh plot can be used in this item.

Figure 4. Sinusoid along the x-direction in three dimensions.

The third synthetic image which was asked to be simulated was a grating along the x-direction. I noticed that Figure 3 can look like a grating if there where no “gradient” in the values. That is why, I thought of modifying the sinusoid code then look for a threshold value and set it to 1 in order to make a grating. The code is shown below. My threshold for this case is 0.9.

//grating along the x-direction
nx = 100; ny = 100; //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
A = zeros (nx,ny);
A(find(abs(sin(3*%pi*Y))>0.9)) = 1;
mesh (A); //if 3-dimensional
imshow (A); //if 2-dimensional

The corresponding grating image along the x-direction is shown in Figure 5.

Figure 5. Grating along the x-direction.

Alternatively, the mesh plot can also be used to this kind of function but it will not show a grating. It will only show a series of ups and downs like that of a 2-bit synchronous counter.

The next image created was an annulus. In Mathematics, it is defined as the area between two concentric circles with different radius [3]. Since it talks about rings, I modified the centered circle Scilab code by choosing to values of radii. These will be the concentric circles and the area between them will be the annulus. The Scilab code is shown below. Moreover, the image of the annulus is shown in Figure 6.

//annulus
nx = 100; ny = 100; //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
r= sqrt(X.^2 + Y.^2); //note element-per-element squaring of X and Y
A = zeros (nx,ny);
A (find(r>0.3 & r<0.7) ) = 1;
imshow (A);

Figure 6. Simulated image of an annulus.

The last task is to create an image of a circular aperture with graded transparency or Gaussian transparency. From the term itself, I thought of a transparent medium which has a Gaussian distribution, meaning the middle has the highest transmittance T and the value of T dies down in all directions since we were dealing with 2-dimensional matrices. It was not indicated if this aperture is center but for symmetry and convenience purposes, I chose to make the aperture centered.

In this case, I made use of Gaussian function. In 2 dimensions, this function centered at (0.5,0.5) is expressed as

where the 0.2 value is the width of the kernel [4]. The Scilab code is written below and Figure 7 shows the said image.

//circular aperture with graded transparency (Gaussian transparency)
nx = 100; ny = 100; //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
A = zeros (nx,ny);
A = exp(-(X.^2 + Y.^2)/(2*0.3^2)); //note element-per-element squaring of X and Y
imshow (A);

Figure 7. Simulated circular aperture with Gaussian transparency

Scilab was already introduced to us last semester in Applied Physics 185. I can still remember I had a very hard time installing the SIVP module. The good thing is that there were some computers in the computer room installed with Scilab. But now, it is successfully installed in my laptop and I can enjoy the free and convenient Scilab program language. This activity did not only focus on the basics of Scilab but also on basic matrix operations. I have learned that Scilab is a good programming language if we are dealing with matrices.

For this activity, I will give myself a grade of 10/10 because I was able to finish the codes before 1pm despite having the hard time to install Scilab and SIVP. I think my figures are also clear even if the resolutions were 100 x 100 pixels only. I followed the instructions in the manual. I cannot give myself some bonus points, I guess, because I did not make use uint8(). I did my codes based on how I can imagine the images in my mind.

References:

1. “Key Facts”, available at http://www.scilab.org/aboutus/consortium/key.

2. Maricor Soriano, “A2 – Scilab Basics”, Applied Physics 186 laboratory manual, 1st semester AY 2012-13.

3. “Annulus”, available at http://www.thefreedictionary.com/annulus.

4. “Plotting 2D Functions”, available at http://www.site.uottawa.ca/~edubois/courses/CEG4311/plotting_2D.pdf.

AP 186 Activity 1: Digital Scanning

Ratio and proportion were already taught as mathematical concepts during the early gradeschool years. Actually, even in the tertiary education, this elementary concept is still utilized. One way is by digitizing an image. By means of ratio and proportion, the image points and the Cartesian coordinates are matched so that one can actually measure the size of an image or a particular part of an image. Digitizing is also very important in the research field especially when collaborators are far from each other. If they need to send large amount of data, they can send an image instead and the other party can digitize it to extract the data. However, one should consider the accuracy of the digitized values.

For this activity, I looked for a hand-drawn graph in the National Institute of Physics library last June 14, 2012. I finally chose a graph showing a curve of current in milliamperes as a function of relative humidity of an electronic hygrometer. I directly scanned the page of the book and it is shown in Figure 1. I acquired the plot from a proceedings entitled “On the Development of an Electronic Hygrometer” from the Proceedings of the Fourth National Physics Congress. The authors were Dr. Caesar A. Saloma and Mr. Enrico G. Yap of National Institute of Physics, College of Science, University of the Philippines-Diliman, Quezon City, Philippines.

It’s a shame that I do not know Mr. Yap. I cannot find a credible source to identify his profession so that I could use a proper name prefix. To be safe, I used the title “Mr.” instead.

The fourth National Physics Congress was held at the University of the Philippines-Baguio on March 31 to April 3, 1985. It was organized by the Samahang Pisika ng Pilipinas (SPP). The compilation of the said proceedings was edited by Mr. Christopher C. Bernido.

Figure 1. Scanned image of the hand-drawn graph from the Proceedings of the Fourth National Physics Congress [1].

The pixel resolution of the scanned image referring to Figure 1 is 2004 x 2724 pixels and its size is 866 kB. However, the resolution of Figure 1 itself in this post is only 300 x 500 pixels for formatting purposes. The scanned image was saved as scanned_plot.jpg. This image was grayscaled (B&Wcommand) using Picasa 3.9.0 by Google, Inc. and was saved as grayscaled_plot.jpg. The image size then dropped to 632 kB with the same pixel resolution. It is shown in Figure 2. Both image formats were JPG (short version of Joint Photographic Experts Group) [2].

Figure 2. Grayscaled image of Figure 1 using Picasa 3.9.0.

I opened the grayscaled_plot.jpg using MS Paint Version 6.1 for Windows 7 Starter afterwards. I used the Select command to crop out the area that I need to measure and to focus on. Upong Select-ing, I noticed that the plot is slightly tilted. That’s why I used Picasa again to correct the orientation. I slightly rotated the plot counterclockwise using the Straighten command. This image was saved and I opened it again in MS Paint for the next step. Fortunately, I found out that the x and y-axes were perpendicular to each other. As can be seen in Figure 1, the authors made use of a guide, maybe a graphing paper, to establish perfect axes. Hence, there will be very minimal room for errors. I then used the Select command to crop the plot so that the lowermost and leftmost point of the image is approximately the origin of the Cartesian coordinate system. The image pixel value will be identified later as I hover the mouse pointer around the image using MS Paint. The x and y-axes I followed were the ones connected to the point (0,0) of the Cartesian coordinate system. The other x and y-axes were actually not perpendicular to each other.

Figure 3 shows how perpendicular the x and y-axes I followed. It also shows that the other axes were not perpendicular. These lines were used to identify the position of the points in the curve. Imperfections such as these are inevitable for hand-drawn graphs. That is why it is very important to use computer programs nowadays to plot numerical values and relationships for accurate display of results. This is one of the benefits of the advancement of technology.

Figure 3. Illustration of how the hand-drawn plot was cropped

On the other hand, Figure 4 shows the cropped plot following the x and y axes connected to the corresponding (0,0) point using Cartesian coordinate system. The image size expectedly dropped to 507 kB with a resolution of 1296 x 1300 pixels. However, the display resolution of this plot in this blog is only 300 x 300 pixels. Note that the origin of the Cartesian coordinate system, point (0,0), has the image pixel location of (9, 1295) px.

Figure 4. Cropped hand-drawn graph which was investigated.

To find how many pixels in the x and y-axes correspond to the physical or actual values of the tick marks of the graph, the actual values were related to the pixel position for each axis. Figure 5 shows the relationships and corresponding linear trends. These were calculated using Microsoft Office Excel 2007.

Figure 5. Linear relationship between pixel locations and axes values.

The two equations from Figure 5 were used to determine the physical values of the points in the curve which in turn used to interpolate points for the entire curve. I chose 19 pixel locations in the curve instead of just the 6 encircled points to get a closer shape of the curve. Figure 6 shows the resulting physical values corresponding to the 19 (x,y) pixel location I chose. In other words, I substituted the 19 x pixel locations to the linear trend for the x-axis and substituted the values of the 19 y pixel locations to the linear trend for the y-axis.

Figure 5. Plot of the interpolated points and the digitized curve.

It can be visually observed that the set of interpolated points were similar to the hand-drawn plot. But as scientists, this observation is not enough. To quantify this relationship, the physical values of the 6 encircled points from Figure 1 points can be compared to the corresponding digitized values of the curve from Figure 6. Table 1 summarizes the said comparison. The second to fourth digitized values were converted into two significant figures so that it will be the same with the physical values. In simple terms, there is no difference between the digitized and the physical values.

Table 1. Comparison of the digitized and physical values of the overlaid plots in Figure 6.

The digitized plot can also be overlaid to the original hand-drawn plot for a clearer and straightforward comparison. I saved the digitized curve in Microsoft Office Powerpoint 2007 using Save as Picture… and opened it again in the same window. In the Format tab, I clicked the Recolor tab in the left in drawing toolbar and chose Set Transparent Color. Then I clicked the white background of the image plot to make it transparent. I then inserted the cropped plot from Figure 4 and clicked Send to Back button in the drawing toolbar. And then I clicked the digitized plot again and overlaid it by clicking Bring to Front button in the same toolbar. There I resized the two images so that they correspond to each other. I superimposed the endpoints of each plot so they will really overlap. I grouped them so that they will be saved in a single image. Figure 7 shows the overlaid images.

Figure 6. Overlaid images of the cropped hand-drawn graph and the digitized curve.

From Figure 7, it can be seen that the curves were really similar to each other. However, they are not perfectly the same. These account for the errors and the 0.9999 correlation coefficients from the relationship of pixel location and physical values from Figure 5. Moreover, the two curves were similar because the hand-drawn x and y-axes were perpendicular.

I recommend that the scanned image be of good resolution so that the correct pixel position will be focused. Pixel position should also be chosen with much care and accuracy.

In this activity, I will give myself a grade of 10/10 because I understood the concept of digital scanning and I think my plot is very near to the hand-drawn plot. I also think that I have done the things that should be done for this experiment. My plots are clear and of good quality.

References:

1. Caesar A. Saloma, Enrico G. Yap, “On the Development of an Electronic Hygrometer” in Proceedings of the Fourth National Physics Congress, (University of the Philippines-Baguio, Philippines, 1985), pp. 111.

2. “Resources”, available at www2.johnson.cornell.edu/currentstudents/logo_template.html.

3. Maricor Soriano, “A1 – Digital Scanning”, Applied Physics 187 laboratory manual 1st semester AY 2012-13.