Digital Image Processing Lecture10: MATLAB Example – Utility M-functions for Intensity Transformations.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Matlab Intro Simple introduction to some basic Matlab syntax. Declaration of a variable [ ] Matrices or vectors Some special (useful) syntax. Control statements.
A MATLAB function is a special type of M-file that runs in its own independent workspace. It receives input data through an input argument list, and returns.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 63 – Manipulating Data Using Methods – Day 2.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Chapter 10.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
MATLAB Cell Arrays Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University.
Chapter 8 Arrays and Strings
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Digital Image Processing, 3rd ed. © 1992–2008 R. C. Gonzalez & R. E. Woods Gonzalez & Woods Chapter 3 Intensity Transformations.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins Digital Image Processing Using MATLAB ® Chapter 3 Intensity Transformations.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Fall 2006AE6382 Design Computing1 OOP: Creating a Class More OOP concepts An example that creates a ASSET class and shows how it might be used Extend the.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 8 Arrays and Strings
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Using Client-Side Scripts to Enhance Web Applications 1.
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Flow of Control Part 1: Selection
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Digital Image Processing Lecture9: Intensity (Gray-level) Transformation Functions using MATLAB.
COMP 116: Introduction to Scientific Programming Lecture 11: Functions.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Digital Image Processing Lecture4: Fundamentals. Digital Image Representation An image can be defined as a two- dimensional function, f(x,y), where x.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
ME 142 Engineering Computation I Using Subroutines Effectively.
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.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
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.
MATLAB for Engineers, by Holly Moore. ISBN © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is.
Digital Image Processing Lecture 12: Image Topology (Suppl)
ME 142 Engineering Computation I Using Subroutines Effectively.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Digital Image Processing Introduction to MATLAB. Background on MATLAB (Definition) MATLAB is a high-performance language for technical computing. The.
CMPS 1371 Introduction to Computing for Engineers CHARACTER STRINGS.
Fall 2006AE6382 Design Computing1 OOP Programming in Matlab Review our ASSET class and study how it might be used as a parent for other classes Extend.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Basics of MATLAB 2- Programming in MATLAB By DR. Wafaa Shabana
OOP: Creating a Class Topics More OOP concepts
Matlab Training Session 4: Control, Flow and Functions
Scripts & Functions Scripts and functions are contained in .m-files
Chapter 14: Exception Handling
User Defined Functions
Functions In Matlab.
CS 1111 Introduction to Programming Fall 2018
7 Arrays.
EECE.2160 ECE Application Programming
The spatial domain processes discussed in this chapter are denoted by the expression
Presentation transcript:

Digital Image Processing Lecture10: MATLAB Example – Utility M-functions for Intensity Transformations

Handling a variable number of inputs and/or outputs To check the number of arguments into an M-function we use function nargin. n = nargin which returns the actual number of arguments input into the M-function. Similarly, function nargout is used in connection with the outputs of an M- function. The syntax is: n = nargout

Handling a variable number of inputs and/or outputs For example, suppose that we execute the following M-function at the prompt: >> T = testhv(4,5); Use of nargin within the body of this function would return a 2, while use of nargout would return 1.

Handling a variable number of inputs and/or outputs Function nargchk can be used in the body of an M-function to check if the correct number of arguments were passed. The syntax is: msg = nargchk (low, high, number) This function returns the message “Not enough input parameters” if number is less than low, or “Two many input parameters” if number is greater than high. If number is between low and high, nargchk returns an empty matrix.

