MATLAB Implementation of mean filtering and median filtering

Source: Internet
Author: User

Currently, there are three typical image denoising algorithms:
 
Mean filtering algorithm: Also known as linear filtering, the main idea is the neighborhood average method, that is, using the gray scale of several pixels.
To replace the gray scale of each pixel. It can effectively suppress addition noise, but may cause image blur,
It can be improved to avoid smooth processing on the edge of the scene.
 

% X is the image to be filtered, and N is the template size (I .e. n × n) function D = avg_filter (x, n) A (1: N, 1: n) = 1; % A is the n × n template. All elements are 1 [height, width] = size (x); % the input image is of hightxwidth, and hight> N, width> Nx1 = double (x); x2 = x1; for I = 1: hight-n + 1 for j = 1: width-n + 1 c = x1 (I: I + (n-1), J: J + (n-1 )). * A; % obtain the elements of N rows and n columns starting from (I, j) in X1 by multiplying S = sum (c) with the template )); % calculate the sum of elements in the C matrix X2 (I + (n-1)/2, J + (n-1)/2) = S/(n * n ); % assign the mean value of each element after the operation with the template to the endend % of the element in the template center, and obtain the original value D = uint8 (X2 );

Median Filter: a non-linear smoothing filter Signal Based on Sorting statistics theory that can effectively suppress noise
Processing technology. The feature of median filtering is to first determine a neighborhood with a pixel as the center,
It is generally a Square area, or a circle or cross, and then the gray scale of each pixel in the area
Value sorting. The median value is used as the new gray value of the central pixel. The field is called a window.
When the mouth is moved, the image can be smoothly processed using the median filter. Its algorithm is simple and time is complex.
Low degree, but it is not suitable to use the median filter for images with many vertices, lines, and top points. It is easy to adapt
 

% The self-compiled median filter function. X is the image to be filtered, and N is the template size (I .e. n × n) function D = mid_filter (x, n) [height, width] = size (X ); % The input image is p × Q, and P> N, q> Nx1 = double (x); x2 = x1; for I = 1: height-n + 1 for j = 1: height-n + 1 c = x1 (I: I + (n-1), J: J + (n-1 )); % extract the N rows and n columns starting from (I, j) in X1, that is, the template (n × n) E = C (1 ,:); % is the first line of the C matrix for u = 2: n e = [E, C (u, :)]; % convert the C matrix into a row matrix end Mm = median (E); % mm is the value of X2 (I + (n-1)/2, J + (n-1)/2) = mm; % assign the value of each element in the template to the endend of the element in the template center. % the original value of the element not assigned is d = uint8 (X2 );


% %

Another version:

A=imread('lena_0.005.bmp '); A = double (a); [DEP, wide] = size (a); new_image = ones (SIZE (a); for I = 3: dep-2 for J = 3: Wide-2 new_image (I, j) = median ([A (I-2, J-2) A (I-2, J-1) A (I-2, J) A (I-2, J + 1) A (I-2, J + 2) A (I-1, J-2) A (I-1, J-1) A (I-1, j) A (I-1, J + 1) A (I-1, J + 2) a (I, J-2) a (I, J-1) a (I, j) a (I, j + 1) A (I, j + 2) a (I + 1, J-2) a (I + 1, J-1) a (I + 1, J) a (I + 1, J + 1) a (I + 1, J + 2) a (I + 2, J-2) a (I + 2, J-1) a (I + 2, J) A (I + 2, J + 1) a (I + 2, J + 2)]); endendfor I = 3: dep-2 % processes two new_image (I, 1) = new_image (I, 3); new_image (I, 2) = new_image (I, 2) = new_image (I, 3); new_image (I, wide) = new_image (I, wide-2); new_image (I, wide-1) = new_image (I, wide-2 ); endnew_image (1, :) = new_image (3, :); % assign all elements in the third row to new_image (2, :) = new_image (3, :); new_image (DEP, :) = new_image (dep-2, :); % assign all element values from the last row to new_image (dep-1, :) = new_image (dep-2 ,:); figure imshow (uint8 (A) Figure imshow (uint8 (new_image) % imwrite(uint8(new_image},'lena_0.005_median_5.bmp ', 'bmp ')

% % 5
.
Gini
Vina Filter: This method minimizes the mean square error between the original image and its restored image,
It is an adaptive filter that adjusts the filter effect based on local variance. For Gaussian Noise Removal
The effect is obvious.



Gaussian filter function:

% Self-compiled Gaussian filter function, S is the image to be filtered, n is the mean, k is the variance function D = gaussfilt (k, n, S) IMG = double (s ); n1 = floor (n + 1)/2); % calculated image Center for I = 1: N for j = 1: n B (I, j) = exp (-(i-n1) ^ 2 + (j-n1) ^ 2)/(4 * k)/(4 * pI * k ); end end % generates Gaussian Sequence B. Img1 = conv2 (IMG, B, 'same'); % perform Gaussian filter d = uint8 (img1) using the generated Gaussian Sequence convolution operation );

Main function:

% This is the main program file, including the main functional units, and the sub-function call try % experiment step 1: grayscale conversion hsf-imread('photo.jpg '); % read the color image c = rgb2gray (h ); % convert a color image into a grayscale image, level 1 figure, imshow (C), title ('original image'); % display the original image G = imnoise (C, 'gaussian ', 0.1, 0.002); % added Gaussian noise figure, imshow (G), title ('image after Gaussian noise '); % display the image after adding Gaussian noise % experiment step 2: Use the system pre-defined filter to perform mean filtering n = input ('Enter the Mean Filter template size \ n '); A = fspecial ('average', n); % generate the system's predefined 3x3 filter y = filter2 (A, G)/255; % use the generated filter for filtering, and normalize figure, imshow (Y), title ('result of mean filtering using the system function '); % display the filtered image % Step 3: use your own function to perform mean filtering y2 = avefilt (G, n); % call the self-compiled function to perform mean filtering. n is the template size figure, imshow (Y2 ), title ('result of mean filtering using a function compiled by myself '); % display the filtered image % Step 4: use the Matlab system function for median filtering n2 = input ('Enter the size of the median filtering template \ n'); Y3 = medfilt2 (G, [n2]); % The system function is called to perform the median filter. N2 is the template size, figure, imshow (Y3), and title ('result after the median filter using the Matlab system function '); % display filtered image % Step 5: Use your own function to perform the median filter Y4 = midfilt (G, N2); % call your own function to perform the median filter, figure, imshow (Y4), title ('result after filtering by using a self-compiled function '); % experiment step 6: use the Matlab system function for Gaussian filtering N3 = input ('Enter the mean of Gaussian filter \ n'); k = input ('Enter the variance of Gaussian filter \ n '); a2 = fspecial ('gaussian ', K, N3); % generate Gaussian Sequence Y5 = filter2 (A2, G)/255; % use the generated Gaussian Sequence for filtering figure, imshow (Y5), title ('result after Gaussian filter using Matlab function '); % display filtered image % experiment step 7: use a self-compiled function for Gaussian filtering y6 = gaussfilt (N3, K, g); % call the self-compiled function for Gaussian filtering, N3 is the mean, k is the variance figure, imshow (y6), title ('result after Gaussian filter using a self-developed function '); % display the filtered image catch % capture the exception disp (lasterr ); % if the program has an exception, output end



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.