matrixbase::ApplyFilter

Description

Apply filter.

Syntax

BOOL ApplyFilter( matrix & mfilter, int nPaddingOption = MFILTER_ZEROPADDINGWINDOW, BOOL bNormalize = TRUE )

Parameters

mfilter
[input] An N x N filter/mask matrix used by Applyfilter
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.
bNormalize
[input]replacement value with weight of mask/filter if TRUE

Return

Returns TRUE on success and FALSE on failure.

Examples

EX1

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

EX2

//Output result to matrix
void matrixbase_ApplyFilter_ex2()
{
    BOOL rc;
    
    matrix<double> mfilter(3,3) = {
        {0,  1,  0},
        {1,  1,  1},
        {0,  1,  0}
    };
    printf("Applied Filter:\n  (0 1 0)\n  (1 1 1)\n  (0 1 0)\n");
    
    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;
    rc = Mat2.ApplyFilter(mfilter); // with defaults: MFILTER_ZEROPADDINGWINDOW & Normalize
    if(!rc) printf("Error: ApplyFilter on a matrix failed. (Option:ZEROPADDINGWINDOW & Normalization)\n");
    else
        printf("Observe the ApplyFiltered 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.ApplyFilter(mfilter,MFILTER_MAPPADDINGWINDOW); // with default:Normalize
    if(!rc) printf("Error: ApplyFilter on a matrix failed. (Option:MAPPADDINGWINDOW & Normalization)\n");
    else
        printf("Observe the ApplyFiltered 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.ApplyFilter(mfilter,MFILTER_REPEATEDGEPADDINGWINDOW,FALSE); // with MFILTER_REPEATEDGEPADDINGWINDOW & non-Normalize
    if(!rc) printf("Error: ApplyFilter on a matrix failed. (Option:REPEATEDGEPADDINGWINDOW & No Normalization)\n");
    else
        printf("Observe the ApplyFiltered matrix window %s from %s. (Option:REPEATEDGEPADDINGWINDOW)\n",
          Mat4.GetName(),Mat1.GetName());
}

Remark

Takes an N x N filter (or mask) matrix and places it centered over each pixel in this image (or matrix). For each cell in the filter/mask it computes the product of the filter/mask element with the overlayed cell of the original image (matrix), sums all of the products, normalizes to the weight of the filter/mask (if specified to do so), and then replaces the pixel in the original image with the weighted average.

See Also

matrixbase::MedianFilter

header to Include

origin.h