You are here: Symbol Reference > MtxExpr Namespace > Classes > Matrix Record > public > Filter2D Method > Matrix.Filter2D Method (TMtx, TMtx, TMtxRect, TMtxRect, TMtxPoint, TVecInt)
MtxVec VCL
ContentsIndex
PreviousUpNext
Matrix.Filter2D Method (TMtx, TMtx, TMtxRect, TMtxRect, TMtxPoint, TVecInt)

Applies 2D filtering

Pascal
function Filter2D(const Src: TMtx; const Kernel: TMtx; const SrcRect: TMtxRect; const DstRect: TMtxRect; const KernelAnchor: TMtxPoint; const Buffer: TVecInt = nil): TMtx; overload;

Computes the dot-product between the kernel matrix and underlaying source matrix elements for each possible position of the kernel and stores the result in to the calling object. Typically the kernel is much smaller than the source matrix. The operation is equivalent to the 2D Convolution and is used also for image resampling, image blurring etc.. 

SrcRect must meet the following condtitions: 

SrcRect.Width <= (Src.Cols - Kernel.Cols) SrcRect.Height <= (Src.Rows - Kernel.Rows

KernelAnchor defines the value to be written in the destination matrix relative to the corresponding position of the Kernel in the Src matrix. This can be for example center element (Kernel.Rows div 2, Kernel.Cols div 2), top left (0,0), bottom right (Kernel.Rows-1, Kernel.Cols-1) element etc... The X and Y coordinates may not exceed Kernel row or column count. 

The kernel can be square or not. Symmetric or not. The only requirement is that it is rectangular. 

It is the users responsability to select such combination of SrcRect, Kernel size and KernelAnchor to get desired border value processing. If the Kernel is 3x3 in size, then it would make sense that SrcRect leaves out 2 rows above and below and also 2 columns left and right in the Src matrix. These regions then need to be initialized to some value for example 0 for black color or use the value of the closest border element. 

The DstRect defines the area to be overwritten in the destination matrix. The width and height parameters of DstRect and SrcRect need to match. The Size of the destination matrix needs to be big enough to allow DstRect position and dimensions.

var H, A, B: Matrix; i: integer; SrcRect, DstRect: TRect; KernelAnchor: TPoint; begin H := [[1, 1, 1], [1, 1, 1], [1, 1, 1]]; // simple average kernel A := [[ 0, 0, 0, 0, 0, 0, 0, 0, 0], [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5], [ 0, 0, 0, 1, 1, 1, 1, 1, 0], [ 0, 0, 0, 2, 2, 2, 2, 2, 0], [ 0, 0, 0, 3, 3, 3, 3, 3, 0], [ 0, 0, 0, 4, 4, 4, 4, 4, 0], [ 0, 0, 0, 5, 5, 5, 5, 5, 0], [ 0, 0, 0, 6, 6, 6, 6, 6, 0], [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]]; SrcRect := TRect.Create(3,2, A.Cols-1, A.Rows-1 ); DstRect := TRect.Create(0,0, SrcRect.Width, SrcRect.Height); KernelAnchor := TPoint.Create(H.Rows div 2, H.Cols div 2); //center element B.Size(DstRect.Height, DstRect.Width); //could be bigger also B.Filter2D( A, H, SrcRect, DstRect, KernelAnchor); // B = [[ 7.5, 10.5, 10.5, 10.5, 7.5], //top left element at Index A[2,3] // [ 12 , 18 , 18 , 18 , 12 ], // [ 18 , 27 , 27 , 27 , 18 ], // [ 24 , 36 , 36 , 36 , 24 ], // [ 30 , 45 , 45 , 45 , 30 ], // [ 23.5, 34.5, 34.5, 34.5, 23.5]] end;
Examples on GitHub
Copyright (c) 1999-2025 by Dew Research. All rights reserved.
What do you think about this topic? Send feedback!