Ask Your Question

Median filter greater than 5.

asked 2014-06-20 15:46:41 -0600

Lorenzo_P gravatar image

updated 2014-06-21 03:55:34 -0600

berak gravatar image


how can I efficiently implement a median filter with kernel size greater than 5. In my case I use float data. In partucular, I need a median filter with kernel size equal to 39.


edit retag flag offensive close merge delete

4 answers

Sort by ยป oldest newest most voted

answered 2014-06-20 16:00:08 -0600


#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace std;
using namespace cv;

int main( int argc, char** argv )

     Mat src = imread( "salt.tif", 1 );
     Mat dst;

     //Apply median filter
     medianBlur ( src, dst, 39 );
     imshow("source", src);
     imshow("result", dst);  

     return 0;

I must give credit to this blog post:

Also see to documentation:


edit flag offensive delete link more

answered 2014-06-22 16:25:08 -0600

Lorenzo_P gravatar image

I can use a median filter graeter than 5 for CV_8U depth image, but not for a CV_32F image. I need to filter an CV_32F image using a median filter with kernel size equal or greater than 40.

edit flag offensive delete link more

answered 2014-06-23 01:51:15 -0600

vaaksiainen gravatar image

updated 2014-06-23 01:55:25 -0600

Linear complexity median filter (such as OpenCV's 8-bit single and multichannel versions) is based on a moving histogram implementation. Naturally for floating point, there is none or it would be close to infinite sized histogram. Simplest workaround would be discretization - say - to 8-bit :) if that suits you. Or then increasing the number of bins in a custom way.

You can follow one of these guidelines or just implement O(N^3) version yourself. The last N comes from the search using std::nth_element in a neighborhood of N^2.

edit flag offensive delete link more

answered 2016-03-12 15:00:25 -0600

gdarmon gravatar image

updated 2016-03-12 15:00:36 -0600

I haven;t tried it, but have a look here, it is the same algorithm as openCV's however it is extended to 16 bits per pixel,

edit flag offensive delete link more

Question Tools


Asked: 2014-06-20 15:46:41 -0600

Seen: 1,839 times

Last updated: Mar 12 '16