Activity #3 – Scilab Basics

Today, we are going to learn a very useful programming language, which is called Scilab. It is a high level, scientific programming language which is an acceptable substitute for Matlab. Best of all, it is FREE! So what’s good about Matlab and Scilab? Most computer-based and software businesses hire computer programmers who are proficient in Matlab. Matlab allows matrix manipulations, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs written in other languages, including C, C++, Java, Fortran and Python (from Wikipedia). From digital computing to digital imaging, everything can be done with Matlab. However, the main reason why Matlab is great for image processing is its optimized element-by-element matrix operations, which is extremely useful for fast creation of different filters. Studying Scilab, which uses almost the same syntax, will engross one person into scientific computing with this high-end programming languages.

What I am going to do today is to create synthetic images using matrices generated in Scilab. Basically, an image can be produced from a matrix, in which each value in the matrix designates a certain value. Each value represents the unit pixel color in the image. In this activity, basic apertures in grayscale were produced using matrix manipulation. For an example, here is a Scilab code and the resulting image of a circular aperture. This one is a revised version from [1].

Code 1
Code for the Circular Aperture.

Let me explain the code for all of you. In this code, I used 1000 x-points and 1000 y-points for a higher resolution of the image. The linspace() function creates a 1-D array of values from a given range of values and number of points. In this case, I created 1000 equally spaced points from -1 to 1. The ndgrid() function then creates two matrices that are a repetition of the given 1-D arrays: one repeats along the columns, and the other repeats the vector along the rows. Line 5 then gives the coordinates of the pixels from the center of the image. The find() function then finds the pixels that have values at the given conditions, which in this case, less than 0.7. This is then designated to be the circle.

Circular Aperture
Figure 1. Circular Aperture.

The radius was chosen to have a magnitude of 0.7 to provide the white, circular aperture in the image. One can easily verify this as a circle by looking at its radius.

Now, let’s move on with the challenges. We are to create different synthetic images using what we’ve learned from matrix manipulation. Again, same number of points were used to create a high resolution of the image. Provided also are the codes, so one can benefit greatly if one is stuck from doing the images. 🙂

1. Centered Square Aperture

This one is fairly easy. One can just set linear boundary conditions for the aperture locations.

Code 2
Code for the Centered Square Aperture
Centered Square Aperture
Figure 2. Centered Square Aperture

One can verify that this is a square by checking for its side lengths. This can be extended then into a general rectangular figure by altering the boundary conditions.

2. Sinusoid along the x-direction (Corrugated Roof)

In this image, one must use the sine (or cosine) function to create a corrugated roof, in which the color value (in grayscale) in the matrix varies sinusoidally. The code is shown below.

Code 3
Code for the Corrugated Roof.
Sinusoid
Figure 3. Sinusoid along the x-axis (Corrugated Roof).

I used a spatial frequency value of 10 to generate this wave. To verify that it is sinusoidal, one can look at the pattern or do a line scan of it. Observe that the color value alternates from dark to bright colors, but with a gradient in each alteration. The darkest color represents -1, while the whitest is 1. One can then change the parameters to direct the variation along any direction.

3. Grating along the x-direction

This image is the same as with the corrugated roof, only with values alternating 0 and 1. One can do this in many ways. What I did was to use the squarewave() function from Scilab to generate a periodic wave that has values 1 and -1.

Code Grating
Code for Grating along the x-direction.
Grating
Figure 4. Grating along the x-axis.

We observe alternating black and white patterns, with no grayscale variation. The square wave generating function used can be altered by adding another input value, which will determine the percentage of the white parts.

4. Annulus

This one is easy. An annulus is an object shaped like a doughnut. Yummy! One can create this image by setting boundary conditions in the circular aperture.

Code 5
Code for the Annulus.
Annulus
Figure 5. Annulus.

Easily enough, you can observe the shape of a donut. Well, perfection is rewarding.

5. Circular Aperture with Graded Transparency (Gaussian Transparency)

Initial reaction:

“Life is like a box of chocolates.”

I admit, the challenge was a bit astounding. Moving on, one can generate this image by knowing first the Gaussian equation for intensity. Basically, it follows a bell shaped curve, with a peak at its middle. The location of the peak is determined by the mean of the Gaussian distribution, while the spread is determined by the standard deviation. The center then is designated to have the peak value for the image. After that, the aperture is created by elemental multiplication of the normal circular aperture.

Code 6
Code for the Gaussian aperture.
Gaussian
Figure 6. Circular Aperture with Gaussian Transparency.

One can see concentric lines indicating the changing value of the intensity of the aperture. The center has the highest intensity. The deviation in the values can be changed by changing the value of σ in the Gaussian distribution. In this case, I used a value 0.9 for the standard deviation. Looks good enough for a Gaussian aperture.

6. Ellipse

This one is also fairly easy. One can create this image by using the general parametric equation for an ellipse. The code and image are shown below.

Code 7
Code for the Ellipse.
Ellipse
Figure 7. Ellipse.

One can easily verify that this is an ellipse by checking the lengths of the semi-major and semi-minor axes, which are different. The shape of the ellipse is dictated by the values associated with the parametric equation for the ellipse.

7. Cross

One can generate this image starting from the centered square aperture. The boundaries for the aperture must be extended to create this image.

Cross Code
Code for cross aperture.
Cross
Figure 8. Cross Aperture.

Obviously, this is a cross. One can manipulate the boundaries to shorten and lengthen the region at which the cross encompasses.

With all those challenges completed, it’s time to go deeper.

Inception

With the knowledge of Scilab and matrix manipulation, one can combine and manipulate the generated image to produce not just synthetic, but aesthetic images. I won’t post the code snippets, so you can challenge yourself in doing this. So, without further adieu, it is time to present Bart’s Magical Images of Imagination.

The Checkerboard

Checkerboard
Figure 9. The Checkerboard.

The checkerboard is basically just a combination of two corrugated roofs in different directions. Can you guess what kind of combination this is?

The X-Factor

X-Factor
Figure 10. X-Factor (Tilted Cross)

This image is produced by tilting a cross aperture. I’ll give you a hint on how to make this. In vector spaces, the rotation matrix (you’ll have to search it) allows for a rotation of the coordinate system, such that the values are still invariant. By wisely using this rotation matrix, one can rotate different images with a desired angle of rotation.

The Orbit (Tilted Ellipse)

Tilted Ellipse
Figure 11. Tilted Ellipse.

This is another image that utilizes the rotation matrix.

The Weaver

The Weaver
Figure 12. Tilted Checkerboard

And yet another one! And my personal favorite…

The Gaussian Factor

Gaussian Cross
Figure 13. Tilted Cross with Gaussian Transparency.

Crazy how many things you can do with element-by-element operations, eh?

Looking at all these synthetic images, I was mesmerized.

Amazed
Waow!

With that, the journey in Image Processing continues. With all these basic Scilab knowledge and matrix manipulation, I have created challenge images, and proceeded further beyond what is wanted. I rate myself 12/10. I am now equipped with one of the best and overpowered programming languages when it comes to digital image processing. Being able to create these synthetic images accurately will allow for the best scientific simulations such as in optics and holography. Creating these synthetic images would also help in simulating the Fourier Transform of images, which is very useful in simulating optical experiments.

Acknowledgements: I would like to thank Ms. Eloisa Ventura for giving us thoughts about producing images beyond what is assigned, and Mario Onglao for reminding me the use of the rotation matrix.

References:

[1] M. Soriano, “Scilab Basics,” Applied Physics 186 Activity Hand-outs, 2015.

One thought on “Activity #3 – Scilab Basics

Leave a comment