Presentation is loading. Please wait.

Presentation is loading. Please wait.

MATLAB Programming Session

Similar presentations


Presentation on theme: "MATLAB Programming Session"— Presentation transcript:

1 MATLAB Programming Session
Program development Planning the program Using Pseudo-code Selecting the right data structures General coding procedures Naming a function uniquely The importance of comments Optimizing for speed Vectorizing your code EE465: Introduction to Digital Image Processing

2 EE465: Introduction to Digital Image Processing
Planning the Program Modular programming Break the problem down into a series of smaller independent tasks Implement each task as a separate function When do I decide to implement a task as a new function? Thumb rule: if this task will be performed more than once (e.g., I might want to use this function in other programs) EE465: Introduction to Digital Image Processing

3 EE465: Introduction to Digital Image Processing
Using Pseudo-code For each pixel x {     - We take a window centered in x and size 2t+1 x 2t+1,  A(x,t).    - We take a window centered in x and size 2f+1 x 2f+1, W(x,f).     wmax=0;     For each pixel y in A(x,t)  &&   y different from x  {         - We compute the difference between W(x,f) and W(y,f),   d(x,y).         - We compute the weight from the distance d(x,y),  w(x,y).                 w(x,y) = exp(- d(x,y) / h);         - If   w(x,y) is bigger than wmax then       wmax = w(x,y);         - We compute the average                average + = w(x,y) * u(y);         - We carry the sum of the weights                 totalweight + = w( x, y); } EE465: Introduction to Digital Image Processing

4 Selecting the Right Data Structure
Usually data structure selection is easy, but sometimes it gets tricky Scenario 1: handle data with varying length (e.g., variable length code) Scenario 2: handle a large amount of data (due to memory constraint) Scenario 3: running speed considerations (e.g., uint8 vs. double) EE465: Introduction to Digital Image Processing

5 General Coding Practices
Use descriptive function and variable names to make your code easier to understand Order subfunctions alphabetically in an M-file to make them easier to find Precede each subfunction with a lock of help text describing what that subfunction does Don’t extend lines of code beyond the 80th column Make sure you have saved your function in a directory recognized by MATLAB path setting EE465: Introduction to Digital Image Processing

6 Naming a Function Uniquely
It appears a simple rule; yet it is not easy to obey (we simply can’t remember them all) >which -all <function name> Doesn’t long function time take more time to type? Yes, but you only need to do it once (you can recall the command using up/down arrow key) TAB key can also help you finish the typing automatically EE465: Introduction to Digital Image Processing

7 The Importance of Comments
I can never emphasize its importance enough, even though I often do a poor job on commenting my own MATLAB programs It makes it easier for both you and others to maintain the program Add comments generously, explain each major section and any smaller segments of code that are not obvious Know how to remove or insert commenting % EE465: Introduction to Digital Image Processing

8 EE465: Introduction to Digital Image Processing
Optimizing for Speed How to vectorize your codes? Avoid using for loop as much as you can Functions Used in Vectorizing (see page 58 of programming_tips.pdf) all: True if all elements of a vector are nonzero. any: True if any element of a vector is a nonzero number or is logical 1 (TRUE). Sum/prod/cumsum (more general than sum) end: END can also serve as the last index in an indexing expression. find (you will find it the most useful) squeeze: remove singleton dimensions. permute,/ipermute, reshape, sort, repmat, shiftdim, ndgrid EE465: Introduction to Digital Image Processing

9 EE465: Introduction to Digital Image Processing
Vectorize Your Code Even if loop is inevitable, try to reduce the number of iterations Example: histogram calculation for a given grayscale image sized 512-by-512 Scheme 1: loop over row and column indexes (512×512= iterations) Scheme 2: loop over intensity values (256 iterations) Scheme 2 is better! EE465: Introduction to Digital Image Processing

10 MATLAB Programming Tip #1
Use functions provided by MATLAB Although we can implement any function from the scratch, the ones offered by MATLAB are optimized in terms of efficiency and robustness. Examples: use bwdist in our implementation of finding skeleton EE465: Introduction to Digital Image Processing

11 MATLAB Programming Tip #2
Use as few loops as possible Example: implementation of histogram calculation Scheme 1: loop over every position in the image, i.e., i=1-M, j=1-N Scheme 2: loop over every intensity value, i.e., 0-255 EE465: Introduction to Digital Image Processing

12 MATLAB Programming Tip #3
Parallelism is preferred by MATLAB Example: in finding skeleton, we can find the local maximum in a parallel fashion, which is much more efficient than looping over every pixel % find the local maximum n=[0 1;-1 0;0 -1;1 0]; sk=dt>0; for i=1:4 sk=sk&(dt>=circshift(dt,n(i,:))); end EE465: Introduction to Digital Image Processing


Download ppt "MATLAB Programming Session"

Similar presentations


Ads by Google