简体   繁体   中英

Fast Median Filter in C / C++ for `UINT16` 2D Array

Does anyone know a fast median filter algorithm for 16-bit (unsigned short) arrays in c++?

http://nomis80.org/ctmf.html

This one seems quite promising, but it only seems to work with byte arrays. Does anyone know either how to modify it to work with shorts or an alternative algorithm?

The technique in the paper relies on creating a histogram with 256 bins for an 8 bit pixel channel. Converting to 16 bits per channel would require a histogram with 65536 bins, and a histogram is required for each column of the image. Inflating the memory requirements by 256 makes this a less efficient algorithm overall, but still probably doable with today's hardware.

Using their proposed optimization of breaking the histogram into coarse and fine sections should further reduce the runtime hit to only 16x.

For small radius values I think you'll find traditional methods of median filtering will be more performant.

Fast Median Search - An ANSI C implementation (PDF) is something for C, it's a paper with the title "Fast median search: an ANSI C implementation". The author claims it's O(log(n)), he also provides some code, maybe it'll help you. It's not better than your suggested code, but maybe a look worth.

std::vector<unsigned short> v{4, 2, 5, 1, 3};
std::vector<unsigned short> h(v.size()/2+1);
std::partial_sort_copy(v.begin(), v.end(), h.begin(), h.end());
int median = h.back();

Runs in O(N·log(N/2+1)) and does not modify your input.

This article describes a method for median filtering of images that runs in O(log r ) time per pixel, where r is the filter radius, and works for any data type (be it 8 bit integers or doubles):

Fast Median and Bilateral Filtering

See equations 4 and 5 in the following paper. The complexity is O(N*W) where W is the width of the filter and N is the number of samples.

See Noise Reduction by Vector Median Filtering .

I know this question is somewhat old but I also got interested in median filtering. If one is working with signals or images, then there will be a large overlap of data for the processing window. This can be taken advantage of.

I've posted some benchmark code here: 1D moving median filtering in C++

It's template based so it should work with most POD data types.

According to my results std::nth_element has poor performance for a moving median, as it must sort the window of values each time.

However, using a pool of values that is kept sorted, one can perform the median with 3 operation.

  1. Remove oldest value out of the pool (calls std::lower_bound)
  2. Insert new value into pool (calls std::lower_bound)
  3. Store new value in history buffer

The median is now the middle value in the pool.

I hope someone finds this interesting and contributes their ideas!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM