Intro matlab and convolution islam

43
Introduction to MATLAB and image processing Amin Allalou [email protected] Centre for Image Analysis Uppsala University Computer Assisted Image Analysis April 4 2008

description

 

Transcript of Intro matlab and convolution islam

Page 1: Intro matlab and convolution islam

Introduction to MATLAB and image processing

Amin [email protected]

Centre for Image AnalysisUppsala University

Computer Assisted Image AnalysisApril 4 2008

Page 2: Intro matlab and convolution islam

MATLAB and images • The help in MATLAB is very good, use it!• An image in MATLAB is treated as a matrix• Every pixel is a matrix element• All the operators in MATLAB defined on

matrices can be used on images: +, -, *, /, ^, sqrt, sin, cos etc.

Page 3: Intro matlab and convolution islam

Images in MATLAB• MATLAB can import/export

several image formats– BMP (Microsoft Windows Bitmap)– GIF (Graphics Interchange Files)– HDF (Hierarchical Data Format)– JPEG (Joint Photographic Experts

Group)– PCX (Paintbrush)– PNG (Portable Network Graphics)– TIFF (Tagged Image File Format)– XWD (X Window Dump)– MATLAB can also load raw-data or

other types of image data

• Data types in MATLAB– Double (64-bit double-precision

floating point)– Single (32-bit single-precision

floating point)– Int32 (32-bit signed integer)– Int16 (16-bit signed integer)– Int8 (8-bit signed integer)– Uint32 (32-bit unsigned integer)– Uint16 (16-bit unsigned integer)– Uint8 (8-bit unsigned integer)

Page 4: Intro matlab and convolution islam

Images in MATLAB• Binary images : {0,1}• Intensity images : [0,1] or uint8, double etc. • RGB images : m-by-n-by-3• Indexed images : m-by-3 color map• Multidimensional images m-by-n-by-p (p is the number of layers)

Page 5: Intro matlab and convolution islam

Image import and export• Read and write images in Matlab

>> I=imread('cells.jpg');>> imshow(I)>> size(I)ans = 479 600 3 (RGB image)>> Igrey=rgb2gray(I);>> imshow(Igrey)>> imwrite(lgrey, 'cell_gray.tif', 'tiff')

Alternatives to imshow>>imagesc(I)>>imtool(I)>>image(I)

Page 6: Intro matlab and convolution islam

Images and Matrices• How to build a matrix (or image)?

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

4 5 67 8 9

>> B = zeros(3,3) B = 0 0 0

0 0 00 0 0

>> C = ones(3,3) C = 1 1 1

1 1 11 1 1

>>imshow(A) (imshow(A,[]) to get automatic pixel range)

Page 7: Intro matlab and convolution islam

Images and Matrices• Accesing image elements (row, column)

>> A(2,1)ans = 4

• : can be used to extract a whole column or row>> A(:,2)ans =2

5 8• or a part of a column or row

>> A(1:2,2)ans =2

5

X

Y

A = 12 345 67 8 9

Page 8: Intro matlab and convolution islam

Image Arithmetic• Arithmetic operations such as addition, subtraction, multiplication and division

can be applied to images in MATLAB– +, -, *, / performs matrix operations

>> A+Aans = 2 4 6

8 10 12 14 16 18

>> A*Aans = 30 36 42

66 81 96 102 126 150

• To perform an elementwise operation use . (.*, ./, .*, .^ etc)>> A.*Aans = 1 4 9

16 25 36 49 64 81

A = 12 345 67 8 9

Page 9: Intro matlab and convolution islam

Logical Conditions• equal (==) , less than and greater than (< and >), not equal (~=) and not (~)• find(‘condition’) - Returns indexes of A’s elements that satisfies the

condition.>> [row col]=find(A==7)row = 3col = 1>> [row col]=find(A>7)row = 3

3col = 2

3>> Indx=find(A<5)Indx = 1

2 4 7

A = 12 345 67 8 9

Page 10: Intro matlab and convolution islam

Flow Control• Flow control in MATLAB

- if, else and elseif statements(row=1,2,3 col=1,2,3)if row==col

A(row, col)=1;elseif abs(row-col)==1

A(row, col)=2;else

A(row, col)=0;end

A =

1 2 0 2 1 2 0 2 1

Page 11: Intro matlab and convolution islam

Flow Control• Flow control in MATLAB

- for loops

for row=1:3for col=1:3 if row==col

