On the median filtering algorithm and the C language implementation

Source: Internet
Author: User

Source: About median filtering algorithm, and C language implementation

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.

The median filter can filter the spike pulses. The goal is to be more interested in filtered data. The filtered data retains the change trend of the original image, while the effects of the spike pulses on the analysis are removed.

    is an example of median filtering for 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 ~ ~ The 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 is two, Can be averaged over two data) here is a C language to implement median filter function:
UnsignedCharGetmediannum (int* Barray,intIfilterlen) {    intI,j;//Loop VariableUnsignedCharbtemp; //sort arrays with bubbling method     for(j =0; J < Ifilterlen-1; J + +)    {         for(i =0; I < ifilterlen-j-1; i + +)        {            if(Barray[i] > barray[i +1])            {                //InterchangeBtemp =Barray[i]; Barray[i]= Barray[i +1]; Barray[i+1] =btemp; }        }    }        //Calculate Median value    if((Ifilterlen &1) >0)    {        //Array has an odd number of elements, returning an element in the middleBtemp = barray[(Ifilterlen +1) /2]; }    Else    {        //the array has an even number of elements, returning the middle two elements meanBtemp = (Barray[ifilterlen/2] + Barray[ifilterlen/2+1]) /2; }    returnbtemp;}
Note: Barray is a shaping pointer, and we pass in a general array that stores the data to be sorted &nbsp; &nbsp; ifilterlen is the length of the filter &nbsp; &nbsp; when used in image processing, because the pixel range is 0~255CharChar, the return value can be changed if the number we are dealing with is float type, or other type. The return value is Btemp, which is the median value we want.
<span style="Color:rgb (Wuyi, Wuyi, Wuyi); > Below is a complete C language program used in image processing </span>
/************************************************************************* * Function Name: * Medianfilter () * parameter: * int IF Ilterh-the height of the filter * int IFILTERW-the width of the filter * int IFILTERMX-the center element of the filter x coordinate * int ifilt Ermy-the center element y coordinate of the filter * Description: * This function has median filtering for DIB images. ************************************************************************/#defineIfilterw 1#defineIfilterh 1#defineIFILTERMX 1#defineIfiltermy 1#defineWidthbytes (BITS) ((bits) + 31)/32 * 4)unsignedCharGetmediannum (int* Barray,intIfilterlen);voidMedianfilter (unsignedChar*pimg1,unsignedChar*pimg,intNwidth,intnheight) {unsignedChar*LPSRC;//pointer to source imageUnsignedChar*LPDST;//pointer to the region to copy    intAVALUE[IFILTERH*IFILTERW];//pointer to array of filters    inti,j,k,l;//Loop Variable    intLlinebytes;//number of bytes per line of imageLlinebytes = Widthbytes (nwidth *8);  for(i=0; i<nwidth;i++,pimg++ )        (*PIMG) =0; //Start Median filter//row (remove edges a few lines)     for(i = ifiltermy; I < Nheight-ifilterh + Ifiltermy +1; i++)    {        //column (excluding several columns of edges)         for(j = ifiltermx; J < Nwidth-ifilterw + Ifiltermx +1; J + +)        {            //Pointer to the first line of the new Dib, J-PixelLPDST = pImg + llinebytes * (Nheight-1-i) +J; //Read Filter Array             for(k =0; K < Ifilterh; k++)            {                 for(L =0; L < Ifilterw; l++)                {                    //Pointer to Dib I-ifiltermy + K line, section j-ifiltermx + L pixelsLPSRC = pImg1 + llinebytes * (Nheight-1-i + ifiltermy-k) + J-ifiltermx +l; //Save pixel valuesAvalue[k * ifilterw + l] = *lpsrc; }            }                        //Get medium Value* Lpdst = Getmediannum (Avalue, Ifilterh *ifilterw); }}}unsignedCharGetmediannum (int* Barray,intIfilterlen) {    intI,j;//Loop VariableUnsignedCharbtemp; //sort arrays with bubbling method     for(j =0; J < Ifilterlen-1; J + +)    {         for(i =0; I < ifilterlen-j-1; i + +)        {            if(Barray[i] > barray[i +1])            {                //InterchangeBtemp =Barray[i]; Barray[i]= Barray[i +1]; Barray[i+1] =btemp; }        }    }        //Calculate Median value    if((Ifilterlen &1) >0)    {        //Array has an odd number of elements, returning an element in the middleBtemp = barray[(Ifilterlen +1) /2]; }    Else    {        //the array has an even number of elements, returning the middle two elements meanBtemp = (Barray[ifilterlen/2] + Barray[ifilterlen/2+1]) /2; }        returnbtemp;}

About median filtering algorithm, and C language implementation (RPM)

Related Article

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.