% ==================================================================================================
% Module:   snnm.m
%
% Usage:    Performes a 7x7 symmetric nearest-neighbor mean 9snnm) on a grayscale
%           image.
%
% Purpose: 
%     Perform grayscale snnm
%
%     
% Input Variables:
%     I image
%     
% Returned Results:
%     The image with filter applied to it.
%  
% Restrictions/Notes:
%     None
%     
% See Also:
%
% References:
%
% 
% Author:       Isaac Gerg
% Date:         3/25/04
% Revisions:    none
% ==================================================================================================

function O = snnm(I)
I = double(I);
M = ones(7,7);
O = zeros(size(I));

v = zeros(1,25);

for x = 1:size(I,1)
    for y = 1:size(I,2)
        if x<4 || y<4 || x>size(I,1)-4 || y>size(I,2)-4
            continue;
        end
        i = 1;
        for m = 1:3
            for n = (-3):(3)
                p1 = I(x+m, y+n);
                p2 = I(x-m, y-n);
                if abs(p1-I(x,y))<abs(p2-I(x,y))
                    v(i) = p1;
                else
                    v(i) = p2;
                end
                i = i+1;
            end
        end
        if abs(I(x,y-3)-I(x,y)) < abs(I(x,y+3)-I(x,y)) 
            v(i) = I(x,y-3);
        else
            v(i) = I(x,y+3);
        end
        i = i+1;
        if abs(I(x,y-2)-I(x,y)) < abs(I(x,y+2)-I(x,y)) 
            v(i) = I(x,y-2);
        else
            v(i) = I(x,y+2);
        end
        i = i+1;
        if abs(I(x,y-1)-I(x,y)) < abs(I(x,y+1)-I(x,y)) 
            v(i) = I(x,y-1);
        else
            v(i) = I(x,y+1);
        end
        v(i+1) = I(x,y);
        avg = mean(v);
        O(x,y) = avg;
    end
end