Handling a variable number of inputs and/or outputs A frequent use of function nargchk is to stop execution via the error function if the incorrect number of arguments is input. The number of actual arguments is determined by the nargin function. For example, consider the following code fragment: function G = test (x, y, z) …. error (nargchk (2,3, nargin) … now, when typing: >> test (6); Which has only one input argument would produce the error. “Not enough input arguments” and execution would terminate.

Handling a variable number of inputs and/or outputs Often it is useful to be able to write functions in which the number of input and/or output arguments is variable. For this we use the variables varargin and varargout. For example: function [m, n] = test (varargin) accepts a variable number of inputs into function test, and function [varargout] = test (m, n, p) returns a variable number of outputs from function test. If function test had, say, one fixed input argument, x, followed by a variable number of input arguments, then function [m, n] = test (x, varargin) would cause varargin to start with the second input argument supplied by the user when the function is called.

Handling a variable number of inputs and/or outputs It is acceptable to have a function in which both the number of input and output arguments is variable. When varargin is used as the input argument of a function, MATLAB sets it to a cell array (def. A cell array is a datatype with index data containers called cells. Each cell can contain any type of data. Cell array commonly contain lists of text strings, combination of text and numbers or numeric arrays of different sizes) that accepts a variable number of inputs by the user. Because varargin is a cell array, an important aspect of this arrangement is that the call to the function can contain a mixed set of inputs. For example, we can call test function as follows: >> [m, n] = test (f, [ ], A, ‘label’);

Handling a variable number of inputs and/or outputs where f is an image, the next argument is a row vector of length 3, A is a matrix, and ‘label’ is a character string. This is indeed a powerful feature that can be used to simplify the structure of functions requiring a variety of different inputs. Similar comments apply to varargout.

An M-function for intensity transformation In this section, we develop a function that computes the following transformation functions: negative, log, gamma and contrast stretching. In writing this function we use function changeclass, which has the syntax g = changeclass (newclass, f) This function converts image f to the class specified in parameter newclass and outputs it as g. Valid values for newclass are ‘uint8’, ‘uin16’ and ‘double’

An M-function for intensity transformation function image = changeclass (class, varargin) %CHANGECLASS changes the storage class of an image % I2 = CHANGECLASS(CLASS, I); % RGB2 = CHANGECLASS(CLASS, RGB); % BW2 = CHANGECLASS(CLASS, BW); % X2 = CHANGECLASS(CLASS, X, 'indexed'); % Copyright The MathWorks, Inc. Used with permission. switch class case 'uint8' image = im2uint8 (varargin{:}); case 'uint16' image = im2uint16 (varargin {:}); case 'double' image = im2double(varargin {:}); otherwise error ('Unsupported IPT data class'); end

An M-function for intensity transformation Note in the following M-function, which we call intrans, how function operations are formatted in the Help section of the code, how a variable number of inputs is handled, how error checking is interleaved in the code, and how the class of the output image is matched to the class of the input. Keep in mind when studying the following code that varargin is a cell array, so its elements are selected by using curly brackets {}.

An M-function for intensity transformation function g = intrans (f, varargin) %INTRANS performs intensity (gray-level) transformations. % G = INTRANS (F, 'neg') computes the negative of input image F. % % G = INTRANS(F, 'log', C, CLASS) computes C*log(1+F) and multiplies % the result by (positive) constant C. If the last two parameters are % omitted, C defaults to 1. Parameter CLASS offers the option to specify % the class of the output as 'uint8' or 'uint16'. if parameter CLASS is % omitted, the output is of the same class as the input. % % G = INTRANS(F, 'gamma', GAM) performs a gamma transformation on the % input image using parameter GAM (a required input).

An M-function for intensity transformation % % G = INTRANS(F, 'stretch', M, E) computes a contrast-stretching % transformation using the expression 1./(1+ (M./(F + eps)).^E). % Parameter M must be in the range [0 1]. the default value for M is % mean2 (im2double(F)), and the default value for E is 4 % % For the 'neg', 'gamma' and 'stretch' transformations, double input % images whose maximum value is greater than 1 are scaled first using % MAT2GRAY. Other images are converted to double first using IM2DOUBLE. % for the 'log' transformation, double images are transfortmed without % being scaled; % % the output is of the same class as the input, except if a different % class is specified for the 'log' option.

An M-function for intensity transformation % Verify the correct number of inputs error (nargchk (2, 4, nargin) % Store the class of the input for use later. classin = class (f); % If the input is of class double, and it is outside the range % [0 1], and the specified transformation is not 'log', convert the % input to the range [0 1]. if strcmp(class(f), 'double') & max(f(:)) > 1 &... - strcmp(varargin{1}, 'log') f = mat2gray(f); else % Convert to double, regardless of class (f) f = im2double(f); end

An M-function for intensity transformation % Determine the type of transformation specified. method = varargin {1}; % Perform the intensity transformation specified. switch method case 'neg' g = imcompelment (f);

An M-function for intensity transformation case 'log' if length(varargin) == 1 c = 1; elseif length(varargin) == 2 c = varargin{2}; elseif length(varargin) == 3 c = varargin {2}; classin = varargin{3}; else error ('Incorrect number of inputs for the log option') end g = c*(log(1 + double(f)));

An M-function for intensity transformation case 'gamma' if length(varargin) < 2 error ('Not enough inputs for the gamma option') end gam = varargin {2}; g = imadjust (f, [ ], [ ], gam);

An M-function for intensity transformation case 'stretch' if length(varargin) == 1 % Use defaults. m = mean2(f); E = 4.0; elseif length(varargin) == 3 m = varargin {2}; E = varargin {3}; else error ('Incorrect number of inputs for the stretch option.') end g = 1./(1 + (m./(f + eps)).^E); otherwise error ('Unknown enhancement method') end

An M-function for intensity transformation % Convert to the class of the input image. g = changeclass (classin, g);