Opencv(C++) learning series---Canny edge detection algorithm

Table of contents

【1】Edge detection algorithm process

【2】Introduction to Canny operator

【3】Complete code


【1】Edge detection algorithm process

  1. Use Gaussian filter to smooth the image to filter out noise. (noise reduction)
  2. Calculate the gradient size and direction of each pixel in the image (finding the gradient is consistent with the sobel algorithm for finding the gradient)
  3. Use non-maximum suppression to eliminate the adverse effects of edge detection (non-maximum suppression)
  4. Determine real and potential edges using double-thread monitoring (dual-thread detection)
  5. Edge detection is accomplished by suppressing isolated weak edges (weak edge suppression)   

For the specific algorithm process, please refer to this article:

https://zhuanlan.zhihu.com/p/494567705

【2】Introduction to Canny operator

void Canny( InputArray image, OutputArray edges,
                         double threshold1, double threshold2,
                         int apertureSize = 3, bool L2gradient = false );

 Specific parameter introduction:

The first parameter is an image of the InputArray type. The input image, that is, the source image, just fill in the object of the Mat class. It can be a three-channel color image or a single-channel grayscale image.
The second parameter, the edges of the OutputArray type, is the output edge map, which is a single-channel edge image.
The third parameter, threshold1 of double type, is the first hysteresis threshold.
The fourth parameter, threshold2 of the double type, is the second hysteresis threshold.

Canny uses two thresholds. According to the grayscale image of the edge response, the one greater than the high threshold is a strong edge, and the one less than the low threshold is a weak edge. The value between the strong and weak edges uses a search algorithm. Whether there is a strong edge in the 8 areas, if there is a strong edge, the point can be set as a strong edge.

The fifth parameter, apertureSize of type int, represents the aperture size for applying the Sobel operator, which has a default value of 3.
The sixth parameter, L2gradient of bool type, is an identifier for calculating the gradient amplitude of the image. It has a default value of false.

【3】Complete code

#include<opencv2\opencv.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include<opencv2\highgui\highgui.hpp>

using namespace cv;
using namespace std;

int main(int argc,char** argv)
{
	//载入原始图
	Mat src = imread("E:\\乔大花进度\\11-28\\边缘检测\\2.jpg");
	Mat src1 = src.clone();
	Mat src2(src.size(),CV_8UC1);
	//第一种直接使用边缘检测canny算法
	//blur(src2,src2,Size(3,3));
	Canny(src,src2,150,180);
	imshow("边缘检测1",src2);

	//第二种高阶边缘检测算法,转为灰度图,降噪,用Canny得到最后的边缘作为掩码,转到原图得到效果图,得到色彩的边缘图
	Mat dst, edge, gray;

	//【1】创建与src同类型和大小的矩阵(dst)
	dst.create(src1.size(),src1.type());
	//【2】将彩色图转为灰度图
	cvtColor(src1,gray,COLOR_BGR2GRAY);
	//【3】先使用3x3的内核用来降噪
	blur(gray,edge,Size(3,3));
	//【4】运行canny算法
	Canny(edge, edge, 3, 9);
	//【5】将dst中的像素都置为0
	dst = Scalar::all(0);
	//【6】与edge为掩码,将src2复制到dst中
	src1.copyTo(dst,edge);

	//【7】显示效果图
	imshow("边缘检测2",dst);

	//显示原始图
	imshow("原图",src);

	waitKey(0);
	system("pause");
	destroyAllWindows();

	return 0;

}

operation result:

0971b4d0603c4555bcf677aa2731bae0.png

Guess you like

Origin blog.csdn.net/qiaodahua/article/details/128078825