Вы находитесь на странице: 1из 6

SciLab

A short introduction to working with SciLab

SCILAB ......................................................................................................................................................... 1 INTRODUCTION ........................................................................................................................................... 1 VARIABLES IN SCILAB ............................................................................................................................... 2 WORKING WITH MATRICES IN SCILAB ....................................................................................................... 2 Declaring Matrices ............................................................................................................................... 3 Suppressing Output............................................................................................................................... 3 Accessing Matrix Elements ................................................................................................................... 3 Determining the Size of a Matrix .......................................................................................................... 3 FLOW CONTROL IN SCILAB ........................................................................................................................ 4 If Statements.......................................................................................................................................... 4 While Loops .......................................................................................................................................... 4 For Loops.............................................................................................................................................. 5 WORKING WITH IMAGES IN SCILAB ........................................................................................................... 5 Loading Images..................................................................................................................................... 5 Displaying Images ................................................................................................................................ 6 Saving Images ....................................................................................................................................... 6 SAVING AND LOADING PROGRAMS IN SCILAB ........................................................................................... 6 IMAGE PROCESSING EXAMPLE ................................................................................................................... 6 Horizontally Flip an Image................................................................................................................... 6

Introduction
SciLab is a programming language/development environment developed for mathematical programming, and in particular working with matrices. It is basically an open source version of Matlab, a commercial product for the same process. For this reason SciLab is a perfect fit for image processing, as most image processing problems reduce to matrix manipulation. SciLab is also relatively easy to use, and its development environment makes it ideal for prototyping. Figure 1 shows a screen shot of the SciLab work environment. Code is entered directly into the main window on the right.

Figure 1: The SciLab workspace

Variables in SciLab
All data in SciLab is stored in matrices, but more of that anon. More importantly we must first notice that in SciLab there is no need to declare variables before using them (as you would in Java/C/C++). Rather new variables are simply introduced as they are required. Variables names follow rules similar to those used in most other languages: No spaces Dont start with a number Variable names are case sensitive

So, for example, if we want to use a new variable called threshold during the thresholding of a grey-level image we could do so as follows:
threshold = 42.4

The value of a variable can be queried simply by entering the variables name. For example:
threshold

Results in the output:


Threshold = 42.4

Working with Matrices in SciLab


Most work in SciLab is performed on matrices (in fact all data within SciLab is stored in matrix format). This section will discuss how matrices are created and accessed in SciLab.

Declaring Matrices
There are a number of different ways in which to declare a new matrix in SciLab. The first is to simply assign a new variable a list of matrix data. This is done as follows, where matrix rows are separated with semi-colons.
A = [1 2 3; 4 5 6; 7 8 9]

The above declaration results in the following output:


A = 1 4 7 2 5 8 3 6 9

SciLab also provides a number of functions which can be used to populate a new matrix with particular values. For example to make a matrix full of zeroes we can use the function zeros(m, n) which creates an m*n matrix of zeros as follows:
EmptyArray = zeros(3,3)

Which outputs the following:


EmptyArray = 0 0 0 0 0 0 0 0 0

Suppressing Output
You will have noticed in all of these examples that once a matrix is declared, its contents are automatically echoed. This is true of all SciLab statements. However, oftentimes it is inappropriate for this to take place. Output can be suppressed by placing a semi-colon at the end of any statement.
A = [1 2 3; 4 5 6; 7 8 9];

Accessing Matrix Elements


Accessing matrix elements in SciLab can be achieved simply by placing the matrix coordinates in brackets after the name of the matrix. Coordinates are given in the order of row followed by column.
A(2, 3)

Watch out: Indices in SciLab start at 1, not 0 as is the case in most programming languages.

Determining the Size of a Matrix


The size function can be used to determine the size of a matrix. size takes a matrix as its only parameter and returns the length of each dimension of the matrix. In order to store each of these dimensions a list of variables is usually used to store the results of the size function. For example, to query and store the size of an image matrix we could use the size function as follows:

[rows, cols] = size(ImageData);

Flow Control in SciLab


SciLab has flow control statements just like every other programming language. In this section we will have a look at if statements, while loops and for loops in SciLab.

If Statements
If statements are simply used to make decisions in SciLab and use the following syntax:
if <condition> then <do some work> else <do some other work> end

So for example if we wished to perform thresholding of an image we could use an if statement as follows:
if ImageData(r, c) > threshold then ThresholdedImage(r, c) = 255; else ThresholdedImage(r, c) = 0; end

While Loops
While loops repeat a piece of work as long as a condition holds true. The while loop in SciLab uses the following syntax:
while <condition> <perform some work repeatedly> end

So, for example to sum all elements in an array until one is greater than 10 we could use the following:
valuesArray = [3 4 2 5 6 7 8 11 14 56 43]; currentVal = 0; index = 1; total = 0; while index <= 10 currentVal = valuesArray(index); total = total + currentVal; index = index + 1; end

For Loops
For loops in SciLab are particularly useful for iterating through the members of a matrix. The SciLab for loop uses the following syntax:
for index = <start>:<finish> <Perform some work> end

For example, to iterate through all of the elements of a 2-dimensional matrix (of size 20 by 20) of image pixels adding together the values of them all we could use the following nested for loops:
total = 0; for rowIndex = 1:20 for colIndex = 1:20 total = total + ImageData(rowIndex, colIndex); end end

Working with Images in SciLab


The following section will discuss how to load, display and save images from within SciLab.

Loading Images
To load an image in SciLab we use the gray_imread function. This function takes one parameter: the name of the image file to load. The image is converted to an appropriately sized matrix which we can assign to a variable as follows:
Moon = imread('U:\moon.bmp')

The image filename can be given as a full file path or as a file path relative to the SciLab current directory. The current directory can be changed from the main SciLab interface window. The supported file formats include bmp, gif, jpg and png. WATCH OUT: The results of any SciLab operation are immediately written to the console window. When an image is loaded this means a long list of pixel values will flash past on the screen. Avoid this happening by placing a semi-colon at the end of each line, as shown below:
Moon = gray_imread('U:\moon.bmp');

When an image is read from a file it is stored in a SciLab floating point matrix. The intensity levels for image pixels are given in the range 0.0 1.0. Once the image data is converted we are ready to perform image processing operations on the image data.

Displaying Images
To display images in SciLab we use the imshow function. This function simply takes the array storing the image values as its only parameter. An example is:
imshow(ImageData);

Saving Images
Images are saved with SciLab using the imwrite function. This function takes the name of the matrix storing the image data to be written and the new file name as its parameters.
imwrite(ImageData, ' ImageData.bmp', 'bmp');

Saving and Loading Programs in SciLab


SciLab is very easily used as a prototyping environment in which we try things out, examine the results and then adjust our programs to give better performance. One way in which to make this easier is to write your SciLab programs using the SciLab editor. In this way we can design our programs before we edit them. Simply press the Editor file menu item to open the editor. Programs can be saved as .sce files and run in SciLab by choosing the Load into Scilab option from the Execute menu. Scilab also includes a full debugger which allows you step through your programs.

Image Processing Example


Horizontally Flip an Image
The following SciLab code reds an image from a file, flips this horizontally and writes the result back to a file.
//Read in an image Moon = gray_imread("Moon.bmp") //Display the image imshow(Moon); //Determine the size of the image [rows,cols] = size(Moon); //Declare a new matrix to store the newly created flipped image FlippedMoon = zeros(rows,cols); //Generate the flipped image for r = 1:rows for c = 1:cols FlippedMoon(r,cols+1-c) = Moon(r,c); end; end; //Display the flipped image imshow(FlippedMoon); //Write an image to a file imwrite(FlippedMoon,'HoriFlippedMoon99.bmp');