Agenda
• Introduction to Matlab
• Basics & Examples
• Image processing with Matlab
• Basics & Examples
What is Matlab?• Matlab stand for Matrix Laboratory
• Matlab is high-level language
• perform computationally intensive tasks faster than with traditional programming languages such as c++
• The help in Matlab is very good, use it!
• Everything is treated as a matrix
The Matlab Environment• Matlab window components:
• Workspace
• Displays all the defined variables
• Command window
• To execute commands in the Matlab environment
• File editor window
• Define your functions
Matlab Help
• Matlab help is an extremely powerful assistance to learning Matlab
• Help not only contains the theoretical background, but also shows demos for implementation
• Matlab help can be opened by using the HELP pull-down menu
Basics syntax
• Comment : ctrl + / OR %
• Uncomment : ctrl + t
• Semicolon : suppress printing
• Arithmetic operators:
+ addition
- subtraction
* multiplication
^ power
' transpose
\ left division
/ right division
• Variable :
>> 2 * 25 + 6 * 22 + 100 / 2
ans =
232
>> a = 25; b = 22;
>> d = a * b
ans =
550
sqrt
returns the square root of
each element of the array X
abs absolute value for real numbers
cos cosine function
sin sin function
exp exponential function
• Who, Whos : current variables in the workspace
• Save : save workspace variables to *.mat file
• Load : load variables from *.mat file
• Clear : clear workspace variables
• Clc : clear command window
• Close : closes the current figure window
>> a = [ 0:2:4 ; 8:1:10; 35:5:45]
a =
0 2 4
8 9 10
35 40 45
>> a = [1:3; 4:6; 7:9]
a =
1 2 3
4 5 6
7 8 9
• A particular element of a matrix can be assigned:
>> a(3,2)
ans = 40
• Place the number 5 in the first row, second column:
• >> a(3,2) = 29
• Operations ad functions that were defined scalars in the previous section can also be used on vectors and matrices, For example.
>> a = [ 1 2 3];
>> b = [ 4 5 6];
>> c = a + b
c =
5 7 9
• Special matrices:
• zeros(n,m) : n*m matrix of zeros
• ones(n,m) : n*m matrix of ones
• eye(n) : n*n identity matrix
• rand(n) : n*n random matrix
Flow Control
• Matlab has five flow control constructs:
1. if statement
2. for loop
3. while loop
4. break statement
if• If statement condition
• The general form of the IF statement is
IF expression
statements
ELSEIF expression
statements
ELSE
statements
END
• Example:
s = input(‘Please enter a scalar value =‘);
if s > 1
error(‘Error!’);
else
disp(‘ok’);
end
for• FOR repeats statements a specific number of
times
• The general form of a FOR statement is:
FOR variable = expression
statements
END
while• WHILE repeats statements an indefinite number
of times
• The general form of a WHILE statement is:
• WHILE expression
• statements
• END
Scripts and Functions
• There are two kinds of M-files:
• scripts, which do not accept input arguments or return output arguments. They operate on data in the workspace.
• Functions, which can accept input arguments and return output arguments. Internal variables are local to the function.
functionfunction [ output_args ] = functionName( input_args )
%functionName Summary of this function goes here
% Detailed explanation goes here
end
Visualization and Graphics
• Plot ( x, y) : plot 1D function
• figure : open a new figure
• hold on, hold off : refreshing
• title(‘figure title) : add title to figure
Image import and Export• Read and write images in Matlab:
image = imread(‘image fileName.format');
figure;
imshow(image);
imwrite(image, ‘new image filename.format');
Images and Matricesrow = 256;
col = 256;
img = zeros(row, col);
img(100:105, :) = 0.5;
img(:, 100:105) = 1;
figure;
imshow(img);
Column 1 to 256
Row 1 to 256
o
[0, 0]
o
[256, 256]
Histograms
• Frequency of the intensity value of the image
• Quantize frequency into intervals
• Probability density function of image intensities
Image Filtering
• Let’s replace each pixel with a weighted average of its neighborhood.
• The widths are called the filter kernel
• What are the weighs for the average of a 3*3 neighborhood?
111
111
111
“box filter”
Image Filtering
I = imread(‘filename.format file’);
h = fspecial(‘unsharp’);
I2 = imfilter(I,h);
imshow(I2);
Top Related