“This is the 7th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
preface
Hello! Friend!!!
Thank you very much for reading haihong’s article, if there are any mistakes in the article, please point out ~
Self-introduction ଘ(੭, ᵕ)੭
Nickname: Haihong
Tag: programmer monkey | C++ contestant | student
Introduction: because of C language to get acquainted with programming, then transferred to the computer major, had the honor to get some national awards, provincial awards… Has been confirmed. Currently learning C++/Linux/Python
Learning experience: solid foundation + more notes + more code + more thinking + learn English well!
Median filtering
Concept: median filtering is based on the theory of order statistics, a kind of nonlinear signal processing technology can effectively restrain noise, median filtering is the basic principle of the digital image or the value of the point in the sequence used at various points in the field of a point at which the median value replace, the pixels around them close to the real value of order to remove isolated noise points. The method is to use a two-dimensional sliding template of some structure to sort the pixels in the plate according to the size of pixel value, and generate a two-dimensional data sequence with monotonous rising (or falling). The two-dimensional median filter output is G (x,y) = Med {f(x-k, y-L),(k, L ∈W)}, where f(x,y) and g(x,y) are the original image and the processed image respectively. W is a two-dimensional template, usually 33,55 areas, can also be different shapes, such as line, circle, cross, circle and so on.
Principles of interpretation: Actually, median filtering is easier to understand. First, we need to find the median and then use it to replace the pixels in a position. For example, in an image, we take a 3*3 template, in which a template has 9 elements, we find the median value and replace the pixel in the middle with it, which is the median filtering. Here is:
MATLAB
Method one: medFilt2 () function implementation
t=imread('a1.jpg'); t=rgb2gray(t); T1 = imnoise (t, 'salt & pepper, 0.3); Subplot (1,2,1),imshow(t1),title(' add salt and pepper noise ') t2=medfilt2(t1,[3 3]); Subplot (1,2,2),imshow(t2),title(' median filtered ')Copy the code
Effect:Note: the first parameter in the medFilt2 () function must be two-dimensional. That is why the image is grayed first. What is the method for filtering the median value of color images? Certainly can ah, separately three channels median filter line.
t=imread('a1.jpg'); T1 = imnoise (t, 'salt & pepper, 0.3); Subplot (1, 2, 1), imshow (t1), title (' after add salt and pepper noise) t2 = t t2 (:, :, 1) = medfilt2 (t1 (:, :, 1), [3]). t2(:,:,2)=medfilt2(t1(:,:,2),[3 3]); t2(:,:,3)=medfilt2(t1(:,:,3),[3 3]); Subplot (1,2,2),imshow(t2),title(' median filtered ')Copy the code
Effect: Method 2: write your own median filter function median filter function:
Function [img] = median_filter (image, m) % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - % % median filtering input: % the image: the original % m: Template template size 3 * 3, m = 3% output: % img: median filtering processing after the image % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - n = m; [ height, width ] = size(image); x1 = double(image); x2 = x1; for i = 1: height-n+1 for j = 1:width-n+1 mb = x1( i:(i+n-1), j:(j+n-1) ); % get n*n matrix in image MB = MB (:); % change MB to vectorization, change a column vector mm = median(MB); % take the median value x2(I +(n-1)/2, j+(n-1)/2) = mm; end end img = uint8(x2); endCopy the code
Main function:
t=imread('a1.jpg'); T1 = imnoise (t, 'salt & pepper, 0.3); Imshow (t1),title(' add salt and pepper noise '); t2=median_filter(t,3); % call figure,imshow(t),title(' median filtered ')Copy the code
Effect: