In real-time image acquisition, it is unavoidable to introduce noise, especially interference noise and salt and pepper noise, the existence of noise has serious effect on edge detection, median filter is a kind of nonlinear smoothing count based on ranking statistic theory, can effectively smooth noise, and can effectively protect the edge information of image. So it is widely used in the edge extraction of digital image processing, the basic principle is to replace the value of a point in a digital image or a digital sequence with the median value of all points in the neighborhood of the point.
Median filter has good filtering effect on salt and pepper noise, especially in filtering noise, it can protect the edge of signal, so it is not blurred. These excellent properties are not in the linear filtering method. Moreover, the median filtering algorithm is simple and easy to implement with hardware. In this article we first use MATLAB to achieve the median filter.
Median filtering method is to treat the current pixel, select a template 3x3, 5x5 or other, here choose a 3x3 matrix, the template for its adjacent several pixels, the template's pixels from small to large to sort, and then use the median of the template to replace the value of the original pixel method.
Sorting algorithms
When we use the 3x3 window to get 9 pixels in the field, we need to sort the 9 pixel values, in order to improve the sorting efficiency, sorting algorithm idea.
(1) in descending order for each row of pixels in the window, the maximum, middle and minimum values are obtained.
(2) Compare the minimum value of three rows to the third column, whichever is the maximum.
(3) The maximum value of three rows is compared to the first column, whichever is the minimum.
(4) Compare the median of the three rows with the second column, and then take the middle value again.
(5) The previous three values are sorted again, the median value is the median of the window.
Sort sorting function
Sort (a) if a can make a matrix or row and column vectors, the default is to sort a in ascending order.
Sort (a) is the default ascending order, while sort (a, ' descend ') is sorted in descending order.
Sort (a) if A is a matrix, the columns of a are sorted in ascending order by default
Sort (A,dim)
Dim=1 is equivalent to sort (A)
Dim=2 in ascending order for each line element in matrix A
Sort (A, dim, ' descend ') sorts each row of the matrix in descending order
MATLAB code implementation
Median Filter
1%RGB_YCBCR2 CLC;3 clear All;4 close all;5 6Rgb_data = Imread ('lena.jpg');%7 8R_data = Rgb_data (:,:,1);9G_data = Rgb_data (:,:,2);TenB_data = Rgb_data (:,:,3); One A%imshow (rgb_data); - -[Row,col, DIM] =size (rgb_data); the -Y_data =zeros (row,col); -Cb_data =zeros (row,col); -Cr_data =zeros (row,col); +Gray_data =Rgb_data; - + forR =1: ROW A forc =1: COL atY_data (r, c) =0.299*r_data (R, C) +0.587*g_data (R, c) +0.114*B_data (R, c); -Cb_data (r, c) =-0.172*r_data (R, C)-0.339*g_data (R, c) +0.511*b_data (R, c) + -; -Cr_data (r, c) =0.511*r_data (R, C)-0.428*g_data (R, c)-0.083*b_data (R, c) + -; - End - End - inGray_data (:,:,1)=Y_data; -Gray_data (:,:,2)=Y_data; toGray_data (:,:,3)=Y_data; + - Figure ; the imshow (gray_data); * $%Median FilterPanax NotoginsengIMGN = Imnoise (Gray_data,'Salt & Pepper',0.02); - the Figure ; + imshow (IMGN); A the + forR =2: row-1 - forc =2: col-1 $median3x3 =[IMGN (R-1, C-1) IMGN (R1, c) IMGN (R-1, c+1) $IMGN (r,c-1) IMGN (r,c) IMGN (r,c+1) -IMGN (r+1, C-1) IMGN (r+1, c) IMGN (r+1, c+1)]; -Sort1 = sort (median3x3,2,'Descend'); theSort2 = Sort ([Sort1 (1), Sort1 (4), Sort1 (7)],'Descend'); -Sort3 = Sort ([Sort1 (2), Sort1 (5), Sort1 (8)],'Descend');WuyiSort4 = Sort ([Sort1 (3), Sort1 (6), Sort1 (9)],'Descend'); theMid_num = Sort ([Sort2 (3), Sort3 (2), Sort4 (1)],'Descend'); -Median_img (r,c) = Mid_num (2); Wu End - End About $ Figure ; -Imshow (MEDIAN_IMG);
Comparison before and after treatment
Gray Lena
Lena after adding salt and pepper noise
Lena of median filter after adding salt and pepper noise
It can be seen that the filtering effect of median filter on salt and pepper noise is very good. Light look at my processing after the picture may not see too obvious contrast, interested friends can try their own. The sort of mtalab here is basically according to the sort algorithm step by step, fully conform to the idea of FPGA implementation, so that the basic principle of mastering the algorithm and Verilog oneself can try to implement with FPGA.
Here it is necessary to emphasize that this filter on the edge of the image is not processed, because we are in the mean or median value, the resulting 3x3 matrix, the first row of pixels is not processed, the same image of the outermost edge of the pixel is not processed, but this has little effect on the whole, so the back to get, The next article is to implement Sobel edge detection.
Please specify the source: Ninghechuan (Ning River)
Personal Subscription Number: Open Source FPGA
If you want to receive a personal blog post in time, you can scan the QR code on the left (or long press the QR code) to follow your personal subscription number
Know Id:ninghechuan
Micro Bo Id:ninghechuan
Original address: www.cnblogs.com/ninghechuan/p/9527915.html