OpenCvSharp function: Laplacian edge detection

Laplacian edge detection

Function description

Calculate the Laplacian operator of the image.
The function calculates the Laplacian operator by adding the second derivatives of x and y of the Sobel operator:
when kSize>1, the calculation is as follows

dst = Δ src = ∂ 2 src ∂ x 2 + ∂ 2 src ∂ y 2 \texttt{dst} = \Delta \texttt{src} = \frac{\partial^2 \texttt{src}}{\partial x^2} + \frac{\partial^2 \texttt{src}}{\partial y^2} dst=Δsrc=x22src+y22src

When kSize==1, use the following 3x3 core

[ 0 1 0 1 − 4 1 0 1 0 ] \begin{bmatrix} 0 & 1 & 0 \\ 1 & -4 & 1 \\ 0 & 1 & 0\end{bmatrix} 010141010

function prototype

void Laplacian(InputArray src,
	OutputArray dst,
	MatType ddepth,
	int ksize = 1,
	double scale = 1.0,
	double delta = 0.0,
	BorderTypes borderType = BorderTypes.Reflect101)

Parameter Description

parameter illustrate                            
InputArray src Input image, usually grayscale image
OutputArray dst Output image. The size and number of channels are the same as the input image
MatType ddepth The desired output depth, see the Depth combinations table for details.
int ksize The kernel size used to calculate the second derivative, which is a positive odd number
double scale Optional scaling factor for calculated derivative values, defaults to 1.
double delta Add this value to the calculation result, which can be understood as brightening (positive number) or darkening (negative number)
BorderTypes borderType Method to obtain pixel values ​​outside the border, Wrap is not supported

Image example

Laplacian edge detection

code example

Mat src;
string winName = "Laplacian Demo";


public void Run()
{
    
    
    if (!Utils.SelectFile(out string fileName)) fileName = ImagePath.Lena;
    src = Cv2.ImRead(fileName, ImreadModes.Color);
    if (src.Empty()) throw new Exception($"图像打开有误:{
      
      fileName}");
    //
    Cv2.GaussianBlur(src, src, new Size(3, 3), 0, 0);

    Cv2.CvtColor(src, src, ColorConversionCodes.BGR2GRAY);

    Cv2.NamedWindow(winName, WindowFlags.AutoSize;
    var tbKSizeName = "kSize 2n+1";
    Cv2.CreateTrackbar(tbKSizeName, winName, 15, KSizeOnChanged);
    Cv2.SetTrackbarPos(tbKSizeName, winName, 1);

    var tbScaleName = "scale n/10";
    Cv2.CreateTrackbar(tbScaleName, winName, 100, SCaleOnChanged);
    Cv2.SetTrackbarPos(tbScaleName, winName, 10);

    var tbDeltaName = "Delta n-50";
    Cv2.CreateTrackbar(tbDeltaName, winName, 100, DeltaOnChanged);
    Cv2.SetTrackbarPos(tbDeltaName, winName, 50);

    bool loop = true;
    while (loop)
    {
    
    
        if (Cv2.WaitKey(50) == 27) break;
    }
    Cv2.DestroyAllWindows();
}
private void OnChanged()
{
    
    
    using var dst = new Mat();
    Cv2.Laplacian(src, dst, MatType.CV_16S, kSize, scale, delta, BorderTypes.Default);
    // converting back to CV_8U
    Cv2.ConvertScaleAbs(dst, dst);
    Utils.PutText(dst, $"kSize={
      
      kSize},Scale={
      
      scale},Delta={
      
      delta}");
    Cv2.ImShow(winName, dst);
}

int kSize = 3;
private void KSizeOnChanged(int pos, IntPtr userData)
{
    
    
    kSize = pos * 2 + 1;
    OnChanged();
}

double scale = 1;
private void SCaleOnChanged(int pos, IntPtr userData)
{
    
    
    scale = pos / 10.0D;
    OnChanged();
}

double delta = 0;
private void DeltaOnChanged(int pos, IntPtr userData)
{
    
    
    delta = pos - 50;
    OnChanged();
}

OpenCvSharp function examples (catalogue)
refer to
https://docs.opencv.org/4.7.0/d5/db5/tutorial_laplace_operator.html

Guess you like

Origin blog.csdn.net/TyroneKing/article/details/130187406