revision on matlab & image processing with matlab

36
Revision o n MATLAB & Image Processing with MATLAB

Upload: hesham-fadl-allah-mohamad

Post on 15-Jan-2016

48 views

Category:

Documents


4 download

DESCRIPTION

Revision on MATLAB & Image Processing With Matlab

TRANSCRIPT

Page 1: Revision on MATLAB & Image Processing With Matlab

Revision on MATLAB & Image Processing

with MATLAB

Page 2: Revision on MATLAB & Image Processing With Matlab

Outline

• Matrices in MATLAB• Basic Operations on Matrices• Variable Name in Matlab• Logical Operators• Flow Control• Scripts and Functions• Visualization and Graphics• Saving your Work• What is the Image Processing ? ........

Page 3: Revision on MATLAB & Image Processing With Matlab

Matrices in MATLAB

• Matrix is the main MATLAB data type

• How to build a matrix?– A=[1 2 3; 4 5 6; 7 8 9];– Creates matrix A of size 3 x 3

• Special matrices:– zeros(n,m), ones(n,m), eye(n,m), rand(), randn()

Page 4: Revision on MATLAB & Image Processing With Matlab

Basic Operations on Matrices

• All operators in MATLAB are defined on matrices: +, -, *, /, ^, sqrt, sin, cos, etc.

• Element-wise operators defined with a preceding dot: .*, ./, .^

• size(A) – size vector

• sum(A) – columns sums vector

• sum(sum(A)) – sum of all the elements

Page 5: Revision on MATLAB & Image Processing With Matlab

Variable Name in Matlab

• Variable naming rules

- must be unique in the first 63 characters

- must begin with a letter

- may not contain blank spaces or other types of punctuation

- may contain any combination of letters, digits, and underscores

- are case-sensitive

- should not use Matlab keyword

• Pre-defined variable names

• pi

Page 6: Revision on MATLAB & Image Processing With Matlab

Logical Operators

• ==, <, >, (not equal) ~=, (not) ~

• find(‘condition’) – Returns indexes of A’s elements that satisfy the condition

Page 7: Revision on MATLAB & Image Processing With Matlab

Logical Operators (cont.)

• Example:

>>A=[7 3 5; 6 2 1], Idx=find(A<4)A=

7 3 5

6 2 1

Idx=3

4

6

Page 8: Revision on MATLAB & Image Processing With Matlab

Flow Control

• MATLAB has five flow control constructs:– if statement– switch statement– for loop– while loop– break statement

Page 9: Revision on MATLAB & Image Processing With Matlab

if

• IF statement condition– The general form of the IF statement is

IF expressionstatements

ELSEIF expressionstatements

ELSEstatements

END

Page 10: Revision on MATLAB & Image Processing With Matlab

switch

• SWITCH – Switch among several cases based on expression

• The general form of SWITCH statement is:SWITCH switch_expr

CASE case_expr,statement, …, statement

CASE {case_expr1, case_expr2, case_expr3, …}statement, …, statement…

OTHERWISEstatement, …, statement

END

Page 11: Revision on MATLAB & Image Processing With Matlab

switch (cont.)

• Note:– Only the statements between the matching CASE and the next CASE, OTHERWISE, or END are executed

– Unlike C, the SWITCH statement does not fall through (so BREAKs are unnecessary)

Page 12: Revision on MATLAB & Image Processing With Matlab

for

• FOR repeats statements a specific number of times

• The general form of a FOR statement is:FOR variable=expr

statements

END

Page 13: Revision on MATLAB & Image Processing With Matlab

while

• WHILE repeats statements an indefinite number of times

• The general form of a WHILE statement is:WHILE expression

statements

END

• CODE

Page 14: Revision on MATLAB & Image Processing With Matlab

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

Page 15: Revision on MATLAB & Image Processing With Matlab

Functions in MATLAB (cont.)

• Example:– A file called STAT.M:

function [mean, stdev]=stat(x)%STAT Interesting statistics.n=length(x);mean=sum(x)/n;stdev=sqrt(sum((x-mean).^2)/n);

– Defines a new function called STAT that calculates the mean and standard deviation of a vector. Function name and file name should be the SAME!

– CODE

