19
Programming and Image Processing with Matlab m-files Input/Output control control flow relational and logical operators Image Processing Basics Contents:

Programming and Image Processing with Matlab

  • Upload
    coen

  • View
    54

  • Download
    2

Embed Size (px)

DESCRIPTION

Programming and Image Processing with Matlab. m-files Input/Output control control flow relational and logical operators Image Processing Basics. Contents:. M-files. Files that contain a Matlab code are called the m-files . - PowerPoint PPT Presentation

Citation preview

Page 1: Programming and Image Processing  with Matlab

Programming and Image Processing with Matlab

m-files Input/Output control control flow relational and logical operators Image Processing Basics

Contents:

Page 2: Programming and Image Processing  with Matlab

M-files

- Files that contain a Matlab code are called the m-files.- The m-files may take input arguments and/or return output

arguments.

Creating a new m-file

Save the m-file as,e.g. function_name.m

Page 3: Programming and Image Processing  with Matlab

Input/Output

- To read in a value, we can use the ‘input’ functiona=input('Please enter an integer: '); Please enter an integer: 5

- To display as output a number or a stringdisp(‘The value of the variable b is ’), disp(b)

The value of the variable b is 5

Page 4: Programming and Image Processing  with Matlab

Passing function arguments/results

- Here is an example of a function file

function [c] = add(a,b)

c=a+b;

disp('The sum of a and b is'),disp(c)

Output argument

Input arguments

To call this function:

>>f=add(5,8)

Page 5: Programming and Image Processing  with Matlab

Control flow

To control the flow of commands, MATLAB supplies four

methods

the for loops the while loops the if-else-end constructions the switch-case constructions

Page 6: Programming and Image Processing  with Matlab

For Loops

for k = array

commands

end

The commands between the for and end statements are executed for all values stored in the array.

Example:sum = 0; %set sum to 0for n = 1:100 %set n to 1, increment by 1 (default) until 100 is reached disp(n) %display n sum = sum + n %add n to the sumend

Page 7: Programming and Image Processing  with Matlab

While Loops

while condition

statement(s)

end

The commands between the while and end statements are executed until the condition is satisfied.

Example:q = 3;while q > 0.01 %Keeps divinding q by 2 as long as q

q = q/2; %is greater than 0.01end

q = 0.005859375

While loop is used when the programmer does not know the number of repetitions a priori.

Page 8: Programming and Image Processing  with Matlab

If-else statement

if expressioncommands (evaluated if expression is true)elsecommands (evaluated if expression is false)end

Example:a = input('Please input an integer :'); if mod(a,2)==0%if modulus after division by 2 is 0, the number is even disp('You entered an even number')else disp('You entered an odd number')end

Page 9: Programming and Image Processing  with Matlab

Relational Operators

Comparisons in MATLAB are performed with the aid of the following operators

Operator == compares two variables and returns ones when they are equal and zeros otherwise.

Page 10: Programming and Image Processing  with Matlab

Logical Operators

There are three logical operators available in MATLAB

Suppose we want to select all entries x that satisfy the inequalities x >= 1 or x < -0.2

>>x = randn(1,7)x = -0.4326 -1.6656 0.1253 0.2877 -1.1465 1.1909 1.1892

%index of true(1) or false(0) is shown as solution>>ind = (x >= 1) | (x < -0.2)

ind = 1 1 0 0 1 1 1

>>y = x(ind)y = -0.4326 -1.6656 -1.1465 1.1909 1.1892

| Or

& And

~ Not

Page 11: Programming and Image Processing  with Matlab

Digital Image Processing

A set of algorithms for manipulating digital images A preprocessing step necessary for image understanding Techniques to transform an image into a meaningful signal

DIP is an important component of many fields

– Remote sensing/Photogrammetry

– Medical Imaging

– Robotics

– Multimedia

– Visualization

Page 12: Programming and Image Processing  with Matlab

Matlab: Review

The trick behind Matlab is that everything is represented in the form of arrays or matrices.

Mathematical Operations starting from simple algebra to complex calculus may be conveniently carried out using this environment.

The main use of Matlab in Image Processing is for algorithm implementations and development.

Code developed in Matlab can be converted into C, C++ or Visual C++.

Page 13: Programming and Image Processing  with Matlab

Three types of images:

Gray-scale images 8 bit image

I(x,y) [0..255]

Binary images 0,1 binary

I(x,y) {0 , 1}

Color images 3 different grey scale images RGB

IR(x,y) IG(x,y) IB(x,y)

Page 14: Programming and Image Processing  with Matlab

210 209 204 202 197 247 143 71 64 80 84 54 54 57 58 206 196 203 197 195 210 207 56 63 58 53 53 61 62 51 201 207 192 201 198 213 156 69 65 57 55 52 53 60 50 216 206 211 193 202 207 208 57 69 60 55 77 49 62 61 221 206 211 194 196 197 220 56 63 60 55 46 97 58 106 209 214 224 199 194 193 204 173 64 60 59 51 62 56 48 204 212 213 208 191 190 191 214 60 62 66 76 51 49 55 214 215 215 207 208 180 172 188 69 72 55 49 56 52 56 209 205 214 205 204 196 187 196 86 62 66 87 57 60 48 208 209 205 203 202 186 174 185 149 71 63 55 55 45 56 207 210 211 199 217 194 183 177 209 90 62 64 52 93 52 208 205 209 209 197 194 183 187 187 239 58 68 61 51 56 204 206 203 209 195 203 188 185 183 221 75 61 58 60 60 200 203 199 236 188 197 183 190 183 196 122 63 58 64 66 205 210 202 203 199 197 196 181 173 186 105 62 57 64 63

x = 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

414243444546474849505152535455

Grayscale Image

y =

Page 15: Programming and Image Processing  with Matlab

Color Image

Page 16: Programming and Image Processing  with Matlab

Image Representation

• An image I is a matrix of pixel values• Pixel value at p = [x,y]T is I(p) or I(x,y)• Origin is top left corner

– I is a matrix– p is a vector– x, y, I(p), I(x,y) are scalars

Image may be displayed with imshow command.

Page 17: Programming and Image Processing  with Matlab

Image Histogram 

Image histogram (see above) is a chart that shows the distribution of intensities in an indexed or intensity image, i.e. the x-axis contains the grey values and the y-axis the count how many pixels in the image have this grey value

Source Image Histogram of Image

>>imhist(image)

Page 18: Programming and Image Processing  with Matlab

Image I/O

You can open an image as a matrix using imread command.– The matrix may simply be m x n form or it may be 3

dimensional array or it may be an indexed matrix, depending upon image type.

Changed image may then be saved with imwrite command.

Page 19: Programming and Image Processing  with Matlab

Basic IP funtions

 

%reading an imageic = imread('image_rgb.jpg');%converting colour image to greyscale imageimggr=rgb2gray(ic);

%Resizing an imageimgsmall = imresize(igr, 0.5, 'bicubic');

%Cropping an image interactivelyimgcropped = imcrop(igr);

%convert to black-and-white (binary) imageimgbw_auto=im2bw(imgcropped, graythresh(imgcropped));

%applying a predefined image filter

sharp = fspecial('unsharp');

imgsharp =imfilter(igr,sharp,'replicate');

figure('name','Sharpened image'); imshow(imgsharp)