2.7 Matlab functions and simulation

This section will contain collection of functions and simulation I made during work on this project.

  1. M file to generate a disk of some radius and center. added June 9, 2008. This function returns a 2D matrix of a disk (white=1,black=0) nma_makeDisk.m

    %**************************************************** 
    %       Copyright (C) 2008 Nasser Abbasi 
    %  Free to use and modify for academic and research only 
    %*************************************************** 
    function I=nma_makeDisk(N,r,cx,cy,blackGrayLevel,whiteGrayLevel) 
    %function nma_makeDisk(N,r,cx,cy) 
    % 
    %function to make disk image 
    %r      %radius in pixels of disk 
    %N      %size of overall image in pixels, say 256 
    %cx,cy  %location of x,y of disk center relative to center of image 
    %       %so, to have the disk center in the center of the image use (0,0) 
    % 
    % by Nasser Abbasi June 9,2008 
     
    if nargin ~=6 
        error('wrong number of arguments'); 
    end 
     
    r = round(r); 
     
    [errmsg,success]=verifyParamters(N,r,cx,cy); 
    if ~success 
        error(errmsg); 
    end 
     
    I=zeros(N); 
    I(:)=blackGrayLevel; 
     
    row=(-1)*cy+ceil(N/2); col=cx+ceil(N/2);  %adjust to center it in the image 
    for y=1:N 
        for x=1:N 
            d = sqrt((x-col)^2+(y-row)^2); 
            if d<=r 
                I(y,x)=whiteGrayLevel; 
            end 
        end 
    end 
     
    end 
    %%%%%%%%%%%%%%%%%%% 
    % Called from above function to validate input 
    % 
    %%%%%%%%%%%%%%%%%%% 
    function [errmsg,success]=verifyParamters(N,r,cx,cy) 
    errmsg=''; 
    success=0; 
     
    if ~isnumeric(N) || ~isnumeric(r)||~isnumeric(cx)||~isnumeric(cy) 
        errmsg='invalid argument type. Numerical values expected'; 
        return; 
    end 
     
    if ~(ceil(N) == floor(N)) 
        errmsg='N, size of image must be integer'; 
        return; 
    end 
     
    if ~(ceil(cx) == floor(cx)) 
        errmsg='cx, x-coordinates of disk must be integer'; 
        return; 
    end 
     
    if ~(ceil(cy) == floor(cy)) 
        errmsg='cy, y-coordinates of disk must be integer'; 
        return; 
    end 
     
    if abs(cx)>floor(N/2) || abs(cy)>floor(N/2) 
        errmsg='coordinates of center of disk outside image'; 
        return; 
    end 
     
    if r>floor(N/2) 
        errmsg='radius is too large, must be less than half the image size'; 
        return; 
    end 
     
    success=1; 
    end      
    

  2. This file is a driver for the above function. Shows examples of how to call the function nma_driver_makeDisk.m

    %**************************************************** 
    %       Copyright (C) 2008 Nasser Abbasi 
    %  Free to use and modify for academic and research only 
    %*************************************************** 
     
    %script to test the function nma_makeDisk(N,r,cx,cy) 
    %a little simulation of a disk moving in 4 different configurations 
    %Nasser Abbasi 
     
    close all; clear all; 
     
    radius =25; 
    N=256; 
    A=nma_makeDisk(N,radius,0,0,0,255); 
     
    radius=25;   %in pixels radius of disk 
    N=256;       %size of overall image in pixels 
     
    cx1=-N/2:N/2-1; cy1=-N/2:N/2-1; %move it along the diagonal 
    cx2=20;         cy2=-N/2:N/2-1; %move it up and down, off center 
     
    angle=linspace(0,2*pi,N); %move it in a circle around center 
    rho=ones(1,N)*radius*3; 
    [cx3,cy3] = pol2cart(angle,rho); 
    cx3=round(cx3); 
    cy3=round(cy3); 
     
    %move it in a circle off center 
    cx4=10+cx3;  cy4=10+cy3; 
     
    h=figure(1); 
    set(h,'DoubleBuffer','on'); 
    colormap(gray); 
     
    figure; 
    N=256; 
    radius=10; 
    CX=20;         CY=-N/2:N/2-1; %move it up and down, off center 
    CX2=-20; 
    for i=1:N 
        A=nma_makeDisk(N,radius,CX,CY(i),0,255); 
        B=nma_makeDisk(N,radius,CX2,CY(i),0,255); 
        imshow(A+B,[]); 
        drawnow; 
    end 
     
    for i=1:N 
        subplot(2,2,1); 
        imshow(nma_makeDisk(N,radius,cx1(i),cy1(i)),[]); 
        line([1,N],[N/2,N/2],'Color','white'); 
        line([N/2,N/2],[1,N],'Color','white'); 
        line([1,N],[N,1],'Color','white','LineStyle',':'); 
        title('along the diagonal'); 
     
        subplot(2,2,2); 
        imshow(nma_makeDisk(N,radius,cx2,cy2(i)),[]); 
        line([1,N],[N/2,N/2],'Color','white'); 
        line([N/2,N/2],[1,N],'Color','white'); 
        line([cx2+N/2,cx2+N/2],[N,1],'Color','white','LineStyle',':'); 
        title('vertical off center'); 
     
        subplot(2,2,3); 
        imshow(nma_makeDisk(N,radius,cx3(i),cy3(i)),[]); 
        line([1,N],[N/2,N/2],'Color','white'); 
        line([N/2,N/2],[1,N],'Color','white'); 
        %plot(cx3,cy3,'Color','white','LineStyle',':'); 
        title('circle centered at center'); 
     
        subplot(2,2,4); 
        imshow(nma_makeDisk(N,radius,cx4(i),cy4(i)),[]); 
        line([1,N],[N/2,N/2],'Color','white'); 
        line([N/2,N/2],[1,N],'Color','white'); 
        %plot(cx4,cy4,'Color','white','LineStyle',':'); 
        title('circle centered off center'); 
     
        drawnow; 
    end