Digital Image Processing Lecture 6: Introduction to M- function Programming.

Slides:



Advertisements
Similar presentations
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
Advertisements

Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Computer Science in Practice This course is an introduction to problems (and solutions) that arise in applied fields of computer science such as machine.
Computer Science in Practice This course is an introduction to problems (and solutions) that arise in applied fields of computer science such as machine.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Introduction to C Programming
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
The switch Statement, DecimalFormat, and Introduction to Looping
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 6.
How to think through your program [ principles of good program design ] Rachel Denison MATLAB for Cognitive Neuroscience ICN, 13 December 2007.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Matlab Programming, part 1 M-files It is generally more convenient to program in Matlab using m-files, ascii text files containing a set of Matlab commands.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Input, Output, and Processing
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
Digital Image Processing Lecture10: MATLAB Example – Utility M-functions for Intensity Transformations.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 05 (Part III) Control Statements: Part II.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
CSE123 Lecture 3 Files and File ManagementScripts.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Digital Image Processing Introduction to M-function Programming.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Perl Chapter 3 Conditional statements. Control Expressions Control expressions – interpreted as T/F (evaluated as strings or numbers) – simple, relational,
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
1 Structured Programming EEN170 Programming in MATLAB.
CSE123 - Lecture 4 Structured Programming- Loops.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Chapter 4 – C Program Control
Chapter 2 - Introduction to C Programming
Matlab Training Session 4: Control, Flow and Functions
JavaScript: Control Statements I
Scripts & Functions Scripts and functions are contained in .m-files
Chapter 2 - Introduction to C Programming
Introduction to MATLAB
Introduction to C++ Programming
3 Control Statements:.
Chapter 4 - Program Control
Matlab Basics.
Introduction to C Programming
Presentation transcript:

Digital Image Processing Lecture 6: Introduction to M- function Programming

M-Files M-Files in MATLAB, can be: –Scripts that simply execute a series of MATLAB statements, or –Functions that can accept arguments and can produce one or more outputs. M-Files are created using a text editor and are stored with a name of the form filename.m.

M-Files The components of a function M-file are: –The function definition line –The H1 line –Help text –The function body –Comments

M-Files The function definition line –It has the form: function [outputs] = name (inputs) For example, a function that computes the sum and the product of two images, has the following definition: function [s, p] = sumprod (f, g) Where f and g are the input images.

M-Files The function definition line –Notes: The output arguments are enclosed by brackets and the input by parentheses. If the function has a single output argument, it is acceptable to list the argument without brackets. If the function has no output, only the word function is used, without brackets or equal sign function sum(f,g) Function names must begin with a letter, and followed by any combination of letters, numbers or underscores. No spaces are allowed

M-Files The function definition line –Notes: Functions can be called at the command prompt, for example: >> [s, p] = sumprod (f, g) >> y = sum (x)

M-Files The H1 line –Is the first text line. –It is a single comment line that follows the function definition line. –There can be no blank lines or leading spaces between the H1 line and the function definition line –Ex: % SUMPROD computes the sum and product of two images –H1 line is the first text that appears when a user types: >> help function_name

M-Files Help Text –Is a text block that follows the H1 line, without any blank lines in between the two. –Help text is used to provide comments and online help for the function. –When a user types help function_name at the prompt, MATLAB displays all comment lines that appear between the function definition line and the first noncomment line (executable or blank). –The help system ignores any comment lines that appear after the Help text block.

M-Files The function body –Contains all the MATLAB code that performs computations and assigns values to output arguments. Comments –All lines preceded by the symbol “%” that are not the H1 line or help text are considered function comment lines and are not considered part of the Help text block.

Operators MATLAB operators are grouped into three main categories: –Arithmetic operators that perform numeric computations –Relational operators that compare operands quantitatively –Logical operators that perform the functions AND, OR and NOT.

Arithmetic Operations MATLAB has two different types of arithmetic operations: –Matrix Arithmetic Operations: are defined by the rules of linear algebra. –Array Arithmetic Operations: are carried out element by element and can be used with multidimensional arrays –The period (.) character, distinguishes array operations from matrix operations.

