opencv-, learn with me. Median filtering in Digital image processing

Source: Internet
Author: User

Median filter (median filter) belongs to the content of spatial smoothing filter in Digital image processing (spatial filtering). It has good effect on eliminating salt and pepper noise.

    • Mathematical Principles

In order to tell the convenience, we take the grayscale image as an example. RGB three-channel color graphs can be obtained by combining each channel's median filter.

Digital images are stored in a matrix and can be stored in a OPENCV manual. Median filtering is done through the so-called mask operation operation. Take the 3x3 mask for example. The matrix form of the image is as follows:

0,0 0,1 0,2 0,3 0,4 0,5
1,0 The The 1,3 1,4 1,5
2,0 2,1 2,2 2,3 2,4 2,5
3,0 3,1 3,2 3,3 3,4 3,5
4,0 4,1 4,2 4,3 bis 4,5
5,0 5,1 5,2 5,3 5,4 5,5

Each box in the diagram represents a pixel, and the number pairs in each box represent the position of the pixel in the diagram. Median filtering starts with the position (in the first), creating a 3x3 mask

I-1,j-1 I-1,j I-1,j+1
I,j-1 I,j I,j+1
I+1,j-1 I+1,j I+1,j+1

Let (i,j) on it, take out the range of pixels covered by the template, fetch the nine worth of the median and then assign (to), as the new pixel value of the position. Then move the center of the template (I,j) to (), repeating the above operation until the entire picture is traversed. It should be noted that when we do median filtering, the median value in the template is assigned to a newly created new image, and the filtered image pixel value does not change. This ensures that the area covered by each template is the pixel of the original image.

Note:

    1. Mask does not have to use a 3x3 matrix to do, the size of the template is not certain.
    2. As you can see from the above procedure, the last row and the first row of the image, the last column and the first column of pixels are not traversed, we generally have two solutions to solve this problem.
      1. You can assign a value of zero to all pixels in that location.
      2. The original image can be added to the left and right of the corresponding all 0 vectors, and then the above-mentioned median filtering operation, so that all the original pixels can be traversed.
    • Based on OPENCV median filter program

OPENCV also uses a ready-to-use median filter function Medianblur

Medianblur (Inputarray src,outputarray DST,int ksize);

Wherein Inputarray SRC is the mat class filtered picture, Outputarray is the Mat class filter output results, Ksize is the size of mask, such as, if using a 3x3 template, ksize on the value of 3;

OPENCV-based middle finger filter code snippet is as follows,

1 //load the Original Image and get some informations2Mat src = imread ("010.jpg",1);3Namedwindow ("Originalimage");4Imshow ("Originalimage", SRC);5Cv_assert (src.depth () = =cv_8u);6 Const intNR =src.rows;7 Const intNC =Src.cols;8 Const intNchannels =src.channels ();9 Ten //OpenCV Solution One Mat Result_opencv; AMedianblur (SRC,RESULT_OPENCV,3); -Namedwindow ("Median FILTER_OPENCV"); -Imshow ("Median FILTER_OPENCV", RESULT_OPENCV);

Simulation results:

Original:

Results of median filtering with Medianblur:

    • Simulation of median filter function based on median filtering principle

This time we are no longer the gray-scale simulation, so it is necessary to briefly introduce the color map in the Mat class in the way of preservation, see (Source: opencv.org).

We can see that the three channels of the pixels in the color picture are saved in the same row, in the order of BGR. If the pointer p[i] is positioned in the blue channel (0,1), then P[i+mat.channels] is positioned in the blue channel of the next column.

The median filter function code is written based on the median filter principle as follows

1 //own median filter algorithm2uchar* previous =NULL;3uchar* current =NULL;4uchar* next =NULL;5uchar* Current_result_own =NULL;6 intarr[9];//Use 3*3 mask7  for(intI=1; i<nr-1; i++)8 {9previous = src.ptr<uchar> (i-1);TenCurrent = Src.ptr<uchar>(i); OneNext = src.ptr<uchar> (i+1); ACurrent_result_own = result_own.ptr<uchar>(i); -      for(intJ=nchannels;j<nchannels* (nc-1); J + +) -     { the          for(intk=0;k<3; k++) -         { -Arr[k] = previous[j+ (K-1)*Nchannels]; -arr[k+3] = current[j+ (K-1)*Nchannels]; +arr[k+6] = next[j+ (K-1)*Nchannels]; -         } +Bubble_sort (arr,9); ACURRENT_RESULT_OWN[J] = arr[4]; at     } - } -      - //set the pixels on the borders to zeros -Result_own.row (0). Setto (Scalar (0)); -Result_own.row (nr-1). Setto (Scalar (0)); inResult_own.col (0). Setto (Scalar (0)); -Result_own.col (nc-1). Setto (Scalar (0)); to  + //Show the result -Namedwindow ("Median Filter_own"); theImshow ("Median Filter_own", Result_own);

Where Bubble_sort is the sort function I write with the bubbling Sort method, the code snippet is as follows,

1 //************************//2 //Bubble Sort3 //************************//4 voidBubble_sort (int* Arr,intnum)5 {6 int temp;7      for(intI=1; i<num-1; i++)8     {9          for(intj=0; j<num-i;j++)Ten            { One                if(arr[j]>arr[j+1]) A                { -temp =Arr[j]; -ARR[J] = arr[j+1]; thearr[j+1] =temp; -                } -            } -     } +}

Simulation results:

opencv-, learn with me. Median filtering in Digital image processing

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.