Reference herein blog: https://www.cnblogs.com/Imageshop/archive/2013/04/26/3045672.html
Median filtering is based on native sorting algorithm, such algorithm substantially complexity O (R & lt 2 approximately), when the larger radius filtering, sorting algorithm becomes very slow. There are many improvements to this algorithm, introduce classical here
Huang algorithm in O (1) algorithm, both the memory based on an algorithm, but the latter more memory.
At a sorting algorithm obvious shortcomings is no memory. When a core mobile rightward, except the leftmost and rightmost columns core data changes, the same intermediate data should be stored, and the sorting algorithm
He did not do this. Huang algorithm idea is to build a nuclear histogram to count the number of pixels for each gradation within the nucleus. When the core moves to the right, a new one will be added to all the data in the histogram, while the most
The old data is deleted from the left column of the histogram, as shown in FIG. Such data can be made so that most of the memory, to reduce duplication operation. When the histogram is updated, can be found in the accumulated pixel values from left to right.
The following is the algorithm codes specific implementation steps:
1. Each row will start the histogram, pixel count, the value of the variable is cleared, all the pixels covered by the core added to the histogram.
2. Calculate the median, sumcnt less than the number of pixels and the gradation value. If the current sumcnt than or equal to the threshold value, it indicates that the actual value is less than the current median, the histogram minus the number of pixels to the left, while the median
Also decreases until sumcnt less than the threshold, then the median is the value at this time (plus the value of the number of pixels sumcnt greater than or equal to the threshold value); if the median plus the number of pixels sumcnt still smaller than the threshold value, that practice
Inter median is greater than the current value, the histogram right cumulative number of pixels, while increasing the median, median until sumcnt plus the number of pixels greater than equal to the threshold, then the median is the value at this time (sumcnt less than a threshold).
3. Nuclear each movement of the right one, nuclear histogram update, comparing a gray value with the median of each newly added, smaller than the median plus 1, compare each gradation value and subtracting a median, the median reduction of less than 1. carried out
Step 2 continues to move once the core, until the whole image is completed are filtered.
% Median filtering HWL % Input: 8 bit depth image, filtering the radius ( 3 * radius of 3 to 1, the percentage value) function grayimg = medianfilterhuang (grayimg, RADIUS, PERCENTAGE) [SIZEX, sizeY] = size (grayimg) ; thres is = Int16 ((( 2 * RADIUS + . 1 ) ^ 2 * PERCENTAGE)); % extended boundary extendimg = Int16 (horzcat (repmat (grayimg (:, . 1 ), . 1 , RADIUS), grayimg, repmat (grayimg (:, sizeY), . 1 , RADIUS))); extendimg = Int16 (vertcat (repmat (extendimg ( . 1 , :), RADIUS, . 1), extendimg, repmat (extendimg (SIZEX, :), RADIUS, . 1 ))); for I = RADIUS + . 1 : SIZEX + RADIUS % initialize histogram for each line a histogram of all pixels added Histogram = Int16 (zeros ( 256 , . 1 )); sumcnt = Int16 ( 0 ); Median = Int16 ( 0 ); for J = RADIUS + . 1 : sizeY + RADIUS % traversing convolutional cover line for K = I-RADIUS: I + RADIUS IF (J> RADIUS + . 1 ) %By deleting the left column, a right column to a new Histogram (extendimg (K, J -radius- . 1 ) + . 1 ) = Histogram (extendimg (K, J-radius- . 1 ) + . 1 ) - . 1 ; Histogram (extendimg (K, J RADIUS +) + . 1 ) = Histogram (extendimg (K, J + RADIUS) + . 1 ) + . 1 ; % only care about the number of pixels in a gradation value of the left IF (extendimg (K, J-radius- . 1 ) < median) sumcnt = sumcnt- . 1 ; End IF (extendimg (K, J + RADIUS) < Median) sumcnt = sumcnt + . 1 ; End the else % of the line for H = J-RADIUS: J + RADIUS Histogram (extendimg (K, H) + . 1 ) = Histogram (extendimg (K, H) + . 1 ) + . 1 ; End End End % sumcnt the median number of pixels is not accumulated inside % old value is too large the while (sumcnt> = thres is) median = median- . 1 ; sumcnt=sumcnt-histogram(median+1); end %旧中值偏小 while(sumcnt+histogram(median+1)<thres) sumcnt=sumcnt+histogram(median+1); median=median+1; end grayimg(i-radius,j-radius)=median; end end end
Performance analysis found that most of the time spent on Huang algorithm update nuclear histogram. And because it is O (r) complexity, and therefore when the radius of the filter becomes large, the time taken to update the histogram part also increases, and the calculated
The median time spent portion has been fixed, because of its complexity is O (1), regardless of the radius of the filter. So in order to make the complexity of the updated histogram can be reduced to O (1), will enhance the memory of the algorithm. It can be seen adjacent row
There are also the same as the overlap between the nuclear center, which is adjacent columns with prior to the nuclear center there is the same behavior coincides similar. Thus, we can maintain a histogram of each column, and a histogram can be summed by nuclear histogram columns
Get updated core when the rightmost one histogram moves down one row, the updated column histograms, minus the leftmost column histogram, nuclear will be updated. So long as each update histograms are moving a column histograms,
While the rest are histogram subtraction operation, it is worth noting that, such implementations have utilized a perfect matrix of parallel addition and subtraction in place for iteratively executed sequentially. Thus, while the update process appears to have a lot of addition and subtraction operations,
In fact quickly executed in parallel.
Another point, due to the parallelization of matrix addition and subtraction, sumcnt can not be used to track changes in the threshold value. If the direct iteration cumulative histogram, the worst case adder 255 is to be performed. In order to calculate the value faster, may be employed
Two histograms for four high storage level (the histogram of size 16 * 1), storage of the whole low-level bit (256 * 1 histogram size). First accumulation level for each narrow, then the accumulated value from the low level found in the respective ranges. just
So at the expense of memory space.
The following is the algorithm codes specific implementation steps:
1. extended image boundary, the boundary extension line r + 1, r rows other boundary extension; two histograms generated for each column, and to their initialization, before the addition of 2 * r + 1 columns of each pixel; generating a low-level link high-level index matrix.
2. Each row starts clearing nuclear histogram, the histogram update each column covered core (down one line), was added to the nuclei histogram columns.
3. Find the value of the initialization sumcnt = 0, where it represents a value equal to less than a pixel and the gradation, the iteration level histogram from left to right, until sumcnt threshold value or more; and then determining the range from the top right to left iteration
Low-level histogram until sumcnt (minus the number of pixels of iterations gradation) is less than the threshold value, the median value of the current iteration.
4. Nuclear each movement to the right, update the histogram nuclear column histogram, step 3 is performed once, then continue moving the core, until the entire filter of FIG.
function grayimg = medianfilterdp (grayimg, RADIUS, PERCENTAGE) [SIZEX, sizeY] = size (grayimg); thres is = Int16 ((( 2 * RADIUS + . 1 ) ^ 2 * PERCENTAGE)); % extended boundary extendimg = Int16 (horzcat (repmat (grayimg (:, . 1 ), . 1 , RADIUS), grayimg, repmat (grayimg (:, sizeY), . 1 , RADIUS))); extendimg = Int16 (vertcat (repmat (extendimg ( . 1 , :), RADIUS + . 1 , . 1 ), extendimg, repmat (extendimg (SIZEX, :), RADIUS, . 1 ))); % for each column to maintain a high level all bit histogram and a histogram histogram_cols_low= Int16 (zeros (sizeY + 2 * RADIUS, 256 )); histogram_cols_high = Int16 (zeros (sizeY + 2 * RADIUS, 16 )); % a full bit histogram value calculation used and a high level histogram histogram_low = Int16 (zeros ( . 1 , 256 )); histogram_high = Int16 (zeros ( . 1 , 16 )); % all bit transfer high acceleration index grayrange = Int16 (zeros ( . 1 , 256 )); for I = . 1 : 16 grayrange (:, ( I - . 1 ) * 16+1:i*16)=i; end %初始化各列直方图 for row=1:2*radius+1 for col=1:sizey+2*radius histogram_cols_low(col,extendimg(row,col)+1)=histogram_cols_low(col,extendimg(row,col)+1)+1; histogram_cols_high(col,grayrange(extendimg(row,col)+1))=histogram_cols_high(col,grayrange(extendimg(row,col)+1))+1; end end for row=int16(radius+2:sizex+radius+1) for col=int16(radius+1:sizey+radius) if(col>radius+1) %更新右边新列,删除左边旧列 histogram_cols_low(col+radius,extendimg(row+radius,col+radius)+1)=histogram_cols_low(col+radius,extendimg(row+radius,col+radius)+1)+1; histogram_cols_low(col+radius,extendimg(row-radius-1,col+radius)+1)=histogram_cols_low(col+radius,extendimg(row-radius-1,col+radius)+1)-1; histogram_cols_high(col+radius,grayrange(extendimg(row+radius,col+radius)+1))=histogram_cols_high(col+radius,grayrange(extendimg(row+radius,col+radius)+1))+1; histogram_cols_high(col+radius,grayrange(extendimg(row-radius-1,col+radius)+1))=histogram_cols_high(col+radius,grayrange(extendimg(row-radius-1,col+radius)+1))-1; histogram_low=histogram_low+histogram_cols_low(col+radius,:)-histogram_cols_low(col-radius-1,:); histogram_high=histogram_high+histogram_cols_high(col+radius,:)-histogram_cols_high(col-radius-1,:); else histogram_low(:,:)=0; histogram_high(:,:)=0; %行首手动初始化各列 for index=1:2*radius+1 histogram_cols_low(index,extendimg(row+radius,index)+1)=histogram_cols_low(index,extendimg(row+radius,index)+1)+1; histogram_cols_low(index,extendimg(row-radius-1,index)+1)=histogram_cols_low(index,extendimg(row-radius-1,index)+1)-1; histogram_cols_high(index,grayrange(extendimg(row+radius,index)+1))=histogram_cols_high(index,grayrange(extendimg(row+radius,index)+1))+1; histogram_cols_high(index,grayrange(extendimg(row-radius-1,index)+1))=histogram_cols_high(index,grayrange(extendimg(row-radius-1,index)+1))-. 1 ; histogram_low = histogram_low + histogram_cols_low (index, :); histogram_high = histogram_high + histogram_cols_high (index, :); End End % of all the number of pixels before the median and sumcnt = Int16 ( 0 ); for gray_high = . 1 : 16 sumcnt = sumcnt + histogram_high ( gray_high); IF (sumcnt> = thres is) BREAK ; End End for gray_low = gray_high * 16 : - . 1:(gray_high-1)*16+1 sumcnt=sumcnt-histogram_low(gray_low); if (sumcnt<thres) break; end end grayimg(row-radius-1,col-radius)=gray_low-1; end end end
The following is a comparison of the performance of the two algorithms, it can be found in a smaller radius filtering time, with better Huang algorithm; when the filter radius> = 8, with the latter algorithm faster. In practice we generally use less than a large radius, but the two
Idea of the algorithm is very good, especially the second of space for time, we may be able to discover the presence of repetitive horizontal, but it is easy to ignore repetitive in the longitudinal direction.