digital image processing matlab basics

46
Digital Image Processing Matlab Basics based on Elboher ‘s slides , HUJI Istructor: Abed Asi

Upload: waylon

Post on 24-Feb-2016

120 views

Category:

Documents


2 download

DESCRIPTION

Digital Image Processing Matlab Basics. based on Elboher ‘s slides , HUJI. Istructor : Abed Asi. Matlab.  It allows one to perform numerical calculations, and visualize the results without the need for complicated and time consuming programming. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Digital Image Processing Matlab  Basics

Digital Image ProcessingMatlab Basics

based on Elboher ‘s slides , HUJI

Istructor: Abed Asi

Page 2: Digital Image Processing Matlab  Basics

 It allows one to perform numerical calculations, and visualize the results without the need for complicated and time consuming programming.

Every variable in Matlab is a multidimensional matrix.

Highly modular.

No memory allocation is necessary.

Matlab enables its own garbage collection.

Simple interface for complex mathematical concepts.

Enables OO Programming.

Matlab

Page 3: Digital Image Processing Matlab  Basics

>> help help (explain how to get help)>> helpbrowser / doc (open the online Matlab documentation)>> help images (list of all commands in the Image Processing Toolbox)>> demo (supply various guide videos) >> lookfor read (display list of functions with ‘read’ in the name or help

text)>> type imread (display contents of file)>> help imread (function name + % block)>> doc imread (function documentation in help browser)

Using [tab] is useful to auto-complete function names and variables

Getting Help

Page 4: Digital Image Processing Matlab  Basics

Digital image representation : 2D function f(x,y) -> finite discrete quantities

Coordinate Conventions:img(r,c)r–rows (height)c–cols (width)

>> size(img) The first pixel:

img(1,1)

Matlab Basics

Page 5: Digital Image Processing Matlab  Basics

Image Types

Checking the image type : isind, isbw, isgray, isrgbConverting image types: rgb2ind, rgb2gray, gray2ind, ind2gray,….

• Intensity images scaled to represent intensities (uint8 – [0,255], double [0,1])

• Binary images logical array of 0s and 1s

• Indexed images Look up table [x, map]

• RGB images truecolor, array of (m*n*3)

Page 6: Digital Image Processing Matlab  Basics

>> f = imread(‘filename’); filename is a string including the file type (jpg, tiff,…); is used for suppressing output

>> [height, width] = size(f);

>> whos f display additional information about an arrayName Size Bytes Class f 512x512x3 786432 uint8 arrayGrand total is 786432 elements using 786432 byte

Reading Images

Page 7: Digital Image Processing Matlab  Basics

>> imshow(f) display the image f according to its type

>> imshow(f, [low high])display as black all values less than ‘low’ and as white all values greater or equal to ‘high’ (in grayscale images)

>> imshow(f, [])set low and high as the minimal and maximal values of array f (in grayscale images)

>> impixelinfodisplay intensity value of individual pixel interactively

>> figure(2), imshow(g)Open a new figure before displaying the image (the default – using the same figure)

Displaying Images

Page 8: Digital Image Processing Matlab  Basics

>> imwrite(f, ‘filename’) f is an image array‘filename’ must include the file format (tif, jpg, bmp,..)

>> k = imfinfo(‘test1.jpg’)Filename: 'test1.jpg'FileModDate: '22-Oct-2005 13:07:36'FileSize: 3464Format: 'jpg'

FormatVersion: ''Width: 256Height: 256BitDepth: 24ColorType: 'truecolor'

FormatSignature: ''Comment: {}

The answer is a structure variable with different fields: k.Width

Writing Images

Page 9: Digital Image Processing Matlab  Basics

Data Classes

Converting between types : B = data_class_name(A)for example: B = double(A)

Page 10: Digital Image Processing Matlab  Basics

When converting between data classes and types it is important to keep the value range for each data class

>> img = double(img)/255;>> img = im2double(img);

Conversions

Page 11: Digital Image Processing Matlab  Basics

Matlab variables do not need to be declared in advance.

‘ans’ is a defined variable containing the last result

Memory is allocated and freed automatically.>> A = [1 2 3; 2 3 4; 3 4 5];

>> A = 0.0005

Variable Decleration and Memory issues

Page 12: Digital Image Processing Matlab  Basics

row vector (1xN) >> v = [1 3 5 7]; (elements separated by space or comma (,))>> v(2) = 3;

