OpenCV (26): Edge detection (2)

Table of contents

1. Laplacian operator edge detection

principle:

Laplacian edge detection function Laplacian()

Sample code:

2. Canny operator edge detection

principle:

Canny algorithm function Canny()

Sample code:


1. Laplacian operator edge detection

principle:

        The principle of the Laplacian operator is based on the second derivative of the image. The first derivative helps us to detect the edges in the image while the second derivative can detect the intersections of the edges i.e. the corners in the image.

The Laplacian operator is defined as the sum of the second-order spatial derivatives and is used for image processing in two-dimensional cases. Its discrete form is as follows:

L(x, y) = d^2(I(x, y)) / dx^2 + d^2(I(x, y)) / dy^2 

Among them, I(x, y) represents the pixel value in the image, d^2 represents the partial derivative, dx represents the derivative in the X direction, and dy represents the derivative in the Y direction.

The Laplacian operator can be computed by applying discrete convolution. A common discrete Laplacian operator template is as follows:

 
 

By convolving this template with the image, we can compute the Laplacian response for each pixel in the image.

Specific steps are as follows:

  1. Convert the image to grayscale (if not grayscale).
  2. A discrete template for applying the Laplacian operator to an image.
  3. Threshold the convolution results to extract edge information.
  4. Post-processing operations such as non-maximum suppression and edge connection are optionally performed on the thresholded edge image.

The output of the Laplacian operator is a high-frequency component image representing the edge. Edges typically appear as boundaries between bright and dark pixels. The width and intensity of the edges depend on the size of the Laplacian operator and the grayscale variation in the image.

Laplacian edge detection function Laplacian()

void cv::Laplacian ( InputArray src,

OutputArray dst,

int        ddepth,

int     ksize = 1,

double   scale = 1,

double    delta =0,

int     borderType = BORDER DEFAULT

)

  • src: Input the original image, which can be a grayscale image or a color image.
  • dst: The output image, which has the same dimensions and number of channels as the input image src.
  • ddepth: The data type (depth) of the output image, which has different value ranges depending on the data type of the input image.
  • ksize: The size of the filter, which must be a positive odd number.
  • scale: scaling factor to scale the derivative calculation result, the default coefficient is 1, no scaling is performed.
  • delta: Bias value, add the bias value to the calculation result.
  • borderType: Pixel extrapolation option flag.
Sample code:
void Laplacian_f(Mat image){
    Mat gray;
    cvtColor(image,gray,COLOR_BGR2GRAY);
    Mat result,result_g,result_G;
    //未滤波提取边缘
    Laplacian(gray,result,CV_16S,3,1,0);
    convertScaleAbs(result,result);
    //滤波后提取边缘
    GaussianBlur(gray,result_g,Size(3,3),5,0);//高斯滤波
    Laplacian(result_g,result_G,CV_16S,3,1,0);
    convertScaleAbs(result_G,result_G);
    //显示图像
    imwrite("/sdcard/DCIM/result.png",result);
    imwrite("/sdcard/DCIM/result_G.png",result_G);
}

                   

  (Extract edge image without filtering) (Extract edge image after filtering)

2. Canny operator edge detection

principle:

Canny algorithm function Canny()

void cv::Canny ( InputArray image,

OutputArray edges,

double  threshold1,

double   threshold2,

int  apertureSize = 3,

bool   L2gradient = false

)

  • image: Input image, must be CV 8U single-channel or three-channel image
  • edges: The output image, a single-channel image with the same size as the input image, and the data type is CV 8U.
  • threshold1: the first hysteresis threshold
  • threshold2: the second hysteresis threshold
  • apertureSize: the diameter of the Sobel operator
  • L2gradient: a sign to calculate the gradient magnitude of the image
Sample code:
void Canny_f(Mat image){
    Mat gray;
    cvtColor(image,gray,COLOR_BGR2GRAY);
    Mat resultHigh,resultLow,resultG;
    //大阈值检测图像边缘
    Canny(image,resultHigh,100,200,3);
    //小阈值检测图像边缘
    Canny(image,resultLow,20,40,3);
    //高斯模糊后检测图像边缘
    GaussianBlur(gray,resultG,Size(3,3),5);
    Canny(resultG,resultG,100,200,3);
    //显示图像
    imwrite("/sdcard/DCIM/resultHigh.png",resultHigh);
    imwrite("/sdcard/DCIM/resultLow.png",resultLow);
    imwrite("/sdcard/DCIM/resultG.png",resultG);
}

(Large threshold detects image edge) (Small threshold detects image edge) (Detects image edge after Gaussian blur)

Guess you like

Origin blog.csdn.net/weixin_63357306/article/details/132735357