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 ISW = 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: width); I _extend (1: SW, SW + width + * SW + width) = I (SW:-, 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) = I (:, :); I _extend (SW + 1: SW + height, SW + width + * SW + width) =... I (1: height, width:-1: width-SW + 1); I _extend (SW + height + 1: 2 * SW + height, 1: Sw) =... I (Height:-1: height-SW + 1, SW:-1:1); I _extend (SW + height + 1: 2 * SW + height, SW + 1: SW + width) =... I (Height:-1: height-SW + 1, 1: width); I _extend (SW + height + 1: 2 * SW + height, SW + width + * SW + width) =... I (Height:-1: height-SW + 1, width:-1: width-SW + 1); Im = zeros (height, width ); I _extend = double (I _extend); H = H * h; for I = Sw + 1: SW + height for J = Sw + 1: SW + width disp ([I-SW, j-SW]); Tw = 0.0; Im = 0.0; for M = I-SW + NB: I + Sw-Nb for n = J-SW + NB: J + Sw-Nb % weight calculate the weight of (X2, Y2) to (x1, Y1) % pay attention to the class type conversion uint8 to double ndif = I _extend (I-NB: I + NB, J-NB: J + Nb)-I _extend (m-NB: m + NB, N-NB: N + Nb); euclidean2 = (ndif (:) '* ndif (:); % euclidean2 = sum (ndif (:). * ndif (:)); W = exp (-euclidean2/h); % (m, n)'s weight to (I, j ); im = IM + W * I _extend (m, n); Tw = TW + W; end IM (I-SW, J-SW) = IM/TW; endendim = uint8 (IM); figure, imshow (IM );