+ All Categories
Home > Documents > Introduction to Matlab - csc576ksu.files.wordpress.com · I. Introduction to Matlab 4 . Digital...

Introduction to Matlab - csc576ksu.files.wordpress.com · I. Introduction to Matlab 4 . Digital...

Date post: 03-Jul-2020
Category:
Upload: others
View: 7 times
Download: 0 times
Share this document with a friend
25
Introduction to Matlab Dr. Ouiem Bchir
Transcript
Page 1: Introduction to Matlab - csc576ksu.files.wordpress.com · I. Introduction to Matlab 4 . Digital Image Representation 5 . Digital Image Representation Image=2-D function, f(x,y) ...

Introduction to Matlab

Dr. Ouiem Bchir

Page 2: Introduction to Matlab - csc576ksu.files.wordpress.com · I. Introduction to Matlab 4 . Digital Image Representation 5 . Digital Image Representation Image=2-D function, f(x,y) ...

MATLAB stands for MATrix LABoratory

A high performance language for technical computing.

Integrates computation, visualization, and programming in an easy-to-use environment.

Complemented by a family of application-specific functions (M-files)

Image Processing Toolbox

I. Introduction to Matlab

2

Page 3: Introduction to Matlab - csc576ksu.files.wordpress.com · I. Introduction to Matlab 4 . Digital Image Representation 5 . Digital Image Representation Image=2-D function, f(x,y) ...

Workspace

I. Introduction to Matlab

3

Page 4: Introduction to Matlab - csc576ksu.files.wordpress.com · I. Introduction to Matlab 4 . Digital Image Representation 5 . Digital Image Representation Image=2-D function, f(x,y) ...

Matlab desktop Command window:

user types commands

Command output displayed

Workspace browser

Workspace: set of variables the user creates in a work session.

Workspace browser: where variables are displayed.

Current directory window

Shows content of the current directory

Command history window

Contains a record of the commands the user has entered.

Can be selected and re-executed or to create an M-file

Figure windows

Shown only when the user displays graphics.

I. Introduction to Matlab

4

Page 5: Introduction to Matlab - csc576ksu.files.wordpress.com · I. Introduction to Matlab 4 . Digital Image Representation 5 . Digital Image Representation Image=2-D function, f(x,y) ...

Digital Image Representation

5

Page 6: Introduction to Matlab - csc576ksu.files.wordpress.com · I. Introduction to Matlab 4 . Digital Image Representation 5 . Digital Image Representation Image=2-D function, f(x,y) ...

Digital Image Representation

Image=2-D function, f(x,y)

x,y: spatial coordinates

f(x,y): intensity of the image at point (x,y).

Gray level= intensity of monochrome images

Color images = combination of individual images

RGB image = Red, Green, and Blue individual component images.

Digitizing a continuous image requires

Sampling & Quantization

I. Introduction to Matlab

6

Page 7: Introduction to Matlab - csc576ksu.files.wordpress.com · I. Introduction to Matlab 4 . Digital Image Representation 5 . Digital Image Representation Image=2-D function, f(x,y) ...

Coordinate conventions

MxN Image

Many image processing textbooks Image Processing Toolbox

I. Introduction to Matlab

7

Page 8: Introduction to Matlab - csc576ksu.files.wordpress.com · I. Introduction to Matlab 4 . Digital Image Representation 5 . Digital Image Representation Image=2-D function, f(x,y) ...

Intensity Images

A digital image can be represented as a MATLAB ⎡ f(1,1) f(1,2 ) L f(1,N)⎤ matrix: ⎢ f( 2,1)

f = ⎢ ⎢ M ⎢

1 ⎣ f(M, )

f( 2,2 ) L f( 2,N)⎥ ⎥

M M ⎥ ⎥

f(M,2 ) L f(M,N)⎦

⎡35 45 20⎤ ⎢43 64 52⎥ ⎢ ⎥ ⎢10 29 39⎥

I. Introduction to Matlab

8