Arithmetic Operations For example, A*B indicates matrix multiplication in the traditional sense, whereas A.*B indicates array multiplication, in the sense that the result is an array, with the same size as A and B, in which each element is the product of corresponding elements of A and B. i.e. if C = A.*B, then C(I,J) = A(I,J) * B(I,J) Since matrix and array operations are the same in addition and subtraction, no (.+), (.-) operators exist

Arithmetic Operations Important note: –When writing an expression such as A = B, MATLAB makes a “note” that B is equal to A, but doesn’t actually copy the data into B unless the contents of A change later in the program. –i.e. MATLAB does not duplicate information unless it is absolutely necessary.

Array and Matrix Arithmetic Operations

The Image Arithmetic Functions Supported by IPT

MAX and MIN Ex1: >> A = [ ] >> max(A) ans = 4 Ex2: >> A = [1 2 3; 4 5 6] >> max(A) ans = 4 5 6

MAX and MIN Ex3: >> A = [1 2 3] >> B = [4 5 6] >> max(A,B) ans = Ex4: >> A = [1 2 3; 4 5 6] >> B = [7 8 9; 1 2 3] >> max(A,B) ans =

Logical Operators and Functions Operators: –&: logical AND –| : logical OR –~: logical NOT Functions: –And (a,b) –or (a,b) –not (a)

Logical Operators and Functions Ex1: >> A = logical ([ ]) >> B = logical ([ ]) >> A & B ans = 1 0

Logical Operators and Functions Ex2: >> A = logical ([ ]) >> B = logical ([ ]) >> A | B ans = 1 1

Logical Operators and Functions Ex3: >> A = logical ([ ]) >> ~ A ans = 0 1

Logical Operators and Functions Ex4: >> A = logical ([ ]) >> B = logical ([ ]) >> xor(A,B) ans = 0 1

Logical Operators and Functions Ex5: >> A = logical ([ ]) >> B = logical([ ]) >> all(A) ans = 0 >> all(B) ans= 1

Logical Operators and Functions Ex6: >> A = logical ([ ]) >> B = logical([ ]) >> any(A) ans = 1 >> any(B) ans= 0

Logical Operators and Functions Ex7: >> A = logical ([1 0 1; 1 1 1]) >> B = logical([0 0 0; 0 1 0]) >> all(A) ans = >> all(B) ans= 0 0 0

Logical Operators and Functions Ex8: >> A = logical ([1 0 1; 1 1 1]) >> B = logical([0 0 0; 0 1 0]) >> any(A) ans = >> any(B) ans= 0 1 0

Flow Control

Flow Control if, else and elseif Conditional statement if has the syntax: if expression statements end General syntax: If expression1 statements1 elseif expression2 statements2 else statements3 end

Flow Control if, else and elseif Ex: function av = Average (f) if (ndims(f)>2) error('the dimensions must be greeter than 2'); end av = sum(f(:))/length(f(:)); Notes: Error: returns the error enclosed in “”, and stops the program. Length: returns no of elements in a matrix

Flow Control for Syntax: for index = start:increment:end statements End Nested for: for index1 = start1:increment1:end statements1 for index2 = start2:increment2:end statements2 end additional loop1 statements end

Flow Control for Ex: count = 0; for k=0:0.1:1 count = count + 1; end Notes: 1.If increment was omitted it is taken to be 1. 2.The increment can be negative value, in this case start should be greater than end.

Flow Control while Syntax: while expression statements end Nested while: while expression1 statements1 while expression2 statements2 end additional loop1 statements end

Flow Control while Ex: a = 10; b = 5; while a a = a – 1; while b b = b - 1; end Note: MATLAB treatment for a numeric value in a logical context: nonzero value  True zero value  False

Flow Control break and continue break –Using break terminates the execution of a for or while loop. Continue –The continue statement passes control to the next iteration of the for or while loop in which it appears, skipping any remaining statements in the body of the loop.

Flow Control switch The syntax: switch switch_expression casecase_expression statements1 case{case_expression1, case_expression_2,…} statements2 otherwise statements3 end

Flow Control switch Ex switch newclass case ‘uint8’ g = uint8(f); case ‘uint16’ g = uint16(f); case ‘double’ g = double(f); otherwise error(‘unknown’); end