column vector (MX1)>> w = [1;3;5;7]; (elements separated semi-comma (;))>> w = v’ (transpose operation)w =1357

To Access blocks of elements we use colon notation>> v(2:4)ans = 3 5 7>> v(1:end) end is the last element in the vector>> v(:) produce a column vector>> v(1:2:end) enables steps (jumps)>> v(end:-2:1) steps can be negative as well

Vector can be used as an index into another vector>> v([1 3 4])ans =

1 5 7

Vector indexing

Page 13: Digital Image Processing Matlab  Basics

Image – 2D array, matrix Matrix can be represented as a sequence of row vectors

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

1 2 34 5 6

7 8 9 To access an element, 2 indexes are used – row index and column index

>> A(2,3) 6>> A(:,3)369>> A(2,:)4 5 6>> a(1:2,1:3)1 2 34 5 6

>> B = A;>> B(:,3) = 0B =

1 2 04 5 0

7 8 0 Using vectors to index into a matrix provide a powerful tool for element selection

A([1 3], [2 3]) 2 3

8 9

Matrix Indexing

Page 14: Digital Image Processing Matlab  Basics

14

Image – 2D array, matrix A matrix is also represented as a long vector

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

1 2 34 5 6

7 8 9 To access sequential elements in a matrix a single index can also be used:

>> A(1:9)A =

1 4 7 2 5 8 3 6 9

Sometimes changing the shape of the matrix is of use:>> reshapse(A(1:2,1:3), 3, 2)A =

1 54 32 6

The size of the output matrix is the size of the index matrix!

Matrix Indexing Cont.

Page 15: Digital Image Processing Matlab  Basics

15

A very useful approach is to use logical matrix as an index to the matrix>> D = logical([1 0 0; 0 0 1; 0 0 0])

D= 1 0 0 0 0 1 0 0 0

>> A(D)ans=

1 6

The use of column operation on a matrix produce a single column vector from the matrix (on a column by column basis). It is very useful for image operations like sum or max>> s = sum(f(:)) (equivalent to: sum(sum(f)))

Matrix Addressing

Page 16: Digital Image Processing Matlab  Basics

16

Arithmetical operators have their algebraic meaning:>> A = [1 2 3; 4 5 6; 7 8 9];>> A + Aans =

2 4 68 10 1214 16 18

>> A * [1 1 1]??? Error using ==> timesMatrix dimensions must agree

>> A * [1 1 1]’ans =

61524

Operators

Page 17: Digital Image Processing Matlab  Basics

17

Vector multiplication depends on the order of the two vectors>> [1 2 3] * [2 3 4]??? Error using ==> timesMatrix dimensions must agree

>> [1 2 3] * [2 3 4]’ans =

20

>>[1 2 3]’ * [2 3 4]ans =

2 3 44 6 86 9 12

Operators cont.

Page 18: Digital Image Processing Matlab  Basics

18

Element by element operators - .* .^ ./>> [1 2 3] .* [2 3 4]ans =

2 6 12

>> [1 2 3] .^ 2ans =

1 4 9

>> [1 2 3] ./ (2:2:6)ans =

0.5000 0.5000 0.5000

Operators cont.

Page 19: Digital Image Processing Matlab  Basics

19

Matlab arrays can be of any dimensions It is useful to operate on specific dimension of the

array, for example:>> height = size(i,1);

Usually we deal with 2D arrays but there are cases we need to address higher dimensions (such as color images)>> i(200:300, 200:400, 3)

To get the number of dimensions of an array >> d = ndims(f)

Array dimensions

Page 20: Digital Image Processing Matlab  Basics

20

Generating simple array enables trying out simple ideas and test the syntax of a function during development

>> zeros(m,n)>> ones(m,n)>> true(m,n)>> false(m,n)>> magic(m) >> rand(n)>> randn(n)>> pascal(n)

Standard Arrays

Page 21: Digital Image Processing Matlab  Basics

21

Arithmetic operators (numeric computations)◦ matrix arithmetic (linear algebra A*B)◦ array arithmetic (element by element A.*B)

+, -, ./, .^,:.. Relational operators (compare)

◦ Compare corresponding elements of arrays of equal dimensions (<, >,<=, >=, ==, ~=) or an array to scalar

Logical operators can operate both on logical and numeric data (and: &, or: |, not: ~) true: logical 1 or non-zero numeric quantityfalse: logical or numerical 0