Page 9: Introduction to Matlab - csc576ksu.files.wordpress.com · I. Introduction to Matlab 4 . Digital Image Representation 5 . Digital Image Representation Image=2-D function, f(x,y) ...

Color Images

R(x,y)

G(x,y)

B(x,y)

I. Introduction to Matlab

9

Page 10: Introduction to Matlab - csc576ksu.files.wordpress.com · I. Introduction to Matlab 4 . Digital Image Representation 5 . Digital Image Representation Image=2-D function, f(x,y) ...

Reading Images

Imread (‘filename’) >> f = imread (‘chestxray.jpg’); (from current dir)

>> f = imread (‘D:\myimageschestxray.jpg’);

Some of the popular image/graphics format supported by imread and imwrite.

I. Introduction to Matlab

10

Page 11: Introduction to Matlab - csc576ksu.files.wordpress.com · I. Introduction to Matlab 4 . Digital Image Representation 5 . Digital Image Representation Image=2-D function, f(x,y) ...

>> size (f) ans =

1024 1024

For further processing

>> [M N] = size (f)

>> whos f

Name Size Bytes Class

f 1024x1024 1048567 uint8 array

Grand total is 108576 elements using 1048576 bytes

I. Introduction to Matlab

11

Page 12: Introduction to Matlab - csc576ksu.files.wordpress.com · I. Introduction to Matlab 4 . Digital Image Representation 5 . Digital Image Representation Image=2-D function, f(x,y) ...

Displaying Images

imshow (f, G) G: number of intensity levels used to display f (default = 256

levels).

imshow (f, [low high]) values <= low displayed black

values >= high displayed white

values in between displayed as intermediate intensity values using the default No. of levels

imshow (f, [ ]) low = minimum value of array f

high = maximum value of array f

pixval Display intensity (RGB) values of individual pixels interactively

I. Introduction to Matlab

12

Page 13: Introduction to Matlab - csc576ksu.files.wordpress.com · I. Introduction to Matlab 4 . Digital Image Representation 5 . Digital Image Representation Image=2-D function, f(x,y) ...

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

>> imshow (f)

Can process the image:

Scale, save, export,

edit, format, print, …

I. Introduction to Matlab

13

Page 14: Introduction to Matlab - csc576ksu.files.wordpress.com · I. Introduction to Matlab 4 . Digital Image Representation 5 . Digital Image Representation Image=2-D function, f(x,y) ...

>> imshow (g)

MATLAB replaces the image in the screen with the new image.

To keep the first image and display another image

>> figure

>> imshow (g)

To display in a specific window

>> figure (3), imshow (g)

I. Introduction to Matlab

14

Page 15: Introduction to Matlab - csc576ksu.files.wordpress.com · I. Introduction to Matlab 4 . Digital Image Representation 5 . Digital Image Representation Image=2-D function, f(x,y) ...

imshow (h) imshow (h, [ ] )

I. Introduction to Matlab

15

Page 16: Introduction to Matlab - csc576ksu.files.wordpress.com · I. Introduction to Matlab 4 . Digital Image Representation 5 . Digital Image Representation Image=2-D function, f(x,y) ...

Writing Images

>> imwrite (f, ‘filename’)

‘filename’ must include file format extension

>> imwrite (f, ‘patient10_run1’, ‘tif’)

or

>> imwrite (f, ‘patient10_run1.tif ’)

Can have parameters depending on the file format

>> imwrite (f, ‘filename.jpg’, ‘quality’, q)

q: integer between 0 and 100 (lower higher quality).

I. Introduction to Matlab

16

Page 17: Introduction to Matlab - csc576ksu.files.wordpress.com · I. Introduction to Matlab 4 . Digital Image Representation 5 . Digital Image Representation Image=2-D function, f(x,y) ...

Vector Indexing

>> v = [1 3 5 7 9];

>> v(2)

ans = 3

>> w = v.’ %transpose operator

w= 1

3

5

7

9

>> v(1:3)

ans = 1 3 5

>> v (3:end)

ans = 5 7 9

>> v ( : )

>> v (1:end)

% produces a column vector

