Image taken from http://scilab.org

Another programming language or tool that one may opt to use for numerical analysis to even image processing is Scilab. Personally, I call this my very own free and simplified version of MATLAB. Even though I haven’t really bonded with MATLAB that much as I love hanging out with Python, from what I basically know… Scilab has most of the things I essentially need in MATLAB. In fact, there are instances wherein their syntax are almost exactly the same!

However, what’s probably making Scilab lighter than MATLAB in terms of space may come from the fact that modules are needed to be installed first. But then that’s what makes this a bit easier since there are instances wherein you only need a few functions and hence you only download the things you need instead of getting the full package.  Modules can be downloaded in the Scilab Comlementary Modules website.

Since the goal of this class (Applied Physics 186) is to develop an understanding and mastery of image processing and pattern recognition techniques as quoted from the course syllabus of Maricor Soriano, PhD., the module needed for Scilab is Signal Image and Video Processing (SIVP).

Downloaded? Installed? Let’s Go!!

Plotting a  sine wave is simple. Similar to MATLAB, a list starting from x1 and ending with x2 with a step size of s is created by the syntax:

A = [x1:s:x2]

Creating a list for our values of t (time) and plugging this in to the sine function will automatically give us our desired output when plotted.

To further test our understanding of the basic functions in Scilab, creating synthetic images as based from algorithms can be done as a form of practice. As an example given in the manual, a circular aperture code with its corresponding output is as seen below:

In line 1, variables nx and ny were used to indicate the dimension of our desired image (for my case I’d have a 200×200 image). The linspace function as used in lines 2 and 3 simply defines the range of the image.  Now since we are dealing with a circular aperture, we hope to have 2-D system. With our x and y values initially set, we now combine these and form a 2-D array  through the use of the ndgrid function. However, it is better to deal with circles in the polar coordinate system and hence we convert x and y to r by using the simple distance formula. Take note that the power  operator here has to have a period before it as it signifies that this operation is to be done to elements in the array individually.

Wanting a circular aperture, we start of with an array of dimension nx and ny with all its element equal to 0 which is done by using the zeros(nx,ny) function. It is important to take note that when these is converted into an image, 0 stands for black  while 1 stands for white. .. which is pretty much similar to the on/off analogy for 1 and 0. As the last step, we now want to convert certain elements in our zero matrix into a 1 which will form into a circle when plotted. What’s great about Scilab is that it has a find(condition) function wherein it gives the indices of the elements that have satisfied your condition. Using this, the conversion process can be done using a single line!

Square Aperture – WANTED! Sign

 Have you ever wondered how you can make your own wanted sign through the use of different functions in Scilab?  This is almost similar to the circular aperture image wherein you start off with an array of dimension nx by ny with all its elements equal to 0 (or black graphically). With the x,y coordinates better used for shapes like squares, we replace certain elements in our zero-matrix to ones by using the list function (or the [::]) to name the indices of the elements we want to change.

My list starts off with nx*0.25 which is 1/4 of the square that we have and it ends at nx*0.75 which is 3/4 of the original square. If I only make use of  the command

A((nx*0.25):1:(nx*0.75)) = 1;

which, at first, is what I did… I’d only end up with a white elongated line lying parallel to the x-axis. We have to remember, that unlike that of the circular aperture wherein it is in polar coordinate system, we are dealing with a 2-D coordinate system and hence we have to add another condition:

A((nx*0.25):1:(nx*0.75),(nx*0.25):1:(nx*0.75)) = 1;

Grating – Behind Bars

The next shape we hope to make resembles that of what the prisoners (ages ago) are usually sporting – black and white stripes. I can easily do this shape by starting off with an array of zeros and manually converting certain elements into ones through listing them one by one… that’d be almost exactly similar to the square aperture earlier done only that we’d have more conditions.

Since codes are done to speed up the process and try to reduce the work of the user, I ought to find an easier method in making the grating . What I thought of was that we, as usual, start off with an array of dimension nx and ny with all its elements to be equal to 0. Then, a while loop was used so that it’ll automatically determine the values of the indices you want to change to get your gratings . But before that, I used a variable which I named as the grating wherein this determines how many bars of black and white will my output have. The C variable then serves as the starting point while the D variable the ending point of where I’d like to change the values to 1. For while loops, it is customary to make use of a variable that serves as the determining factor to whether it’ll continue to loop or not. For my case, the i variable was set to 0 and upon reaching the value of i = nx (or the edge of the image), the loop will stop. The variable fac was essential since this’ll be the factor by which variable C, D and even i will change everytime the loop repeats itself. Now the indices of the elements we hope to convert to 1’s was named similar as to how the square aperture was done BUT since we would like to get rectangles instead, I set the other condition to be

