Matlab Tutorial

 

                  EN161 Image Understanding
                     Matlab lab Session
                  
             


=======================================================
Matlab 6 introduction

- Matrix Laboratory
   . environment for scientific computation
   . workspace: commandline, editor, figures
   . platform independent

- a tool for mathematical computation
   . process matrices, variables, strings
      - basic array operations
      - multi-dementional arrays
   . solve algebra, polynomial, equations
   . do optimization, differentiation/intergration,
     solve differential equations
   
- a tool for visualization/GUI (Graphic User Interface)
   . 2D/3D plotting
   . showing images, grids
   . figures, subplots, axis labels
   . contour plots, surface plots, volumn visualization
   . videos, multimedia/web support
   . GUI: mouse interactivity/controls, dialog boxes

- a programming language   
   . variables, strings
   . logical operations
   . flow control
      - if, for loop, while loop
   . function *.m files
   
- a programming environment
   . command line interpreter
   . *.m program editor
   . debugger, variable watcher
   . profiler
   . C, FORTRAN, JAVA
   
=======================================================
Useful Matlab commands/functions

help
help imread      (help ANY_FUNCTION)
help complex

doc              (same as help, but better)

demo             (good starting demonstration)

who              (see current variables)
whos             (with detailed info)
A=1              (variable name '3A' is not allowed.)
% comment

pi               3.1416
format long
pi               3.14159265358979

ver
path
cd..            
pwd

sin, cos, tan, exp, log, log2, sqrt, floor, ceil, round, erf, gamma, dot, cross

imread         (image I/O, try 'doc imread')
imwrite
imshow

BASIC OPERATIONS:
54.4 + 23*6

input A
pause

ARRAY OPERATIONS:

A = [1 2 3;4 5 6;7 8 9]
=======================================================
Matlab matrix manipulation:

Use the ':' syntax to speedup computation (avoid loops)


>> A=[1 2 3; 4 5 6]
A =
     1     2     3
     4     5     6

>> size(A)
ans =
     2     3

>> A(2:end)                    % (column than row)
ans =
     4     2     5     3     6     

>> A(1:2,2:3)                  % (part of an array)
ans =
     2     3
     5     6     

>> A(:,2)=7
A =
     1     7     3
     4     7     6

>> A(:,:)=8                    
A =
     8     8     8
     8     8     8

>> A(:) = 9 
A =
     9     9     9
     9     9     9

>> A=7                         %  (re-assign A)
A =
     7
     
>> B=(0:0.1:1)*pi
B =
  Columns 1 through 8 
         0    0.3142    0.6283    0.9425    1.2566    1.5708    1.8850    2.1991
  Columns 9 through 11 
    2.5133    2.8274    3.1416  
       
ARRAY TRANSPOSE

C=1:5
C'       

INITIALIZE AN ARRAY

>> ones(3,4) 
ans =
     1     1     1     1
     1     1     1     1
     1     1     1     1

>> zeros(2,3)
ans =
     0     0     0
     0     0     0

>> d=pi
d =
    3.1416

>> D=repmat(d,3,4)             % (Replicate matrix, 3x4)
ans =
    3.1416    3.1416    3.1416    3.1416
    3.1416    3.1416    3.1416    3.1416
    3.1416    3.1416    3.1416    3.1416  

>> E=reshape(D, 4, 3)          % (Reshape to 4x3)
E =
    3.1416    3.1416    3.1416
    3.1416    3.1416    3.1416
    3.1416    3.1416    3.1416
    3.1416    3.1416    3.1416  

>> E(2:3,1:3)=[1 2 3; 4 5 6]  %(process sub-image)
E =
    3.1416    3.1416    3.1416
    1.0000    2.0000    3.0000
    4.0000    5.0000    6.0000
    3.1416    3.1416    3.1416    
    
LOGICAL ARRAY
>> B=[5 -3; 2 -4]
B =
     5    -3
     2    -4
>> x=abs(B)>2
x =
     1     1
     0     1

>>  
>> y=-3:3
y =
    -3    -2    -1     0     1     2     3
>> k=find(abs(y)>1)
 =
     1     2     6     7   
>> max(y)
ans =
     3
>> [my, index]=max(y)      % (return 2 values of a function call)
my =
     3
index =
     7
>> 
=======================================================
Image Manipulation:
   - read image from file (JPG, GIF, BMP, TIFF, PNG, PCX...)
   - write image to file
   - show image on screen, zoom in/out
      . get pixel value
   - process image as an array of pixels
      . UINT8: unsigned 8-bit (0 to 255)  % what happens if we subtract the image?
      . DOUBLE: double-precision (0 to 1)
      . convert between the 2 format: im2double, im2uint8
        'whos' to check the image format

% EXAMPLE CODE OF problem1:

% THE SLOW VERSION:

image = imread ('coins.png');
info = imfinfo ('coins.png');
height = info.Height;
width  = info.Width;
threshold = 80;
for i=1:height
   for j=1:width
      if image(i,j) > threshold
             image(i,j) = 0;
        else image(i,j) = 255;
      end     
   end
end

imshow (image);
imwrite (image, 'problem1.jpg');


% THE FAST VERSION (WITHOUT LOOP):

image = imread ('coins.png');
image = im2double(image);
image = image > 80/255;
imshow (image);


% THE FAST AND NEAT VERSION:

threshold = 80
coins = imread('coins.png');

problem1 = coins < threshold;

subplot(2,1,1)
imshow(coins); 
subplot(2,1,2)
imshow(problem1); 

imwrite(problem1, 'lab1p1.jpg');

=======================================
problem3:

% BRUTE_FORCE APPROACH:
double for loop, im2double, img=1-img


% CONVERSION APPROACH:

coins = uint8(255-double(coins));
imshow(coins);


% BETTER:

invert_image = bitxor(coins,255);

help format
format hex

>> invert_image(1,2)
ans =
   1e                                 1*16+14=30
>> invert_image(problem3(1,2),255)
ans =
   e1
>>