Generating synthetic images using Scilab

Scilab is a programming platform that has similar functions with that of MATLAB, only that it’s open-source.

Images, in computer ‘language’, are just matrices of numbers corresponding to pixel values.  Scilab is a useful tool in manipulating images because it can do a lot of matrix operations with only a few lines of codes.

Circular aperture

Below is the code for generating an image with circular aperture.

circular-aperture-code

The first thing to do is to make a list that has elements for the x and y axis (lines 9 and 10). We mesh these two lists to create an nx x ny 2D array (line 12). For every element in the mesh grid, one can obtain the x-value and y-value of that mesh grid.

We want to make a circular aperture with radius r. We can simply calculate for the radius of one pixel with respect to the center of the image by using the distance formula

r = sqrt(x^2 + y^2)

for every pixel coordinates.

Next is to make a ‘dummy’ 2D array called A of the same size with all its elements being zero (line 15).  We manipulate the value of A by finding all r values less than 0.7 and convert it to value 1. The resulting image would look like the one below.

circular-aperture

Square aperture

Below is the code for generating an image with square aperture.

square-aperture-code

We do the same method as before in creating the mesh grid. Only, our conditions for the values of A would change. The key here is to notice that the square (or rectangle) is symmetric with respect to the y-axis. Thus, in choosing which pixels should have a value=1, we use the condition set in line 32.

abs(x)<side

-x < side < x

The resulting image would look like the one below.

square-aperture

 

Sinusoid along the x-direction

The next one is a bit harder. We want to generate an image such that there seems to be a sinusoid function (or wave) moving in the x-direction. Below is the code used in generating it.

sinusoid-code

Notice that instead of our x list having only values from 0-1, we changed it in terms of radians and calculate the sin values for each x.

Now instead of making a dummy array, we can directly equate the mesh grid to A.  Plotting it would give is the image below.

sinusoid

Grating along x-direction

Below is the code in generating a grating (alternating blacks and whites).

grating-code

You can see that it is similar to the sinusoids, only that the A matrix takes only 1 and 0 values. We can do this by setting a condition that for all sin(x)>1, its ‘color’ is white.

The image to be generated would look like the one below.

grating

Annulus

An annulus is just a circle with a hole in it. It can be defined properly after knowing the inner radius r1 and the outer radius r2. Below is the code used.

annulus-code

This is similar to the circular aperture but with two conditions: one at r=0.3 and another at r=0.5. The generated image would look like the one below.

annulus

Circular aperture with graded transparency

Below is the code used to generate it.

circ-aper-graded-code

This is just similar to that with the circular aperture only that our ‘dummy’ A matrix was created with values according to a 2D Gaussian function.

r^2 = x^2 + y^2

z = exp(-r^2)

Next was to condition A such that grater than some radius r (=0.5), all values are 0 (black). The image that would be formed is shown below.  Notice that instead of white colored circle, it now has some gradient property.

circ-aper-gradient

Ellipse

An ellipse can be expressed mathematically as

r^2 = (x^2/a^2) + (y^2/b^2)

Where the a and b are the parameters for the elongation of the ellipse in the x and y direction. Below is the code used in generating it.

ellipse-code

We just need to calculate r using the equation and condition the A matrix to make all values with r<0.5 to be white. The image formed would be the same as the one below.

ellipse

Cross

Below is the code used in generating a cross image.

cross-code

All the things you need to do is to condition only the A a couple of times.  Running only lines 128 and 129 would make a square. Including lines 130 and 131 would complete the cross. The image should look like the one below.

cross

The point here is that if these things would be done in other programming languages like python or C+, it would take multiple lines of code just to generate one type of image.

 

 

 

 

 

 

 

 

Advertisements

Author: klguial

undergraduate physics student

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s