You are on page 1of 39

Assist. Prof. Dr. Rasper Dh.

Rashid
E-mail : rasber.dhahir@ukh.edu.krd
Master Students / Computer Science
2022-2023

Dr.Rasper Dh. Rashid


Accessing Matlab
 By default, matlab will open a console with a file
browser and command history window along with a
window to input commands.
Menu bar
Tool bar Current directory

Current folder
Work space

Command window

History command

Dr.Rasper Dh. Rashid


Matrix and Vector Indexing
 Parentheses are used for subscripts
 Indexes can be scalars, vectors, or matrices.
 To refer to an entire row or column of a matrix, replace the
other index with a colon (:).
 The reserved keyword end can be used to refer to the last
element in an object
>> vec = [1 3 5 7];
>> vec(end + 1) = 4
vec =
1 3 5 7 4
>> mat = [7 2 3; 4 5 9; 8 12 1];
>> disp(mat(2,:))
4 5 9
Dr.Rasper Dh. Rashid
Matrix and Vector Indexing (Cont.)
 Single rows and columns can be assigned without loops.
>> mat(2,:) = [10 11 12];
>> mat(:,2) = [10 11 12];
>> disp(mat)
7 10 3
10 11 12
8 12 1
 To replace more than one row or column, the replacement must conform
to the target:
>> mat(1:2,:) = [100 101 102; 200 201 202];
>> mat(:,1:2) = [100 101 102; 200 201 202]’;
>> disp(mat)
mat =
100 200 102
101 201 202
102 202 1
Dr.Rasper Dh. Rashid
Matrix and Vector Indexing (Cont.)
 To specify contiguous indices, the colon operator (:) can be used.
 The two forms of the operator are start:finish and
start:increment:finish.
>> mat1 = [1 7;2 3;4 5;9 12];
>> mat1(2:3,:)
ans =
2 3
4 5
 The colon operator can also be used to create vectors containing
sequences.
 For non-contiguous indices, ordinary vectors can be used:
>> mat1([1 3 4],:)
ans =
1 7
4 5
9 12
Dr.Rasper Dh. Rashid
Removing Elements of Matrices and Vectors
 To remove a row or column of a matrix, set its value to an empty vector:
>> x = [7 2 4;9 8 12;4 7 8;12 1 4];
>> x(:,2) = []
x=
7 4
9 12
4 8
12 4
 The same technique can be used to remove single vector elements:
>> y = [13 7 12 9 15 8];
>> y(3) = []
y=
13 7 9 15 8

Dr.Rasper Dh. Rashid


Some Basic Matrix Functions
 zeros - create a matrix of all zeroes
>> zeros(2)
ans =
0 0
0 0
>> zeros(2,3)
ans =
0 0 0
0 0 0
 ones - create a matrix of all ones
>> ones(3)
ans =
1 1 1
1 1 1
1 1 1
 eye - create identity matrix
>> eye(3)
ans =
1 0 0
0 1 0
Dr.Rasper Dh. Rashid
0 0 1
Some Basic Matrix Functions (Cont.)
 diag - create diagonal matrix or extract diagonal elements
>> x = [1 2 3 ;10 20 30;100 200 300];
>> diag(x)
ans =
1
20
300
 rand - create matrix of uniform random numbers
>> rand(2)
ans =
0.6541 0.7482
0.6892 0.4505
>> rand(2,3)
ans =
0.0838 0.9133 0.8258
0.2290 0.1524 0.5383

Dr.Rasper Dh. Rashid


Some Basic Matrix Functions (Cont.)
 reshape - change dimensions of matrix or vector
>> reshape(x,1,9)
ans =
1 10 100 2 20 200 3 30 300
>> reshape(x,9,1)
ans =
1
10
100
2
20
200
3
30
300

Dr.Rasper Dh. Rashid


Logical Subscripts
 Logical expressions on vectors or matrices result in
logical variables of the same size and shape. These
expressions can be used to extract elements which
meet a particular condition.
>> x = rand(10,1) * 10;
>> x(x>5)
ans =
7.0605 8.2346 6.9483 9.5022
>> length(x(x<5))
ans =
6

Dr.Rasper Dh. Rashid


Operators

Dr.Rasper Dh. Rashid


Using Functions
 Functions can return single or multiple values.
 Most functions can operate on vectors or matrices
returning an object of the same size by applying the
function to each element.
>> x = [9 17 24 15 12 82];
>> sx = sqrt(x)
sx =
3.0000 4.1231 4.8990 3.8730 3.4641 9.0554
 When some functions (mean, max, sum, median, prod,
sort, std, etc.) are used with matrices, they automatically
operate on each column.

