[C++] [Opencv] cv::Canny() edge detection function detailed explanation and examples

Canny edge detection is a popular edge detection algorithm developed by John F. Canny in 1986. It is a multi-stage process that includes noise filtering, computing gradients of image intensity, non-maximum suppression, and dual-threshold detection. This article explains the cv::Canny() function in detail through function prototype interpretation and examples to help everyone understand and use it.

principle

The steps of Canny edge detection are as follows:

(1) Gaussian filter (noise filter): Use Gaussian filter to smooth the image to reduce noise. Gaussian filter is a linear filter that removes high-frequency noise from images.
(2) Calculate gradient intensity and direction: Calculate the gradient intensity and direction of each pixel in the image. The gradient strength represents the edge strength at the pixel point, while the gradient direction represents the direction of the edge.
(3) Non-maximum suppression: After calculating the gradient strength and direction, non-maximum suppression will suppress those pixels that are not local maxima. This means that only pixels with local maximum values ​​will be retained.
(4) Double threshold detection: Finally, double threshold detection is used to detect edges. This requires two thresholds, often called a low threshold and a high threshold. If the gradient strength of a pixel is greater than the high threshold, the pixel is considered an edge; if the gradient strength of the pixel is between the two thresholds, the pixel is considered an edge candidate; if the gradient strength of the pixel is lower than the low threshold, the pixel is considered an edge candidate. Pixels are considered non-edge.

Function introduction

void cv::Canny(InputArray image, OutputArray edges, double lowThreshold, double highThreshold, int apertureSize = 3);

参数解释:

image:输入图像,应该是灰度图像。
edges:输出图像,即检测到的边缘图像。
lowThreshold:低阈值,用于双阈值检测。
highThreshold:高阈值,用于双阈值检测。
apertureSize:指定Sobel算子的大小,默认为3。

Run the example

Set the thresholds to 50 and 150 respectively.
The code is as follows:

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main() {
    
    
    Mat image = imread("ceshi.jpg", IMREAD_COLOR); // 读取输入图像
    if (image.empty()) {
    
    
        cout << "Failed to read image." << endl;
        return -1;
    }
    Mat gray_image; cvtColor(image, gray_image, COLOR_BGR2GRAY); // 转换为灰度图像
    Mat edges_image;

    // 应用Canny边缘检测算法
    Canny(gray_image, edges_image, 50, 150);

    // 显示结果图像
    imshow("Input", image);
    imshow("Edges", edges_image);
    imwrite("cnany.jpg", edges_image);
    waitKey(0);
    return 0;
}

In the above example, we first read the input image and convert it to grayscale image. We then apply the Canny edge detection algorithm using the Canny function and specify two thresholds (low threshold and high threshold). Finally, we display the original image and the detected edge image. Image comparison is shown below.

Insert image description here
The upper part is the original image, and the lower part is the edge detection effect image.
Insert image description here

summary

When choosing to use the Canny function for edge calculation, you should set appropriate low and high threshold parameters based on project needs and scenarios to obtain the best edge detection results. A lower threshold may result in more edges being detected, while a higher threshold may result in fewer edges being detected. Therefore, choosing an appropriate threshold is one of the keys to using the Canny edge detection function.

Guess you like

Origin blog.csdn.net/qq_22734027/article/details/134491214