Edge Detection

Get introduced to different types of edge detection, and learn how to detect edges using these techniques.

One of the most frequent uses of OpenCV is the detection of shapes, edges, faces, and various other components in images. In this chapter, we’ll learn about some of these detections.

Detecting edges helps us solve many practical problems. It helps us detect shapes, documents, and various other components of the image. It can be used for fingerprint recognition, medical imaging, and also vehicle detection. Here, we’ll discuss two different methods of detecting edges:

  • Canny edge detection
  • Sobel edge detection

First, we need to convert our image to grayscale. Converting the image to grayscale is really important because we use the edge detection algorithm to detect edges, and grayscale images help the algorithm find the edges properly. It helps in simplifying algorithms and eliminates the complexity of the computational requirements.

cv::cvtColor(img, src_gray, COLOR_BGR2GRAY);

Canny edge detection

For Canny edge detection, we use the Canny() function of the OpenCV library. This function requires four parameters, which are listed below:

  • The first parameter is the input image.
  • The second parameter is the output image.
  • The third parameter is the minimum value of the intensity gradient. The edges with an intensity gradient lower than the minimum value are not considered edges. So, increasing the minimum value reduces the number of edges.
  • The fourth parameter is the maximum value of the intensity gradient. The edges with an intensity gradient more than the maximum value are considered edges.

The values that lie between these two thresholds are classified as edges or non-edges based on their connectivity.

cv::Canny(src_gray, canny_img, 150, 150);

The Canny edge detection algorithm goes through four main stages:

  1. First, it uses Gaussian blur to reduce noise.
  2. Then, it finds the intensity gradient of the image.
  3. It goes through non-maximum suppression where the image is scanned, and it removes the unwanted pixels.
  4. The final step is hysteresis thresholding. Here, the algorithm decides whether the edges detected are edges, based on the minimum and maximum values of the intensities provided as parameters.

#include <opencv2/opencv.hpp>

int main() 
{
    //Reading image
    std::string path = "/usercode/photo.jpg";
    cv::Mat img = cv::imread(path);
    cv::Mat src_gray;
    cv::cvtColor(img, src_gray, cv::COLOR_BGR2GRAY);

    //Canny edge detection
    cv::Mat canny_img;
    cv::Canny(src_gray, canny_img, 150, 150);
    cv::imshow("Canny Image", canny_img);
    cv::waitKey(0);

    return 0;
}
Canny edge detection

Look at the pictures below, and compare your output.

Sobel edge detection

For Sobel edge detection, we use the Sobel() function of the OpenCV library. The algorithm of Sobel edge detection is based on the fact that at the edges, there’s a drastic change in the pixel intensity. We can use the first derivative to observe this change. A higher change in the gradient suggests a major change in the image. The algorithm decides those points are edges. It also uses Gaussian smoothing for noise reduction.

cv::Sobel(src_gray, outputImg, CV_64F, xGradient, yGradient);

The Sobel() function requires five parameters, which are listed below:

  • The first parameter is the input image.
  • The second parameter is the output image.
  • The third parameter is the data depth. For this, we use CV_64F, which means that the NumPy array dtype is float64 (64-bit floating-point).
  • The fourth parameter is the gradient’s value in the x-direction.
  • The fifth parameter is the gradient’s value in the y-direction.

Look at the code below. Since we’re displaying three different pictures, press a key on the keyboard to look at the next image.

#include <opencv2/opencv.hpp>

int main() 
{
    //Reading image
    std::string path = "/usercode/photo.jpg";
    cv::Mat img = cv::imread(path);
    cv::Mat src_gray;
    cv::cvtColor(img, src_gray, cv::COLOR_BGR2GRAY);

    // Sobel edge detection
    int kernel_size = 3;
    cv::Mat sobelx, sobely, sobelxy;
    cv::Sobel(src_gray, sobelx, CV_64F, 1, 0, kernel_size);
    cv::Sobel(src_gray, sobely, CV_64F, 0, 1, kernel_size);
    cv::Sobel(src_gray, sobelxy, CV_64F, 1, 1, kernel_size);

    // Display Sobel edge detection images
    cv::imshow("Original Image", img);
    cv::waitKey(0);
    cv::imshow("SobelX", sobelx);
    cv::waitKey(0);
    cv::imshow("SobelY", sobely);
    cv::waitKey(0);
    cv::imshow("SobelXY", sobelxy);
    cv::waitKey(0);

    return 0;
}
Sobel edge detection

Look at the pictures below, and compare your output.

Congratulations! We’ve successfully detected edges using two different techniques.