Dr.Rasper Dh. Rashid


Functions: Max and Min
>> y=[2 4 6 8 5];
>> max(y)
ans =
8
>> x=[2 3 5;5 6 8;9 5 2];
>> max(x)
ans =
9 6 8
>> max(x(:))
ans =
9

>> min(x)
ans =
2 3 2
>> min(x(:))
ans =
2

Dr.Rasper Dh. Rashid


Functions: Sort
 In its simplest form the sort command sorts a vector:
>> x = [9 7 14 12 15 8];
>> sort(x)
ans =
7 8 9 12 14 15
 An optional character variable can be used for a
descending sort:
>> sort(x,’descend’)
ans =
15 14 12 9 8 7
Dr.Rasper Dh. Rashid
Functions: Sum
 Sum of array elements
>> x=[1 3 5 6 7];
>> sum(x)
ans =
22

>> y = [12 19 15;7 8 12;14 5 9;10 16 2];


>> sum(y)
ans =
43 48 38

>> sum(y(:))
ans =
129

Dr.Rasper Dh. Rashid


Functions: Mean
 Averages of all observations
>> x=[1 3 5 6 7];
>> mean(x)
ans =
4.4000
>> y = [12 19 15;7 8 12;14 5 9;10 16 2];
>> mean(y)
ans =
10.7500 12.0000 9.5000
>> mean(y(:))
ans =
10.7500

Dr.Rasper Dh. Rashid


Functions: Median & Mode
>> x=[11 23 5 16 7];
>> median(x)
ans =
11

>> z=[2 4 6 3 2 5 3 2]
>> mode(z)
ans =
2

Dr.Rasper Dh. Rashid


Graphics
 The basic use of the plotting commands is very
straight forward.
>> x = [17 19 24 29 32 38];
>> y = [25 29 32 38 40 44];
>> plot(x,y)

Dr.Rasper Dh. Rashid


Graphics: Colors, Markers and Linestyles
 Graphics properties can be modified by combining
symbols from the following columns into a third
argument to plot function:

Dr.Rasper Dh. Rashid


Graphics: Colors, Markers and Linestyles(Cont.)
>> x = [17 19 24 29 32 38];
>> y = [25 29 32 38 40 44];
>> plot(x,y,’ro--’)

 By providing additional vectors to the plot command,


it’s easy to put multiple plots in a single figure.
>> a = [7 14 19 21 25 29 34];
>> b = [12 19 25 29 32 35 40];
>> plot(x,y,’ro--’,a,b,’b^-’)

Dr.Rasper Dh. Rashid


Graphics: Adding Information to Plots
 The following functions and commands are available:
 xlabel, ylabel - add x- or y- axis labels
 axis - control axes properties
 grid - use grid on or grid off
 box - use box on or box off
 title - add a title above the plot
 text - add text to a plot
 annotation - annotate a plot
 legend - add a legend to a plot

Dr.Rasper Dh. Rashid


Graphics: Adding Information to Plots(Cont.)

>> x = [17 19 24 29 32 38];