% produces a raw vector

I. Introduction to Matlab

17

Page 18: Introduction to Matlab - csc576ksu.files.wordpress.com · I. Introduction to Matlab 4 . Digital Image Representation 5 . Digital Image Representation Image=2-D function, f(x,y) ...

>> v(1:2:end)

ans = 1 5 9

>> v(end:-2:1)

ans = 9 5 1

x = linspace (a, b, n)

generates a row vector x of n elements linearly spaced between a & b

A vector can be used as an index

>> v([1 4 5])

ans = 1 7 9

I. Introduction to Matlab

18

Page 19: Introduction to Matlab - csc576ksu.files.wordpress.com · I. Introduction to Matlab 4 . Digital Image Representation 5 . Digital Image Representation Image=2-D function, f(x,y) ...

Matrix Indexing

Represented as a sequence of row vectors.

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

A= 1 2 3

4 5 6

7 8 9

>> A(2,3)

ans = 6

>> C3 = A (:,3)

C3 = 3

6

9

% same as A(1:3, 3)

I. Introduction to Matlab

19

Page 20: Introduction to Matlab - csc576ksu.files.wordpress.com · I. Introduction to Matlab 4 . Digital Image Representation 5 . Digital Image Representation Image=2-D function, f(x,y) ...

>> R2 = A(2, :) R2 = 4 5 6

>> T2 = A (1:2, 1:3);

>> B = A;

>> B(:, 3) = 0

B= 1 2 0

4 5 0

7 8 0

>> A (2:end, end:-2:1)

ans = 6 4

9 7

% top 2 rows

I. Introduction to Matlab

20

Page 21: Introduction to Matlab - csc576ksu.files.wordpress.com · I. Introduction to Matlab 4 . Digital Image Representation 5 . Digital Image Representation Image=2-D function, f(x,y) ...

>> D = logical ([1 0 0; 0 0 1; 0 0 0]);

>> A(D)

ans = 1

6

>> v = T2(:) % All elements column by column

v= 1

4

2

5

3

6

>> s = sum(A(:))% sum of all elements

s = 45

>> s = sum(A)

s = 12 15 18

>> s = sum(sum(A))

s = 45

Elements of a matrix are stored as a 1-D vector A(3) = 7; A(8) = 6; …

I. Introduction to Matlab

21

Page 22: Introduction to Matlab - csc576ksu.files.wordpress.com · I. Introduction to Matlab 4 . Digital Image Representation 5 . Digital Image Representation Image=2-D function, f(x,y) ...

f

1024 x 1024

f (end: -1: 1, :) f(257:768,257:768)

f(1:2:end, 1:2:end)

Plot ( f (512, :) )

I. Introduction to Matlab

22

Page 23: Introduction to Matlab - csc576ksu.files.wordpress.com · I. Introduction to Matlab 4 . Digital Image Representation 5 . Digital Image Representation Image=2-D function, f(x,y) ...

Standard Arrays

zeros (M, N): MxN matrix of 0’s of class double

ones (M, N): MxN matrix of 1’s of class double

true (M,N): MxN logical matrix of 1’s

false (M,N): logical matrix of 0’s

rand (M,N): random elements, uniformly distributed in [0, 1]

randn (M,N): random elements, normally distributed with mean 0 and variance 1.

I. Introduction to Matlab

23

Page 24: Introduction to Matlab - csc576ksu.files.wordpress.com · I. Introduction to Matlab 4 . Digital Image Representation 5 . Digital Image Representation Image=2-D function, f(x,y) ...

M-Files

Scripts that execute a series of MATLAB

statements.

Functions that can accept arguments

and can produce outputs.

I. Introduction to Matlab

24

Page 25: Introduction to Matlab - csc576ksu.files.wordpress.com · I. Introduction to Matlab 4 . Digital Image Representation 5 . Digital Image Representation Image=2-D function, f(x,y) ...

•There is much more stuff !!!

•Check textbook website & other

links for tutorials, demos, …

I. Introduction to Matlab

25


Recommended