Edge detection operators: Canny,Sobel,Laplacian

opencv implements some functions of edge detection algorithm, which is Canny(),Sobel(),Laplacian(); Edge operator is a very important method in image recognition and detection. When people recognize objects, they also rely on the edge contour, because in general, the edge features are not affected by the light; the same image only has the edge, the pixel amount will be greatly reduced, which is beneficial for processing.

As for the algorithm and principle of operators, you can find professional materials to study, most of them are based on the image gradient characteristics to find the edge;

If you don't say much, look at the code below:

////////////////////////////////
//Image edge detection
int threshval = 60;//Initialization threshold
static void on_trackbar(int, void*)//Callback function of track bar
{
    Mat srcImg = imread("D:/ImageTest/sudoku.png");
    Mat dstImg;
    Canny(srcImg,//input image 
          dstImg,//Output image
          threshval,//First threshold
          threshval*3,//Second threshold
          3);//1,3,5,7, Sobel operator kernel size
    // The small threshold between threshold 1 and threshold 2 is used to control edge connection, and the large threshold is used to control the initial segmentation of strong edge.
    imshow("Canny", dstImg);
}

void imgEdge()//Image edge detection
{
    Mat srcImg = imread("D:/ImageTest/sudoku.png");
    ///Canny edge detection
    //    Mat dstImg;
    //    Canny(srcImg, / / input image
    //          dstImg, / / output image
    //          10, / / first threshold
    //          80, / / second threshold
    //          3) ; / / 1,3,5,7, Sobel operator kernel size
    //    //The small threshold between threshold 1 and threshold 2 is used to control edge connection, and the large threshold is used to control the initial segmentation of strong edge. ;
    namedWindow("Canny", 1 ); //Create processing window
    createTrackbar(
                "Threshold", //const char* trackbarName, slider name
                "Canny",//const char* windowName. The name corresponds to the window name. Otherwise, it will not be displayed
                &threshval,//int* value, initial value of slider when displayed
                100, //int count, Max
                on_trackbar//CvTrackbarCallback onChange, trigger function when sliding
                );//Create track bar
    on_trackbar(threshval, 0);

    /////Sobel edge detection
    Mat grad_x, grad_y;
    Mat abs_grad_x, abs_grad_y, dst;
    //Because the result of Sobel is positive and negative, and the 8-bit unsigned representation is incomplete, the 16 bit signed representation is used
    Sobel(srcImg,   //input image 
          grad_x,     //Output image
          CV_16S,    //Output image depth
          1, //Difference in x direction
          0, //The difference in y direction, where the image in x direction is needed, so the image in y direction is 0;
          3, //ksize core size, default 3, can only take 1,3,5,7;
          1, //Zoom scale defaults to 1, indicating no zoom
          1, //delta value, default 0;
          BORDER_DEFAULT);//Boundary mode
    convertScaleAbs(grad_x, abs_grad_x);//Convert 16 bit signed to 8-bit unsigned
    Sobel(srcImg, grad_y, CV_16S, 0, 1, 3, 1, 1, BORDER_DEFAULT);
    convertScaleAbs(grad_y, abs_grad_y);
    addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, dst);
    //The size of the extended Sobel core must be 1, 3, 5 or 7. In addition to size 1, in other cases, the aperture ﹐ size × aperture ﹐ size separable kernel will be used to calculate the difference.

    ///Laplacian edge detection
    Mat dst2;
    Mat abs_dst;
    Laplacian(srcImg, //input image 
              dst2,          //Output image
              CV_16S,     //Output image depth
              3,              //Ksize, filter aperture size, must be integer odd;
              1,             //Scaling
              0,             //delta value
              BORDER_DEFAULT);//Boundary mode
    convertScaleAbs(dst2, abs_dst);

    imshow("src", srcImg);
    //    imshow("Canny", dstImg);
    imshow("Sobel X direction", abs_grad_x);
    imshow("Sobel Y direction", abs_grad_y);
    imshow("Sobel combine", dst);
    imshow("Laplacian", abs_dst);
    waitKey(0);
}
The results are as follows:



Tags: OpenCV

Posted on Thu, 30 Apr 2020 20:42:31 -0700 by joecooper