>> y = [25 29 32 38 40 44];
>> a = [7 14 19 21 25 29 34];
>> b = [12 19 25 29 32 35 40];
>> plot(x,y,'ro--',a,b,'b^-')
>> title('Plot of 2 lines')
>> xlabel('X variable')
>> ylabel('Y variable')
>> legend({'line 1' 'line 2‘})
Dr.Rasper Dh. Rashid
Plot Multiple Figures
 The subplot function allows you to divide the plotting
figure into subplots.
 The basic form of the command is
subplot(nrows,ncols,nwhich)
 This sets the current plotting region to the nwhich-th
plot within an nrows by ncols grid, counting by rows.

Dr.Rasper Dh. Rashid


Plot Multiple Figures (Cont.)
 To create a 3 × 2 grid of trigonometric functions on a single figure:

>> xvals = linspace(-pi,pi,100);


>> subplot(3,2,1)
>> plot(xvals,sin(xvals))
>> subplot(3,2,2)
>> plot(xvals,cos(xvals))
>> subplot(3,2,3)
>> plot(xvals,tan(xvals))
>> subplot(3,2,4)
>> plot(xvals,sinh(xvals))
>> subplot(3,2,5)
>> plot(xvals,cosh(xvals))
>> subplot(3,2,6)
>> plot(xvals,tanh(xvals))

Dr.Rasper Dh. Rashid


Images in Matlab
 Matlab is optimised for operating on matrices
 Images are matrices!
 Many useful built-in functions in the Matlab Image
Processing Toolbox
 Very easy to write your own image processing functions

Dr Rasper Dh. Rashid


Images in MATLAB
• MATLAB can import/export
several image formats: • Data types in MATLAB
– Double (64-bit double-
– BMP (Microsoft Windows Bitmap)
precision floating point)
– GIF (Graphics Interchange Files)
– Single (32-bit single-precision
– HDF (Hierarchical Data Format) floating point)
– JPEG (Joint Photographic – Int32 (32-bit signed integer)
Experts Group)
– Int16 (16-bit signed integer)
– PCX (Paintbrush)
– Int8 (8-bit signed integer)
– PNG (Portable Network
– Uint32 (32-bit unsigned integer)
Graphics)
– Uint16 (16-bit unsigned integer)
– TIFF (Tagged Image File Format)
– Uint8 (8-bit unsigned integer)
– XWD (X Window Dump)
– raw-data and other types of
image data

Dr Rasper Dh. Rashid


Binary Image

Dr Rasper Dh. Rashid


Greyscale Image

Dr Rasper Dh. Rashid


Color Image

Dr Rasper Dh. Rashid


Loading and displaying images
>> I=imread('mandrill.bmp','bmp'); % load image

image format as a
Matrix with image image filename as a string
data string/ Path +file name

>> image(I) % display image


>> whos I

Name Size Bytes Class

I 512x512x3 786432 uint8 array

Grand total is 786432 elements using 786432 bytes

Matlab can only


perform arithmetic
Dimensions of I (red, green
operations on data with
and blue intensity
class double!
information)
Dr Rasper Dh. Rashid
Representation of Images
 Images are just an array of numbers
>> I % ctrl+c to halt output!

 Intensity of pixel is represented by the pixel element’s


value in the red, green and blue matrices
>> I(1,1,:) % RGB values of element (1,1)
ans(:,:,1) = Red
135 Images where the pixel value in the image
represents the intensity of the pixel are
ans(:,:,2) =
called intensity images.
97 Green
ans(:,:,3) =
33 Blue

Dr Rasper Dh. Rashid


Image Import and Export
• Read and write images in Matlab
Write Image

imwrite(image,filename,fmt)
• Ex:
img = imread('apple.jpg');
figure;
imshow(img);
imwrite(img, 'output.bmp', 'bmp');

Dr Rasper Dh. Rashid


• Function size gives the row and column dimentions of an
image:
>>size(f)
>>[M, N]=size(f );
• Alternatives to imshow
imagesc(I)
imtool(I)
image(I)
Displaying Images

>>f=imread(’rose_512.tif’);
>> imshow(f)

Dr Rasper Dh. Rashid


>> f=imread(’rose_512.tif’);
>> g=imread(’cktboard.tif’);
>> imshow(f), figure, imshow(g)

Dr Rasper Dh. Rashid


Writing Images
imfinfo filename
>> imfinfo bubbles25.jpg
ans =
Filename: ’bubbles25.jpg’

FileModDate: ’02-Feb-2005 09:34:50’


FileSize: 13354
Format: ’jpg’
FormatVersion: ’’
Width: 720
Height: 688
BitDepth: 8
ColorType: ’grayscale’
FormatSignature: ’’
NumberOfSamples: 1
CodingMethod: ’Huffman’
CodingProcess: ’Sequential’
Comment: {}
Dr Rasper Dh. Rashid
Converting between Image Classes and Types
Name Converts Input to: Valid Input Image
Data Classes
im2uint8 uint8 logical, uint8,
uint16, and double
im2uint16 uint16 logical, uint8,
unit16, and double
mat2gray double (in range [0,1]) double
im2double double logical, uint8,
uint16, and double
im2bw logical uint8, uint16, and
double

Dr Rasper Dh. Rashid


Example

>> f=[-0.5 0.5;0.75 1.5]

f =

-0.5000 0.5000
0.7500 1.5000
>> g=im2uint8(f)

g =

0 128
191 255
Dr Rasper Dh. Rashid
>> h=uint8([25 50; 128 200]);
>> g=im2double(h)

g =

0.0980 0.1961
0.5020 0.7843

Dr Rasper Dh. Rashid


Other Image Conversions
• gray2ind - intensity image to index image
• im2bw - image to binary
• im2double - image to double precision
• im2uint8 - image to 8-bit unsigned integers
• im2uint16 - image to 16-bit unsigned integers
• ind2gray - indexed image to intensity image
• mat2gray - matrix to intensity image
• rgb2gray - RGB image to grayscale
• rgb2ind - RGB image to indexed image

Dr Rasper Dh. Rashid

You might also like