A(1:1:ny,C:1:D) = 1;

 where it simply says that it starts at the top-most portion of the image (1)  up until bottom-most part (ny).

Corrugated Roof – Behind Bars in 3D!

Almost similar to the previous shape, a corrugated roof was tasked to be drawn next. As seen in the image to the left, this resembles the typical jail bars but now it seems as if it has depth (making it somewhat 3D-ish) all thanks to the presence of a gradient. The mere fact that we are no longer dealing with simple black and white indicates that our end-matrix would have values ranging between 0 and 1.

As discussed earlier, the sine wave can easily be done by starting off with a list that signifies our “t”. I created a variable width so that we can easily change the dimension of our image just by varying this variable. Plugging in our list to the sin function, we get our desired sine output. If we carelessly plot this, we’d get a line of width equal to our variable “width” only and not our desired block. In order to come up with the square, we have to find a way such that our line (or list) appears to be stacked forming an array or similar values along the y-axis. Thankfully, a function that does that exact thing is available in both Scilab and MATLAB. The repmat() function asks for three parameters:

repmat(MATRIX,repeat in x, repeat in y);

The first parameter is the list or array that you hope to repeat. The second parameter asks for how many times will you hope to stack the given array along the x-axis while the third is for the y-axis. Since we only want to have our original list of our sine function to repeat along the y-axis, I set it to be equal to width*10 (10 comes form the fact that the step size of our t list is 0.1). The x-repetition parameter is set to 1 since we no longer need to have it repeated. 

For fun, I also tried to look at the mesh (or the 3-D plot) of our resulting image.

 

Grating – Behind Bars (REMIX!)

Thankfully, there is a much simpler way of drawing the gratings in Scilab. As suggested by Mr. Gino Borja during class, we can make use of the sine function and convert it into a 2-D matrix. We are to make use of the property of sine waves where it is known to have a positive and negative values moving periodically and smoothly.

Similar to what I did for the corrugated roof, we initially start with a sinusoidal image. Now we set the conditions where if the values of the array is greater than 0, the elements will be converted into a 1 while if the value is less than 0 it will be converted to zero. This is simply applying the technique we’ve been doing for the circular aperture (as well as the square aperture).

From 15 lines with the use of while loops, we have managed to reduce it into 7 lines!

Annulus – Eye Spy

 The next image is very much related to the first image we’ve done – the circular aperture. To put things simply, we are to just add another condition wherein after changing a set of elements that makes up a radius r=0.7 to 1, we’d have to change the elements of a smaller radius (r = 0.3) back to 0! As simple as that!

Gaussian Transparency – You are my sunshine 🙂

 The last image tasked to make looks like the sun shining on a foggy day. I’ve managed to come up with two methods in making the Gaussian transparency in Scilab. The first method resulting with the image located to the left comes from the basic definition of a Gaussian function. Recall that the Gaussian function is proportional to the negative exponent of x. But since we hope to end up with circular Gaussian pattern, we instead plug in r (remember when we converted our x and y axis into polar coordinates).  Varying the constants will give us a different size for our gloomy sun.

As for my second method, I made use of the imfilter and fspecial filter to end up with a more defined gloomy sun as seen on the image to the right.  I’ve managed to find that there exists a function in Scilab wherein there are different filters automatically set and are waiting to be used – fspecial. Wanting our end product to be a Gaussian transparency, I chose the “gaussian” mode for the fspecial function wherein the [200,200] is simply the dimension/size of our filter and 10 the sigma in the Gaussian equation. But the fspecial filter only creates the filter! We need to use the function imfilter wherein it takes in the original image you wish to filter and the filter you wish to use. With our original image a circular aperture (variable A in our case) and the filter the one we initially set (variable filt), the end product is the beautiful Gaussian transparency.

For this activity, I think I deserve to get a grade of 12/10 as I was able to give out more than one technique in producing certain shapes. Even though I planned to make a unique image (such as a tree or a flower) as a challenge for myself, due to lack of time..I wasn’t able to do so anymore.

Personal Notes:

Through this activity, I was able to gain a little bit more confidence when it comes to programming. I have always felt that I’m lacking with my programming skills as compared to my classmates even before. I’d always have a hard time trying to think of the right algorithm for things and believe me, Applied Physics 155 and 156 were something so terrible for me. Add the fact that since my mom’s a computer science student while my father’s known to program things which would usually take other people a week (he’s an engineer by the way)… the pressure add’s up to my belittling myself. I guess I do have an inner programmer in me in some way right?