Canny边缘检测
1、Canny边缘检测
Canny是一种很好的边缘检测器,也是一种常见的图像处理算法。
实现Canny边缘检测的步骤:
- 1、高斯模糊
- 2、转灰度
- 3、计算梯度(Sobel/Scharr)
- 4、非最大信号抑制
- 5、高低阈值输出二值图
高低阈值的输出:
Canny边缘检测的api:
2、代码实现
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int element = 5;
int max_size = 50;
Mat img;
void CannyFunc(int, void*);
int main(int argc, char**)
{
img = imread("G:/testpic/img.png");
if (img.empty())
{
printf("cannot load the image");
return -1;
}
namedWindow("input", WINDOW_AUTOSIZE);
imshow("input", img);
namedWindow("output", WINDOW_AUTOSIZE);
createTrackbar("num:", "output", &element, max_size, CannyFunc);
CannyFunc(0,0);
waitKey(0);
return 0;
}
void CannyFunc(int, void*)
{
Mat img_blur, gray, dst;
GaussianBlur(img, img_blur, Size(3,3), 0, 0);
imshow("blur", img_blur);
cvtColor(img_blur, gray, COLOR_BGR2GRAY);
imshow("gray", gray);
Canny(gray, dst, element, element*2, 3, flase);
imshow("dst", dst);
}
原图:
canny: