1. What is median filtering?
Median filter is a sort of pixel gray value in a sliding window, which replaces the original gray value of the center pixel of the window, it is a nonlinear image smoothing method, It is good for suppressing the noise of the impulse noise, and it can effectively protect the edge and reduce the blur while suppressing the random noise.
is an example of median filtering of one-dimensional signals. For grayscale sequences 80, 120, 90, 200, 100, 110, 70, if arranged in size order, The result is 70, 80, 90, 10O, 110, 120, 200, where the gray value of the location is 10O, then the median value of the grayscale sequence is 100. The one-dimensional signal median filter is actually the signal value that uses the median value instead of the specified position (usually the central position of the original signal sequence). For the sequence mentioned above, the result of median filtering is to substitute the median value of 100 in the sequence 80, 120, 90, 200, 100, 110, 70, the signal Sequence Center position value 200, the resulting filtering sequence is 80, 120, 90, 100, 100, 110, 70. If 200 is a noise signal in this sequence, the noise point can be removed by this method.
Two-dimensional median filter algorithm is: for an image of the pixel matrix, take the target pixel as the center of a Sub-matrix window, this window can be 3*3, 5*5, etc., according to the need to select the pixel gray order within the window, take the middle of a value as the target pixel new gray Value. Window examples such as ooooxoooo above x for the target pixel, and the surrounding O to form the 3*3 matrix array, and then the 9 elements of the gray order, sorted by the intermediate element array[4] is the new gray value of x, so that the object of the pixel x is the median filter, Then iterate over the other required pixels to filter.
The realization method of median filter in image processing
1: by one of the images
The sampling window takes an odd number of data to sort2:
Replace the data to be processed with a sorted medianCan be the median filter algorithm implementation process, focusing on sorting, the most commonly used bubble sort ~ ~ Filter interval data from small to large to sort, and then take the median value, (if It is an odd number of data, then the median is only one, if an even number of data, the median value has two, can be averaged over two Data)
The following is a C language implementation of the median filter function:
unsigned char getmediannum (int * barray, int ifilterlen) {int i,j;//loop variable unsigned char btemp;//sort array by bubbling for (j = 0; J &L T iFilterLen-1; J + +) {for (i = 0; I < ifilterlen-j-1; i + +) {if (barray[i] > barray[i + 1]) {//interchange btemp = barray[i];barray[i] = BA Rray[i + 1];barray[i + 1] = btemp;}}} Calculate median if ((ifilterlen & 1) > 0) {//array has an odd number of elements, returns the middle of an element btemp = Barray[(ifilterlen + 1)/2];} The Else{//array has an even number of elements, returning the median value of two elements btemp = (barray[ifilterlen/2] + BARRAY[IFILTERLEN/2 + 1])/2;} Return btemp;}
Note: Barray is a shaping pointer, we pass in is generally an array, used to store the data to be sorted Ifilterlen is the length of the filter used in the image processing, because the value of the pixel range is 0~255, just is unsigned char range, So the return value of the function is unsigned char, if we want to handle the number is float type, or other type, the return value can also change ~ ~ The return value is btemp, that is, we want the median
the Following is a complete C language program, used in image processing
/************************************************************************* * Function Name: * Medianfilter () * parameter: * int Ifilterh-the height of the filter * int ifilterw-the width of the filter * int Ifiltermx-the Center element of the filter x coordinate * int Ifilte Rmy-the Center element y coordinate of the filter * Description: * This function has median filtering for DIB Images. /#define IFILTERW 1 #define IFILTERH 1 # Define IFILTERMX 1 #define IFILTERMY 1 #define widthbytes (bits) ((bits) + 4) unsigned char Getmedian Num (int * barray, int ifilterlen); void Medianfilter (unsigned char *pimg1,unsigned char *pimg,int nwidth,int nheight) {unsigned Char *lpsrc; Pointer to the source image unsigned char *lpdst; Pointer to the region to be copied int avalue[ifilterh*ifilterw]; Pointer to array of filters int i,j,k,l; Loop variable int llinebytes; Number of bytes per line of image LLinebytes = widthbytes (nwidth * 8); For (i=0;i<nwidth;i++,pimg++) (*pimg) = 0; Start median filter//line (remove edge several lines) for (i = ifiltermy; i < Nheight-ifilterh + ifiltermy + 1; i++) {//column (except Go to edge several columns) for (j = ifiltermx; J < Nwidth-ifilterw + ifiltermx + 1; j + +) {//point to new DIB line i, section J Pixel pointer lpdst = pImg + llinebytes * (nHeight-1-i) + j; Read filter array for (k = 0, k < ifilterh; k++) {for (l = 0; L < ifilterw; l++ ) {//pointing to Dib I-ifiltermy + k-line, j-ifiltermx + l-pixel pointer lpsrc = PImg1 + llinebytes * (nHeight-1-i + ifiltermy-k) + j-ifiltermx + l; Save Pixel Value Avalue[k * ifilterw + l] = *lpsrc; }}//get Median * lpdst = getmediannum (avalue, Ifilterh * ifilterw); }}} unsigned char getmediannum (int * barray, int ifilterlen) {int i,j; Cyclic variable unsigned char btemp; Sort the array by bubbling for (j = 0; J < iFilterLen-1; j + +) {for (i = 0; i < ifilterlen-j-1; i +) {if (barray[i] > barray[i + 1]) {//interchange btemp = b array[i]; barray[i] = Barray[i + 1]; Barray[i + 1] = btemp; }}}//calculate median if ((ifilterlen & 1) > 0) {//array has an odd number of elements, returning an element in the middle Btemp = Barray[(ifilterlen + 1)/2]; The Else {//array has an even number of elements, returning the middle two elements mean btemp = (barray[ifilterlen/2] + BARRAY[IFILTERLEN/2 + 1]) /2; } return btemp; }
On the median filtering algorithm and the C language implementation