Median filtering algorithm for fast median filtering algorithm:
In image processing, a certain degree of noise reduction is usually required before further processing such as edge detection. Median filtering is a nonlinear digital filter technique that is often used to remove noise from images or other signals. The idea is to check the sample in the input signal and determine whether it represents the signal, which is achieved using an observation window consisting of an odd number of samples. The values in the Watch window are sorted, and the median value in the middle of the view is used as the output. Then, discard the earliest values, get a new sample, and repeat the calculation above. Median filtering is a common step in image processing, which is particularly useful for speckle noise and salt and pepper noise. Saving Edge features makes it useful in situations where you don't want edges to blur.
To demonstrate the working process of the median filter, we add the following array to Observation window 3, repeating the value of the boundary:
x = [2 80 6 3]
Y[1] = median[2 2 80] = 2
Y[2] = median[2 6] = median[2 6 80] = 6
Y[3] = median[80 6 3] = median[3 6 80] = 6
Y[4] = median[6 3 3] = median[3 3 6] = 3
So
y = [2 6 6 3]
Where Y is the median filter output of x.
Common median filtering algorithm pseudo-code:
Input:image X of size m*n, kernel radius r.
Output:image Y as X.
For i = R to M-r do
For j = R-N-r do
Initialize list a[]
For a = I-r to I+r
For b = J-r to J+r
Add X (A, B) to a[]
End
End
Sort a[] Then Y (i, j) = A[a.size/2]
End
End
Before processing: after processing:
However, the complexity of the above algorithm is O (R2).
OPENCV Implementation code:
#include"cv.h"#include"highgui.h"#include<iostream>using namespacestd;using namespaceCV;intMainintargcChar*argv[]) {Mat src= Imread ("beauty.jpg"); Mat DST; //the parameters are written sequentially//Gaussian filter//src: Input image//DST: Output image//size (5,5) template sizes, odd//x-Directional Variance//y-Directional VarianceGaussianblur (Src,dst,size (5,5),0,0); Imwrite ("gauss.jpg", DST); //Median filter//src: Input image//DST:: Output image//template width, oddMedianblur (SRC,DST,3); Imwrite ("med.jpg", DST); //mean value filter//src: Input image//DST: Output image//Template Size//Point ( -1,-1): The location of the smoothed points, the center of the core for negative valuesBlur (Src,dst,size (3,3), point (-1,-1)); Imwrite ("mean.jpg", DST); //Bilateral filtering//src: Input image//DST: Entering images//Filter Template Radius//color space Standard deviation//standard deviation of coordinate spaceBilateralfilter (SRC,DST,5,10.0,2.0);//There is no effect of filtering here, do not understandImwrite ("bil.jpg", DST); Waitkey (); return 0;}View CodeFast median filtering algorithm: O (r) Complexity Huang algorithm: <a fast two-dimensional Median Filtering algorithm>
The core of this code is to maintain a kernel histogram that allows for fast reading and deletion of pixel values in the scanned area.
intStopamount = (2* Radius +1) * (2*radius +1) *percentile;intSum =0; for(intI =0; I <=255; i++) {Sum+=Histblue (I); if(Sum >= Stopamount)//meet Stop conditions{Value= I;//Get Middle Value Break; }}
O (1) CTMF Algorithm for complexity: <median Filter in Constant time.pdf>
First, for each column of images, we maintain a histogram for them (for 8-bit images, the histogram has 256 elements), and during the entire process, the histogram data must be maintained. The histogram of each column accumulates the information of 2r+1 adjacent pixels in the vertical direction, initially, the 2r+1 pixels are centered on each pixel of the first row, respectively. The histogram of the kernel is obtained by accumulating 2r+1 adjacent column histogram data. In fact, what we do is to decompose the kernel histogram into his corresponding set of column histograms, which are kept up-to-date in two steps over a constant period of time throughout the filtering process.
Consider the case of moving one pixel to the right from a pixel. For the current row, the Lies-Fontoux on the far right of the kernel needs to be updated first, while the data in the column histogram for that column is centered on the pixel at the corresponding position in the row. Therefore, you need to subtract the histogram of the last pixel and then add the histogram information of one of the pixels below it. The effect of this is to reduce the column histogram data by one line. This step is obviously a 0 (1) operation, with only one addition and one subtraction, regardless of radius r.
The second step is to update the kernel histogram, which is the sum of the histogram of 2r+1 columns. This is obtained by subtracting the leftmost column histogram data, and then adding the column histogram data for the column processed in the first step. This step is also an O (1) operation, as shown in 2. As mentioned earlier, the time-consuming of addition, subtraction, and calculation of the median of the histogram is some computation that depends on the depth of the image, regardless of the filter radius.
The difference between this algorithm and the previous Huang algorithm is that it takes advantage of the previous data, which is a bit like dynamic planning and a strategy for exchanging space for time.
Reference Blog: http://www.cnblogs.com/Imageshop/archive/2013/04/26/3045672.html
Fast median filtering algorithm for digital image processing