A(row, col)=1;elseif abs(row-col)==1

A(row, col)=2;else

A(row, col)=0;end

endend

A =

1 2 0 2 1 2 0 2 1

Page 12: Intro matlab and convolution islam

Flow Control• while, expression, statements, end

Indx=1;while A(Indx)<6

A(Indx)=0;Indx=Indx+1;

end

A = 12 345 67 8 9

A =

0 2 3 0 5 6 7 8 9

Page 13: Intro matlab and convolution islam

Working with M-Files• M-files can be scripts that simply execute a series of MATLAB statements,

or they can be functions that also accept input arguments and produce output.

• MATLAB functions:– Are useful for extending the MATLAB language for your application.– Can accept input arguments and return output arguments.– Store variables in a workspace internal to the function.

Page 14: Intro matlab and convolution islam

Working with M-Files• Create a new empty m-file

function B=test(I)[row col]=size(I)for r=1:row

for c=1:col if r==c

A(r, c)=1;elseif abs(r-c)==1

A(r, c)=2;else

A(r, c)=0;end

endend

B=A;

Page 15: Intro matlab and convolution islam
Page 16: Intro matlab and convolution islam
Page 17: Intro matlab and convolution islam
Page 18: Intro matlab and convolution islam

Linear Systems

Page 19: Intro matlab and convolution islam
Page 20: Intro matlab and convolution islam

Superposition theorem

• The superposition theorem for electrical circuits states that for a linear system the response (Voltage or Current) in any branch of a bilateral linear circuit having more than one independent source equals the algebraic sum of the responses caused by each independent source acting alone, while all other independent sources are replaced by their internal impedances.

Page 21: Intro matlab and convolution islam

Requirements for Linearity

• A system is called linear if it has two mathematical properties:

1.Homogeneity2.Additivity3.Shift invariance (where “Shift invariance” is not a strict

requirement for linearity, but it is a mandatory property for most DSP techniques.)

Page 22: Intro matlab and convolution islam

Homogeneity

Page 23: Intro matlab and convolution islam

Additivity

Page 24: Intro matlab and convolution islam

Shift invariance

Page 25: Intro matlab and convolution islam

Superposition: the Foundation of DSP

Page 26: Intro matlab and convolution islam
Page 27: Intro matlab and convolution islam
Page 28: Intro matlab and convolution islam

Decomposition & Convolution

• While many different decompositions are possible, two form the backbone of signal processing:

1.Impulse decomposition 2.Fourier decomposition. • When impulse decomposition is used, the

procedure can be described by a mathematical operation called convolution.

Page 29: Intro matlab and convolution islam
Page 30: Intro matlab and convolution islam

Definition of delta function (unit impulse) and impulse response

Page 31: Intro matlab and convolution islam

Shifted & Scaled Delta function

Page 32: Intro matlab and convolution islam

Convolution

If the system being considered is a filter, the impulse response is called the filter kernel, the convolution kernel, or simply, the kernel. In image processing, the impulse response is called the point spread function.

Page 33: Intro matlab and convolution islam

Correlation

Page 34: Intro matlab and convolution islam

Correlation

Correlation is a mathematical operation that is very similar to convolution.

Just as with convolution, correlation uses two signals to produce a third signal.

This third signal is called the cross-correlation of the two input signals.

If a signal is correlated with itself, the resulting signal is instead called the autocorrelation.

Page 35: Intro matlab and convolution islam

The formula essentially slides the function along the x-axis, calculating the integral of their product at each position. When the functions match, the value of is maximized. This is because when peaks (positive areas) are aligned, they make a large contribution to the integral. Similarly, when troughs (negative areas) align, they also make a positive contribution to the integral because the product of two negative numbers is positive

Correlation

Page 36: Intro matlab and convolution islam

Convolution In MATLAB

• Linear filtering of an image is accomplished through an operation called convolution. Convolution is a neighborhood operation in which each output pixel is the weighted sum of neighboring input pixels. The matrix of weights is called the convolution kernel, also known as the filter. A convolution kernel is a correlation kernel that has been rotated 180 degrees.

Page 37: Intro matlab and convolution islam
Page 38: Intro matlab and convolution islam
Page 39: Intro matlab and convolution islam
Page 40: Intro matlab and convolution islam
Page 41: Intro matlab and convolution islam
Page 42: Intro matlab and convolution islam

Go to Matlab …..

Page 43: Intro matlab and convolution islam

Thanks,,,