Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 508
user1359341 Avatar asked Apr 26 '12 18:04

user1359341


People also ask

What is median filter in image processing?

The median filter is widely used in digital image processing just because it preserves edge properties. Store the pixel values of input image in an array. For each pixel value store all the neighbor pixel value including that cell in a new array (called window).

Is there a good fast median search implementation for C?

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.

How can I get the median of a 1D pixel array?

At the heart it can use either a numpy sort routine or a call to one of the C library functions to return the median of a 1D pixel array (and optionally apply the threshold parameter). See the adaptive-medianrepo on github for source code and a sample image.

What is the average complexity of moving median filter?

Average complexity is N/2. Depending on the kernel size it might worth using a binary-insertion-sort algorithm instead, but I would like to avoid to use any recursive algorithm. /** * Moving Median Filter.


Video Answer


3 Answers

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.

like image 66
Mark Ransom Avatar answered Oct 15 '22 05:10

Mark Ransom


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.

like image 24
Sebastian Dressler Avatar answered Oct 15 '22 07:10

Sebastian Dressler


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.

like image 23
Joachim Avatar answered Oct 15 '22 06:10

Joachim