% ==================================================================================================
% Module:   median.m
%
% Usage:    Median filter an image
%
% Purpose: 
%     Performs the median filter on an image of a specified window size.
%
%     
% Input Variables:
%     fxy     input image (binary data)
%     A      median window
%     
% Returned Results:
%     The image, median filtered with a window of size A.
%     
% Restrictions/Notes:
%     This function is geared for binary images. 
%
% See Also:
%   med()
% 
% References:
% 
% Author:       Justin Ford
% Date:         2/1/04
% Revisions:    none
% ==================================================================================================


function result = med(fxy, A)

result=zeros(size(fxy,1),size(fxy,2));

A_row_dim = size(A,1);
A_col_dim = size(A,2);

if (A_row_dim > 1)
    row_diff = (A_row_dim - 1)/2;
else
    row_diff = 0;
end

if (A_col_dim > 1)
    col_diff = (A_col_dim - 1)/2;
else
    col_diff = 0;
end

for i=1:1:size(fxy,1)
    for j=1:1:size(fxy,2)
        if ( (i > row_diff) & (i < ( size(fxy,1) - row_diff )) & (j > col_diff) & (j < ( size(fxy,1) - col_diff )) )         
            result(i,j) = med( fxy((i-row_diff):(i+row_diff),(j-col_diff):(j+col_diff)) );
        end
    end
end