An Introduction To Programming in Matlab
An Introduction To Programming in Matlab
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
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
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); }
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 Dont extend lines of code beyond the 80th column Make sure you have saved your function in a directory recognized by MATLAB path setting
(we simply cant remember them all) >which -all <function name> Doesnt 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
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 %
of iterations Example: histogram calculation for a given grayscale image sized 512-by-512
Scheme 1: loop over row and column indexes (512 512=262144 iterations) Scheme 2: loop over intensity values 0-255 (256 iterations) Scheme 2 is better!
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
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