logical functions : any, all

Additional Operators

Page 22: Digital Image Processing Matlab  Basics

22

>> i = imread('sowrds0040.bmp');>> i = rgb2gray(double(i)/255);>> imshow(i)

Examples - Matrix indexing

Page 23: Digital Image Processing Matlab  Basics

23

>> i = imread('sowrds0040.bmp');>> i = rgb2gray(double(i)/255);>> imshow(i)

Examples - Matrix indexing

Page 24: Digital Image Processing Matlab  Basics

24

>> i = imread('sowrds0040.bmp');>> i = rgb2gray(double(i)/255);>> imshow(i)>> s = i(end:-1:1, :);>> imshow(s);

Examples - Matrix indexing

Page 25: Digital Image Processing Matlab  Basics

25

>> i = imread('sowrds0040.bmp');>> i = rgb2gray(double(i)/255);>> imshow(i)>> s = i(end:-1:1,:);>> imshow(s);

Examples - Matrix indexing

Page 26: Digital Image Processing Matlab  Basics

26

>> i = imread('sowrds0040.bmp');>> i = rgb2gray(double(i)/255);>> imshow(i)>> s = i(end:-1:1,:);>> imshow(s);>> a = i(200:300,200:400);>> imshow(a);

Examples - Matrix indexing

Page 27: Digital Image Processing Matlab  Basics

27

>> i = imread('sowrds0040.bmp');>> i = rgb2gray(double(i)/255);>> imshow(i)>> s = i(end:-1:1,:);>> imshow(s);>> a = i(200:300,200:400);>> imshow(a);

Examples - Matrix indexing

Page 28: Digital Image Processing Matlab  Basics

28

>> i = imread('sowrds0040.bmp');>> i = rgb2gray(double(i)/255);>> imshow(i)>> s = i(end:-1:1,:);>> imshow(s);>> a = i(200:300,200:400);>> imshow(a);>> t = i(1:2:end, 1:2:end);>> imshow(t);

Examples - Matrix indexing

Page 29: Digital Image Processing Matlab  Basics

29

>> i = imread('sowrds0040.bmp');>> i = rgb2gray(double(i)/255);>> imshow(i)>> s = i(end:-1:1,:);>> imshow(s);>> a = i(200:300,200:400);>> imshow(a);>> t = i(1:2:end, 1:2:end);>> imshow(t);

Examples - Matrix indexing

Page 30: Digital Image Processing Matlab  Basics

30

>> i = imread('sowrds0040.bmp');>> i = rgb2gray(double(i)/255);>> imshow(i)>> s = i(end:-1:1,:);>> imshow(s);>> a = i(200:300,200:400);>> imshow(a);>> t = i(1:2:end, 1:2:end);>> imshow(t);>> i(200:300, 200:400) = 0;>> imshow(i);

Examples - Matrix indexing

Page 31: Digital Image Processing Matlab  Basics

31

>> i = imread('sowrds0040.bmp');>> i = rgb2gray(double(i)/255);>> imshow(i)>> s = i(end:-1:1,:);>> imshow(s);>> a = i(200:300,200:400);>> imshow(a);>> t = i(1:2:end, 1:2:end);>> imshow(t);>> i(200:300, 200:400) = 0;>> imshow(i);

Examples - Matrix indexing

Page 32: Digital Image Processing Matlab  Basics

32

>> i = imread('sowrds0040.bmp');>> i = rgb2gray(double(i)/255);>> imshow(i)>> s = i(end:-1:1,:);>> imshow(s);>> a = i(200:300,200:400);>> imshow(a);>> t = i(1:2:end, 1:2:end);>> imshow(t);>> i(200:300, 200:400) = 0;>> imshow(i);>> imshow(i/2);

Examples - Matrix indexing

Page 33: Digital Image Processing Matlab  Basics

33

>> i = imread('sowrds0040.bmp');>> i = rgb2gray(double(i)/255);>> imshow(i)>> s = i(end:-1:1,:);>> imshow(s);>> a = i(200:300,200:400);>> imshow(a);>> t = i(1:2:end, 1:2:end);>> imshow(t);>> i(200:300, 200:400) = 0;>> imshow(i);>> imshow(i/2);

Examples - Matrix indexing

Page 34: Digital Image Processing Matlab  Basics

34

