# 16.15 Custom Filter

## Description

The filter2 X-Function applies a square filter matrix to a source matrix. The filter matrix is centered over each cell of the source matrix.

1. Create two matrixes with data. One will be the source matrix; the other is the filter matrix.
2. Activate the source matrix.
3. Select Analysis: Mathematics:Custom Filter from the Origin menu to open the filter2 dialog.

## Dialog Options

Recalculate Controls recalculation of analysis results None Auto Manual For more information, see: Recalculating Analysis Results The input matrix object. For help with range controls, see: Specifying Your Input Data The filter matrix object. The filter matrix may lie outside the source matrix at the edges. Choose a padding method (see Algorithm, below). ZeroPadding Pad with zeroes. MapPadding Map the corresponding inside cell values to outside. RepeatPadding Repeat edge values. If selected, normalize replacement value with weight of filter. The output matrix object. For help with the range controls, see: Output Results

## Algorithm

This function takes an $N\!$ by $N\!$ filter matrix and places it centered over each cell of the source matrix. For each cell in the filter, the function computes the product of the filter element with the overlaid cell of the original matrix, sums all of the products, normalizes to the weight of the filter (if selected), and then replaces the pixel in the original image with the recalculated value.

For example:

$\begin{pmatrix}2 & 3& 4 &5\\3 & 4 &5 &6\\4 & 5 &6 &7\\5 & 6 & 7 &8 \end{pmatrix}+\begin{pmatrix}1& 1& 1\\1& 2&1 \\1 & 1 &1 \end{pmatrix}=\begin{pmatrix}1.4& 2.4& 3.1 &2.5\\2.4 & 4 &5 &3.9\\3.1 & 5 &6 &4.6\\2.5 & 3.9 & 4.6 &3.6 \end{pmatrix}$

The value of cell(2,2) in the output matrix is:

$Cell(2,2)=\frac{2\times 1+3\times 1+4\times 1+3\times 1+4\times 2+5\times 1+4\times 1+5\times 1+6\times 1}{1+1+1+1+2+1+1+1+1}=4$

If the Normalize check box is selected, the overlaid cells' products will be normalized by the weight of the corresponding cell of the filter.

When applying a filter to the edge of the source matrix, some part of the filter may lie outside the source matrix boundary. When this occurs, the user can opt to pad the area outside the source matrix with zeros (Zero Padding), by reflection padding (Map Padding), or repeating edge values (Repeat Padding).