#include #include #ifndef ICLMEDIAN_H #define ICLMEDIAN_H namespace icl { /// Class that provides median filter abilities /** The median class provides the ability for arbitrary mask sizes. Although the functionality of the IPP-optimized implementation and the fallback C++-implementation is identical, the performances are of different orders of magnitude. The fallback implementation uses the naive algorithm of sorting all N pixel values inside the median mask, and setting the destination pixel value to the mid-element of the sorted pixel list. This algorithm runs in O(w*h*N*log(N)) where (w,h) is the size of source images ROI, and N=n*n is the mask size used. The following code extract explains the operation of the fallback algorithm in Img-style notation for a single channel image, and Img8u type (the real implementation uses some special optimizations, that are not mentioned further):
      void channel_median_8u(Img8u &src, Img8u &dst, int w, int h, int c)
      {
          std::vector list;
          
          for(Img8u::iterator s=src.begin(c), d=dst.begin(c); s.inRegion() ; s++, d++ )
          {
              for(Img8u::iterator sR(s,w,h); sR.inRegion(); sR++)
              {
                 list.push_back(*sR);
              }
              std::sort(list.begin(),list.end());
              *d = list[w*h/2];
              list.clear();
          }
      }
      
The IPP implementation uses a fast median algorithm that estimates the median not by sorting a list, but by working on a histogram. Look at the IPPI manual for more detail.

No IPP for floats

Currently the IPP supports no 2D-median filtering for Ipp32f type, so the C++-fallback is used then.

Mask-Sizes

Although the fallback C++ implementation can work with arbitrary mask sizes, the Median will internally use odd mask dimension like 3x3 or 5x7. If an even width or height parameter is given to the Median constructor, the next higher odd value is used.

Benchmarks

table

3x3 5x5 51x51 51x3 3x51
icl8u, ipp ~5ms ~32ms ~434ms ~38ms ~430ms
icl8u, c++ ~146ms ~334ms ~84000ms ~3400ms ~3500ms
float, c++ ~181ms ~464ms ~115000ms ~4600ms ~4700ms

Details

Test A 1000x1000 icl8u-image with IPP

- mask size 3x3 ~5ms (highly optimized) - mask size 5x5 ~32ms - mask size 51x51 ~434ms (still usable!) - mask size 51x3 ~38ms - mask size 3x51 ~430ms (mask height specifies time usage)

Test B 1000x1000 icl8u-image no IPP

- mask size 3x3 ~146ms (30 times slower) - mask size 5x5 ~334ms - mask size 51x51 ~84000ms (unusable!) - mask size 51x3 ~3500ms - mask size 3x51 ~3500ms

Test C 1000x1000 icl32f-image (no IPP for floats)

- mask size 3x3 ~181ms (no special optimization) - mask size 5x5 ~464ms - mask size 51x51 ~115000ms (unusable!) - mask size 51x3 ~4600ms - mask size 3x51 ~4700ms

Example

Here is an examle, how to use the Median object.
    // create source and destination image
    Img8u src(640,480,3), *poDst=0;
  
    // acquire some image data
    ...
  
    // create the median object
    Median m(Size(5,5));

    // apply the median on the images - first call (slow)
    // destination image is renewed to 640x480x3 (memory allocation)
    m.apply(&src,&poDst);

    // enter iteration loop 
    while(1)
    {
        // aquire some new image data
        ...       

        // apply the median filter (now fast, as no memory
        // allocation must be performed)
        m.apply(&src,&poDst);

        // further processing steps
        ...
    }
    
*/ class MedianOp : public NeighborhoodOp { public: /// Constructor that creates a median filter object, with specified mask size /** @param maskSize of odd width and height Even width or height is increased to next higher odd value. **/ MedianOp (const Size &maskSize):NeighborhoodOp(adaptSize(maskSize)){} /// applies the median operation on poSrc and stores the result in poDst /** The depth, channel count and size of poDst is adapted to poSrc' ROI: @param poSrc source image @param ppoDst pointer to destination image **/ void apply(const ImgBase *poSrc, ImgBase **ppoDst); /// ensures that mask width and height are odd /** This is a workaround, necessary because of an ipp Bug that allows no even mask sizes here! @param size size to adjust @return adjusted size (width and height are rounded up to the next higher odd value **/ virtual Size adaptSize(const Size &size){ return Size(1+ 2*(size.width/2),1+ 2*(size.height/2)); } }; } #endif