function [out_img] = apply_anisotropic_diffusion(in_img, iternum, K, lambda, which_g)
       
    for i = 1:iternum
        [N S E W] = create_nearest_neighbor_difference_arrays(in_img);
        
        if which_g == 1
            c_N = g1(abs(N),K);
            c_S = g1(abs(S),K);
            c_E = g1(abs(E),K);
            c_W = g1(abs(W),K);
        else
            c_N = g2(abs(N),K);
            c_S = g2(abs(S),K);
            c_E = g2(abs(E),K);
            c_W = g2(abs(W),K);
        end;
         
       in_img = in_img + lambda.*(c_N.*N + c_S.*S + c_E.*E + c_W.*W);
    end
    
    out_img = in_img;
end