目录
[1. 简述](#1. 简述)
[2. Sobel 边缘检测](#2. Sobel 边缘检测)
[3. Canny 边缘检测](#3. Canny 边缘检测)
1**.** 简述
边缘的特征在于像素强度的突变。为了检测边缘,我们需要在相邻像素中寻找这种变化。OpenCV 提供了两种重要的边缘检测算法:Sobel 边缘检测和 Canny 边缘检测。
2. Sobel 边缘检测
Sobel 边缘检测是应用最广泛的边缘检测算法之一。Sobel 算子用于检测由像素强度突变所形成的边缘。
示例代码:
cpp
void Test_Sobel()
{
cv::Mat src = cv::imread("D:\\TestVideo\\guiyang_east_station.jpg");
// Sobel edge detection
Mat sobelx, sobely, sobelxy;
Sobel(src, sobelx, CV_64F, 1, 0, 5);
Sobel(src, sobely, CV_64F, 0, 1, 5);
Sobel(src, sobelxy, CV_64F, 1, 1, 5);
// Display Sobel edge detection images
imshow("original image", src);
imshow("Sobel X", sobelx);
imshow("Sobel Y", sobely);
imshow("Sobel XY using Sobel() function", sobelxy);
waitKey(0);
cv::destroyAllWindows();
}
调用结果:
原图:

垂直核,X 方向增强:

水平核,Y 方向增强:

双向增强:

3. Canny 边缘检测
Canny 边缘检测是当今最流行的边缘检测方法之一,因其具有极强的健壮性和灵活性而备受青睐。该算法本身包含从图像中提取边缘的三个阶段;若加上旨在降低噪声的必要预处理步骤------图像模糊,则构成了一个包含以下四个阶段的过程:
(1) 降噪
(2) 计算图像的强度梯度
(3) 抑制伪边缘
(4) 滞后阈值处理
示例代码:
cpp
void Test_Canny()
{
cv::Mat src = cv::imread("D:\\TestVideo\\guiyang_east_station.jpg");
Mat dst;
Canny(src, dst, 100, 200, 3, false);
imshow("Canny edge detection", dst);
waitKey(0);
cv::destroyAllWindows();
}
运行结果:
