函数参数: InputArray image 输入图片,该图片为单通道图片
OutputArray edges 输出图片 也是单通道图
double threshold1, double threshold2, 取值范围
int apertureSize Sobel 算子内核大小
bool L2gradient 采用更精确的方式计算图像梯度
void Canny( InputArray image, OutputArray edges,
double threshold1, double threshold2,
int apertureSize = 3, bool L2gradient = false );
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main(void) {
Mat img_brg, img_gry,img_cny;
namedWindow("BGR", WINDOW_AUTOSIZE);
namedWindow("GRAY", WINDOW_AUTOSIZE);
img_brg = imread("C:/Users/Lenovo/Desktop/1.png"); //读取图片
imshow("BGR", img_brg); //输出BGR图片
cvtColor(img_brg, img_gry, COLOR_BGR2GRAY); //BGR图片灰度化
imshow("GRAY", img_gry); //输出灰度图
Canny(img_gry, img_cny, 10, 100, 3, true); //Canny边缘检测
namedWindow("CANNY", WINDOW_AUTOSIZE);
imshow("CANNY", img_cny); //输出边缘化后的图片
waitKey(0);
return 0;
}
关于2个阈值参数:
- 低于阈值1的像素点会被认为不是边缘;
- 高于阈值2的像素点会被认为是边缘;
- 在阈值1和阈值2之间的像素点,若与第2步得到的边缘像素点相邻,则被认为是边缘,否则被认为不是边缘。