function Im = NLmeansfilter(I, nb, sw, h)
%NLFILTER Denoise the image of I
% IM = NLFILTER(I, NB, SW, H) outputs the denoised image of I
% I is the original image.
% NB is a number which shows the neiborhood of the point(i,j) the
% neiborhood of the point(i,j) is (2*nb + 1) * (2*nb + 1)
% SW is the search window. The size of the search window is (2*SW +
% 1)*(2*SW + 1)
% H acts as the filtering parameter
% Create the mirror(I_extend) of image I
sw = sw + nb;
[height, width] = size(I);
I_extend = uint8(zeros(2*sw + height, 2*sw + width));
I_extend(1:sw, 1:sw) = I(sw:-1:1, sw:-1:1);
I_extend(1:sw, sw + 1:sw + width) = I(sw:-1:1,1:width);
I_extend(1:sw, sw + width + 1:2*sw + width) = I(sw:-1:1, width:-1:width-sw+1);
I_extend(sw + 1:sw + height, 1:sw) = I(1:height,sw:-1:1);
I_extend(sw + 1: sw +height, sw + 1:sw + width) =