Edge detection filters
In this sample the following edge detection filters are compared:
Explanation
The Sobel filter is a simple linear filter for marking gradients in an image. The Scharr filter is very similar to this; only the characteristic of the filter kernel is somewhat different. The Scharr filter has a better isotropy, i.e. the detection of edges is independent of the direction.
The Laplace filter detects edges based on the zero points of the discrete 2nd derivation of the image. This variant is susceptible to noise by comparison.
Variables
// Sobel
ipImageSobel : ITcVnImage;
ipImageSobelDisp : ITcVnDisplayableImage;
eSobel_DestDepth : ETcVnElementType := TCVN_ET_USINT;
aSobel_DerivOrder : ARRAY [0..1] OF UDINT := [1, 1];
nSobel_KernelSize : UDINT := 3;
fSobel_Scale : LREAL := 10;
fSobel_Delta : LREAL := 0;
eSobel_BorderExtra : ETcVnBorderInterpolationMethod := TCVN_BIM_DEFAULT;
// Scharr
ipImageScharr : ITcVnImage;
ipImageScharrDisp : ITcVnDisplayableImage;
eScharr_DestDepth : ETcVnElementType := TCVN_ET_USINT;
eScharr_FilterDir : ETcVnFilterDirection := TCVN_FD_X;
fScharr_Scale : LREAL := 1;
fScharr_Delta : LREAL := 0;
eScharr_BorderExtra : ETcVnBorderInterpolationMethod := TCVN_BIM_DEFAULT;
// Laplace
ipImageLaplace : ITcVnImage;
ipImageLaplaceDisp : ITcVnDisplayableImage;
eLaplace_DestDepth : ETcVnElementType := TCVN_ET_USINT;
nLaplace_KernelSize : UDINT := 3;
fLaplace_Scale : LREAL := 10;
fLaplace_Delta : LREAL := 0;
eLaplace_BorderExtra : ETcVnBorderInterpolationMethod := TCVN_BIM_DEFAULT;
Code
// Execute the Sobel Filter
hr := F_VN_SobelFilterExp(
ipSrcImage := ipImageIn
ipDestImage := ipImageSobel,
eDestDepth := eSobel_DestDepth,
nXOrder := aSobel_DerivOrder[0],
nYOrder := aSobel_DerivOrder[1],
nKernelSize := nSobel_KernelSize,
fScale := fSobel_Scale,
fDelta := fSobel_Delta,
eBorderType := eSobel_BorderExtra,
hrPrev := hr);
// Execute the Scharr Filter
hr := F_VN_ScharrFilterExp(
ipSrcImage := ipImageIn,
ipDestImage := ipImageScharr,
eDestDepth := eScharr_DestDepth,
eFilterDirection:= eScharr_FilterDir,
fScale := fScharr_Scale,
fDelta := fScharr_Delta,
eBorderType := eScharr_BorderExtra,
hrPrev := hr);
// Execute the Laplacian Filter
hr := F_VN_LaplacianFilterExp(
ipSrcImage := ipImageIn,
ipDestImage := ipImageLaplace,
eDestDepth := eLaplace_DestDepth,
nKernelSize := nLaplace_KernelSize,
fScale := fLaplace_Scale,
fDelta := fLaplace_Delta,
eBorderType := eLaplace_BorderExtra,
hrPrev := hr);
Results
The sample project contains the functions calls of the three filters. The parameter configuration and its effect on a sample image can be tested in this sample.
Original image | Sobel filter | Scharr filter | Laplace filter |