Document not found! Please try again

SciLab

75 downloads 536 Views 71KB Size Report
SciLab. A short introduction to working with SciLab. SCILAB ...................................... ................................................................................................................... 1.
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



Don’t 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 variable’s 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 then else 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 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