function [filtered_img] = median_filter(in_img, r)
    filtered_img = zeros(size(in_img));
    in_padded = padarray(in_img, [r r], 'replicate');
    for i = 1:size(filtered_img,1)
        for j = 1:size(filtered_img,2)
            filtered_img(i,j) = median(median(in_padded(i:i+2*r, j:j+2*r)));
        end
    end
end