How to use MATLAB to realize median filtering

In real-time image acquisition, noise will inevitably be introduced, especially interference noise and salt and pepper noise. The presence of noise seriously affects the effect of edge detection. Median filtering is a nonlinear smoothing count based on the sorting statistics theory, which can effectively smooth Noise, and can effectively protect the edge information of the image, so it is widely used in the edge extraction of digital image processing. The basic principle is to use the value of a point in the digital image or digital sequence with all the points in the neighborhood of the point to sort the middle Value instead.

The median filter has a good filtering effect on salt and pepper noise, especially while filtering the noise, it can protect the edge of the signal and prevent it from being blurred. These excellent characteristics are not possessed by the linear filtering method. Moreover, the algorithm of median filtering is relatively simple and easy to implement with hardware. In this article, we first use MATLAB to implement median filtering.

The median filtering method is to select a template 3x3, 5x5 or others for the current pixel to be processed. Here, a 3x3 matrix is ​​selected. The template is composed of several adjacent pixels. The pixels of the template are sorted from small to large, and then used The method of replacing the original pixel value with the median value of the template.

How to use MATLAB to realize median filtering

Schematic diagram of sorting algorithm

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, the sorting algorithm idea is shown in the figure.

(1) Sort each row of pixels in the window in descending order to get the maximum value, the middle value and the minimum value.

(2) Compare the minimum value of the three rows, that is, the third column, and take the maximum value.

(3) Compare the maximum value of the three rows, that is, the first column, and take the minimum value.

(4) Compare the middle value of the three rows, that is, the second column, and take the middle value again.

(5) Sort the three previous values ​​again, and the median value obtained is the median value of the window.

sort sort function

sort(A) If A can be a matrix or row-column vector, the default is to sort A in ascending order.

sort(A) is the default ascending order, and sort(A,'descend') is the descending order. sort(A) If A is a matrix, the columns of A are sorted in ascending order by default. Sort(A, dim) is equivalent to sort(A) when dim=1

When dim=2, it means to arrange the elements of each row in matrix A in ascending order

sort(A, dim,'descend') sorts each row of the matrix in descending order

MATLAB Median Filter code implementation

1 %RGB_YCbCr 2 clc; 3 clear all; 4 close all; 5 6 RGB_data = imread('lena.jpg');% 7 8 R_data = RGB_data(:,:, 1); 9 G_data = RGB_data(:,:, 2);

10 B_data = RGB_data(:,:,3); 11 12 %imshow(RGB_data); 13 14 [ROW,COL, DIM] = size(RGB_data); 1516 Y_data = zeros(ROW,COL);

17 Cb_data = zeros (ROW, COL); 18 Cr_data = zeros (ROW, COL); 19 Gray_data = RGB_data; 20 21 for r = 1: ROW 22 for c = 1: COL23

Y_data(r, c) = 0.299*R_data(r, c) + 0.587*G_data(r, c) + 0.114*B_data(r, c); 24

Cb_data(r, c) = -0.172*R_data(r, c)-0.339*G_data(r, c) + 0.511*B_data(r, c) + 128;25

Cr_data(r, c) = 0.511*R_data(r, c)-0.428*G_data(r, c)-0.083*B_data(r, c) + 128;26

end27 end 28 29 Gray_data(:,:,1)=Y_data;30 Gray_data(:,:,2)=Y_data;31 Gray_data(:,:,3)=Y_data;32 33 figure;

34 imshow(Gray_data);

35 36 %Median Filter37 imgn = imnoise(Gray_data,'salt & pepper', 0.02);

38 39 figure;40 imshow(imgn);41 42 43 for r = 2: ROW-144

for c = 2: COL-145

median3x3 =ï¼»imgn(r-1,c-1) imgn(r-1,c) imgn(r-1,c+1)46

imgn(r,c-1) imgn(r,c) imgn(r,c+1) 47

imgn(r+1,c-1) imgn(r+1,c) imgn(r+1,c+1)]; 48

sort1 = sort(median3x3, 2,'descend');49

sort2 = sort([sort1(1), sort1(4), sort1(7)],'descend');50

sort3 = sort([sort1(2), sort1(5), sort1(8)],'descend');51

sort4 = sort([sort1(3), sort1(6), sort1(9)],'descend'); 52

mid_num = sort([sort2(3), sort3(2), sort4(1)],'descend');53

Median_Img(r,c) = mid_num(2);54 end55 end56 57 figure;58 imshow(Median_Img);

Comparison before and after treatment

How to use MATLAB to realize median filtering

Gray lena

How to use MATLAB to realize median filtering

Lena after adding salt and pepper noise

How to use MATLAB to realize median filtering

Lens with median filtering after adding salt and pepper noise

It can be seen that the median filter has a very good filtering effect on salt and pepper noise. Just looking at the picture after I processed it, you may not see too obvious contrast, friends who are interested can try it by themselves. The sorting part of MTALAB here is basically based on the sorting algorithm step by step, which is completely in line with the idea of ​​FPGA realization, so you can try to use FPGA to master the basic principles of the algorithm and Verilog yourself.

What needs to be emphasized here is that such filtering does not process the edges of the image. This is because when we calculate the average or median value, the generated 3x3 matrix, the pixels in the first row are not processed, and the outermost layer of the same image The edge pixels are not processed, but this has little effect on the whole, so I will do it later, and the next article will implement Sobel edge detection.

Cable for apple

Cable for apple

The cable for apple also call iphone cable,USB C apple,USB C to lightning, lightning cable.This USB Cable connects your iPhone, iPad, Mac, AirPods or iPod with Lightning Connector to your computer's USB port for syncing and charging. You can connect to the Apple USB power adapter for convenient charging from a wall outlet. 1 meter in length.Wisesir provide a variety of cables for Apple accessories, high performance chips, support fast charging.


Type C Chargercable for apple


Cable for apple,Usb C to Usb C,Data Link Cable,Usb Data Cable Price

Pogo Technology International Ltd , https://www.wisesir.net