Académique Documents
Professionnel Documents
Culture Documents
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.
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
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]
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)
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];
Watch out: Indices in SciLab start at 1, not 0 as is the case in most programming languages.
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
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');