Lecture 7 Introduction To M Function Programming Examples
Lecture 7 Introduction To M Function Programming Examples
Qadri Hamarsheh
Example 7.1:
Write a Matlab function that computes the average intensity of an
image.
The program should produce an error if the input is not a one-or two-
dimensional array.
Hints:
1) 2-D array f can be converted to a column vector v, by letting
v= f ( : )
(The function will be able to work with both vector and image
inputs).
2) To return the size of the longest dimension of an array, we use
length (A).
3) Another way to obtain the number of elements in an array directly
is to use function numel, with syntax:
n= numel (A)
1
Dr. Qadri Hamarsheh
Example 7.2:
Write an M-function (based on for loop) to extract a rectangular sub
image from an image.
Solution:
The inputs to the function:
Original image
The size (number of rows and columns) of the sub image
Coordinates of the top left comer of the extracted image.
Keep in mind
The image origin in Matlab is at (1,1)
2
Dr. Qadri Hamarsheh
Example 7.4: individual pixel processing:
The intensity of the red color channel of rgb image.jpg is divided by 2.
(Low-level processing)
Example 7.5
The intensity of the red color channel of rgb image.jbg is divided by 2
using high-level processing (array notation--- vectorization)
3
Dr. Qadri Hamarsheh
Example 7.6
Vectorization of program for extracting sub regions (example 7.2)
Example 7.7
Convert the rgb color image football.jpg to a greyscale image:
(Using the method of computing the mean of the three color channels
and then store it in file grayfootball.jpg ;
Using low-level processing (for loop).
Using high-level processing (vectorization).
Home work1:
Write an M-function for grayscale image blurring Method (compute the
mean of a 3*3 pixel environment and by setting the resulting center
pixel to this value (mean)). Using the vectorization and for loop
methods. The function also compares the computational speed time
between two methods (use tic and toc Matlab function).
4
Dr. Qadri Hamarsheh
Interactive I/O
To write interactive M-functions that display information and instructions to users
and accept inputs from the keyboard
function disp is used to display information on the screen.
Syntax:
disp (argument).
Argument may be:
a) Array :
>> A = [1 2 ; 3 4];
>> disp (A);
b) variable contains string :
>> sc = 'Digital Image processing' ;
>> disp (sc)
c) string:
>> disp ('This is another way to display text')
function Input: is used for inputting data into an M-function.
Syntax:
I. Form 1
t = input ('message')
• This function outputs the words contained in message and waits for an input from the
user, followed by a return, and stores the input in t.
• The input can be
a) Single number.
b) Character string (single quotes)
c) Vector (enclosed by square brackets)
d) matrix (enclosed by square brackets) and rows separated by semicolons).
II. Form 2
t = input ('message','s')
• Outputs the contents of message and accepts a character string whose elements can be
separated by commas.