2.2.3.9.47 matrixbase::MedianFilter

Description

Median filter.

Syntax

BOOL MedianFilter( int nWindowSize, int nPaddingOption = MFILTER_ZEROPADDINGWINDOW )

Parameters

nWindowSize
[input] The size of the window (N) for median filter
nPaddingOption
[input] Pads the area outside the borders in one of the following ways
=MFILTER_ZEROPADDINGWINDOW -- Zero Pad, pads with zeroes
=MFILTER_MAPPADDINGWINDOW -- Reflect Pad, values from immediate neighbors are repeated but more inside values are reflected padding more than 1 line;
=MFILTER_REPEATEDGEPADDINGWINDOW--- repeat edge padding.

Return

Returns TRUE on success and FALSE on failure.

Examples

EX1

void matrixbase_MedianFilter_ex1()
{
    matrix<double> mat1 = {
        {1,  2,  3,  4},
        {2,  4,  6,  8},
        {5, 10, 15, 20}
    };
    
    int rc = mat1.MedianFilter(4);
    if(!rc) printf("Error: MedianFilter on a matrix failed.");
    else{
    	printf("The matrix is:\n");
        for(int ii=0; ii< mat1.GetNumRows(); ii++){
			for(int jj=0; jj< mat1.GetNumCols(); jj++) 
				printf("%g   ", mat1[ii][jj]);
			printf("\r\n");
        }
    }
}

EX2

void matrixbase_MedianFilter_ex2()
{
    matrix<double> mat1 = {
        {1,  2,  3,  4},
        {2,  4,  6,  8},
        {5, 10, 15, 20}
    };
    MatrixPage MatPg1;
    MatPg1.Create("Origin");
    MatrixLayer MatLy1 = MatPg1.Layers(0);
    Matrix Mat1(MatLy1);
    Mat1 = mat1;
    
    MatrixPage MatPg2;
    MatPg2.Create("Origin");
    MatrixLayer MatLy2 = MatPg2.Layers(0);
    Matrix Mat2(MatLy2);
    Mat2 = mat1;
    int rc = Mat2.MedianFilter(3); // default is MFILTER_ZEROPADDINGWINDOW
    if(!rc) printf("Error: MedianFilter on a matrix failed. (Option:ZEROPADDINGWINDOW (default))\n");
    else
        printf("Observe the MedianFiltered matrix window %s from %s. (Option:ZEROPADDINGWINDOW (default))\n",
          Mat2.GetName(),Mat1.GetName());

    MatrixPage MatPg3;
    MatPg3.Create("Origin");
    MatrixLayer MatLy3 = MatPg3.Layers(0);
    Matrix Mat3(MatLy3);
    Mat3 = mat1;
    rc = Mat3.MedianFilter(3,MFILTER_MAPPADDINGWINDOW );
    if(!rc) printf("Error: MedianFilter on a matrix failed. (Option:MAPPADDINGWINDOW)\n");
    else
        printf("Observe the MedianFiltered matrix window %s from %s. (Option:MAPPADDINGWINDOW)\n",
          Mat3.GetName(),Mat1.GetName());
          
    MatrixPage MatPg4;
    MatPg4.Create("Origin");
    MatrixLayer MatLy4 = MatPg4.Layers(0);
    Matrix Mat4(MatLy4);
    Mat4 = mat1;
    rc = Mat4.MedianFilter(3,MFILTER_REPEATEDGEPADDINGWINDOW );
    if(!rc) printf("Error: MedianFilter on a matrix failed. (Option:REPEATEDGEPADDINGWINDOW)\n");
    else
        printf("Observe the MedianFiltered matrix window %s from %s. (Option:REPEATEDGEPADDINGWINDOW)\n",
          Mat4.GetName(),Mat1.GetName());
}

Remark

MedianFilter examines the N x N pixels centered around each pixel of an image (matrix).It finds the median value of the N x N pixels, and then replaces the central pixel with the median value. It is a useful filter to remove spot noise (white spots, black spots) in an image because such noisy pixels have values way off the median value.

See Also

matrixbase::ApplyFilter

Header to Include

origin.h