View Raw SPL
/*****************************************************************************
*                                                                            *
*   MEDFILT.SPL  Copyright (C) 2011 DSP Development Corporation              *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Median filter a standard or RGB image                       *
*                                                                            *
*   Revisions:    6 Mar 2011  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/

#include 

#if @HELP_MEDFILT

    MEDFILT

    Purpose: Performs 2D median filtering on an image

    Syntax:  medfilt(image, ksize)

              image - An array, the input image
              ksize - Optional. An integer, the filter kernel size. Must
                      be odd, defaults to 3 (3x3 kernel).

    Returns: An array, the filtered image

    Example:
             W1: density(ravel(readb("baboon.dat", ubyte), 128));gray
             W2: medfilt(w1)

             W1 reads and shapes a 128x256 raw binary image.
             W2 contains the 3x3 median filtered output.

    Example:
             W1: readimage(gethome + "data\mandrill.bmp")
             W2: medfilt(w1, 5)

             W1 reads a 250x240 bitmap image.  The result is a 24 bit
             RGB image. W2 contains the 5x5 median filtered output.

    Remarks:
             For 24 bit RGB images, MEDFILT automatically performs filtering
             on each R, G, B component.

             If the filter kernel is even, the size is incremented to
             make it odd.

    See Also:
             Conv2d
             Iminterp
             Maxfilt
             Minfilt
             Nonlin2d
#endif


/* 2D median filtering */
medfilt(img, ksize)
{
        if (argc < 2)
        {
                if (argc < 1) error("medfilt - input series required");
                
                ksize = 3;
        }

        /* core function */
        img = rgbnonlin2d(img, ksize, 0);
        
        return(img);
}