>> i = imread('sowrds0040.bmp');>> i = rgb2gray(double(i)/255);>> imshow(i)>> s = i(end:-1:1,:);>> imshow(s);>> a = i(200:300,200:400);>> imshow(a);>> t = i(1:2:end, 1:2:end);>> imshow(t);>> i(200:300, 200:400) = 0;>> imshow(i);>> imshow(i/2);>> imshow((i>0.8).*i);

Examples - Matrix indexing

Page 35: Digital Image Processing Matlab  Basics

35

>> i = imread('sowrds0040.bmp');>> i = rgb2gray(double(i)/255);>> imshow(i)>> s = i(end:-1:1,:);>> imshow(s);>> a = i(200:300,200:400);>> imshow(a);>> t = i(1:2:end, 1:2:end);>> imshow(t);>> i(200:300, 200:400) = 0;>> imshow(i);>> imshow(i/2);>> imshow((i>0.8).*i);

Examples - Matrix indexing

Page 36: Digital Image Processing Matlab  Basics

36

M-Files can be one of two: Scripts – A series of commands that are performed

on the global scope. No input and output variables.

Functions – A set of commands performed over a given input, with a required output. Functions have a scope of their own. (accessing the global scope can be done by defining the variables to be ‘globals’).

In both types – the m-file must be in the current directory, or in a previously added path (added with the function addpath)

M-Files

Page 37: Digital Image Processing Matlab  Basics

37

Components of m files: Function definition line

function [out1 out2] = name(in1, in2, in3)

H1 line - a single comment line that follows the function definition line. % SQUARESUM compute the sum of the square of the matrix elements This line appears when user writes>> help function_name

>> lookfor keyword - display all functions where the keyword appeared in H1 line

M-Function Programming

Page 38: Digital Image Processing Matlab  Basics

38

Components of m files (cont.):

Help Text - text block following the H1 line without any blank line in between the two

Function body – the Matlab code

Comments – lines starting with % Note: – add short and clear comments to your code!

M-Function Programming

Page 39: Digital Image Processing Matlab  Basics

39

if, else, elseif, end switch, case, otherwise, end return try...catch…end

for i=start:increment:end, end while, end break (used with for or while) continue (used with for or while)

Flow control

Try not to use

Page 40: Digital Image Processing Matlab  Basics

40

1D indexing Convert for / while loops to equivalent vector or matrix operations

>> for x = 1:kff(x) = 5*sin((x-1)/(2*pi));

end

Code Optimization – Vectorizing Loops

>>x = 0:k-1 >>ff = 5*sin(x/(2*pi));

Page 41: Digital Image Processing Matlab  Basics

41

2D indexingmeshgrid – convert rows vectors to arrays C and R that can be used for evaluating function with two variables

>> for r = 1:10>> for c = 1:10>> b(r,c) = r.^2+ c.^2>> end>> end

Code optimization – vectorizing loops

Vectorzing code accelerates the computation significantlyFor Example: using meshgrid runs on the order of 30 times faster the same computation based on loops on Image of 512x512 pixels

[ >>C, R = ](1: , 1: )meshgrid c r >>h = R.^2 + C.^2;

Page 42: Digital Image Processing Matlab  Basics

42

Simple way to improve code execution is to pre-allocate the size of the arrays in the program.

>> f = zeros(1024);

Code Optimization – Pre-allocating large arrays

Page 43: Digital Image Processing Matlab  Basics

43

Cell array is multidimensional array whose elements are copies of other arrays>> c = {‘gauss’,[1 0;0 1], 3}>> c{1}ans =

gauss Structures are similar to cell arrays (allow

grouping of a collection of dissimilar data) but they addressed by fields rather than by numbers>> params.nimgs = 100;>> params.jump = 2;>> params.baseStr = ‘testImg’

Cell arrays and Structures

Page 44: Digital Image Processing Matlab  Basics

44

Matlab arguments are always passed by value

Checking whether an argument exist>> exist(a,’var’)

Checking number of arguments to the functions>> nargin, nargout, nargchk

Getting variable number of arguments>>varargin, varargout

Arguments

Page 45: Digital Image Processing Matlab  Basics

45

>> guide (Graphic User Interface Development Environment) Start the GUI Layout Editor. Guide create◦ fig file: complete description of the gui elements

and their arrangements

◦ gui m-file: the code that controls the gui operations, initializations functions, callback functions

Gui

Page 46: Digital Image Processing Matlab  Basics

Any Questions?