Page 16: Revision on MATLAB & Image Processing With Matlab

Visualization and Graphics

• plot(x,y),plot(x,sin(x)) – plot 1D function• figure, figure(k) – open a new figure• hold on, hold off – refreshing• axis([xmin xmax ymin ymax]) – change axes• title(‘figure titile’) – add title to figure• subplot(3,1,2) – locate several plots in figure

- CODE and Debug CODE

Page 17: Revision on MATLAB & Image Processing With Matlab

Saving your Work• save mysession

% creates mysession.mat with all variables • save mysession a b

% save only variables a and b • clear all

% clear all variables • clear a b

% clear variables a and b • load mysession

% load session

Page 18: Revision on MATLAB & Image Processing With Matlab

Image Processing?

Page 19: Revision on MATLAB & Image Processing With Matlab

What is Image Processing?

Image processing is the collective name for techniques used to extract information from digital images or to

manipulate them to render variations of the input image.

Photo stitching Color boost

Vehicle detection and tracking

Page 20: Revision on MATLAB & Image Processing With Matlab

What is Image Processing? Popular technologies which make use of the camera as a

sensor

The Wii Remote uses an IR camera to sense its location relative to the Wii

Sensor Bar.

The Kinect uses image processing techniques on depth images to

detect and track locations of multiple persons in the field of view.

Page 21: Revision on MATLAB & Image Processing With Matlab

Pixels

Pixel

• A pixel (abbr. for picture element) is the smallest unit of an image.

• Therefore, a 640x480 image is a matrix of 640 columns and 480 rows, each element of this matrix is called an image pixel.

Page 22: Revision on MATLAB & Image Processing With Matlab

MATLAB Image Coordinates

• MATLAB stores images as matrices.

• In MATLAB, image pixels are referenced using (row, col)

values.

• Origin of the coordinate system (1,1) is the top left corner of the image

img

Thus, img(4,3) refers to the pixel at the 4th row and 3rd column.

(1,1)

Page 23: Revision on MATLAB & Image Processing With Matlab

RGB and Grayscale• In RGB format, each Pixel has 3 color components: Red,

Green, and Blue.

• Other color representations, e.g. HSV, YUV, CMYK, are also used. Transformations from RGB to these color spaces and back are defined in MATLAB.

• If only intensity (bright/dark) variations are considered, the resultant image is called a grayscale image. Each pixel has only 1 component: intensity.

RGB Gray

Page 24: Revision on MATLAB & Image Processing With Matlab

Examples 1

Blending two images

Page 25: Revision on MATLAB & Image Processing With Matlab

Examples 2

Sobel descriptor to detect object edge

Page 26: Revision on MATLAB & Image Processing With Matlab

Binary Image

Page 27: Revision on MATLAB & Image Processing With Matlab

Greyscale Image

Page 28: Revision on MATLAB & Image Processing With Matlab

Color Image

Page 29: Revision on MATLAB & Image Processing With Matlab

Addition

Image: I Image: I+50

Page 30: Revision on MATLAB & Image Processing With Matlab

Subtraction

Image: I Image: I-80

Page 31: Revision on MATLAB & Image Processing With Matlab

Multiplication

Image: I Image: I*3

Page 32: Revision on MATLAB & Image Processing With Matlab

Division

Image: I Image: I/2

Page 33: Revision on MATLAB & Image Processing With Matlab

Complement

Image: I Image: 255-I

Page 34: Revision on MATLAB & Image Processing With Matlab

Loading and displaying images

>> I=imread('mandrill.bmp','bmp'); % load image

>> image(I) % display image

image filename as a string

image format as a string

Matrix with image data

Page 35: Revision on MATLAB & Image Processing With Matlab

Representation of Images• Images are just an array of numbers

>> I % ctrl+c to halt output!

• Intensity of each 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) =

135

ans(:,:,2) =

97

ans(:,:,3) =

33

Images where the pixel value in the image represents the intensity of the pixel are called intensity images.

Red

Green

Blue

Page 36: Revision on MATLAB & Image Processing With Matlab

Histograms• Frequency of the intensity values of the

image

• Quantise frequency into intervals (called bins)

• (Un-normalised) probability density function of image intensities