OpenCV learning basic image operations (11): Canny edge detection

Original link: http://www.cse.iitd.ac.in/~pkalra/csl783/canny1986.pdf

Introduction

The classic Canny edge detection algorithm usually starts with Gaussian blur and ends with edge connection based on dual thresholds. However, in actual engineering applications, considering that the input images are all color images, the images after the final edge connection must be binarized for output and display, so the complete Canny edge detection algorithm implementation steps are as follows:

  • 1. Convert color image to grayscale image
  • 2. Gaussian blur the image
  • 3. Calculate the image gradient, calculate the edge amplitude and angle of the image according to the gradient
  • 4. Non-maximum signal suppression processing (edge ​​refinement)
  • 5. Double threshold edge connection processing
  • 6. Binarized image output results

Detailed algorithm

step1: Gaussian smoothing filter

  Filtering is to remove noise. Gaussian filtering is also chosen because among many noise filters, Gaussian performs best (how is the performance defined? How good is it best?), you can also try other filters such as mean filtering, medium Value filtering and so on. The generating equation of a Gaussian filter kernel with a size of (2k+1)x(2k+1) (the kernel is generally of odd size) is given by:

   

  The following is an example of a Gaussian convolution kernel with sigma = 1.4 and a size of 3x3. Note that the sum of the matrix is ​​1 (normalized):

  For example: if a 3x3 window in the image is A and the pixel to be filtered is e, after Gaussian filtering, the brightness of pixel e is:

   Among them * is the convolution symbol, sum represents the sum of all elements in the matrix. Simply put, each pixel value after filtering = the weighted sum of its original pixel center value and its neighboring pixels. Image convolution is a very important and widely used operation in image processing and must be understood well.

The size of the Gaussian convolution kernel will affect the performance of the Canny detector. The larger the size, the stronger the denoising ability, so the less noise, but the more blurred the picture, the stronger the anti-noise ability of the canny detection algorithm, but the side effects of blur will also lead to low positioning accuracy. Generally, the recommended size is 5*5 , 3*3 is also OK.

step2: Calculate the gradient strength and direction

The most important feature of the edge is the dramatic change in gray value. If the gray value is regarded as a binary function value, then the change in gray value can be described by the "derivative" (or called gradient) of the binary function. Since the image is discrete data, the derivative can be represented by a difference value. In actual engineering, the difference is the gray level difference, which means the difference between two pixels. A pixel has 8 neighborhoods, so it is divided up and down, left and right diagonally, so the Canny algorithm uses four operators to detect horizontal, vertical and diagonal edges in the image. The operator calculates the gradient in the form of image convolution, such as Roberts, Prewitt, Sobel, etc. Here, the Sobel operator is used to calculate the difference between the x-axis and the y-axis of the two-dimensional image (the origin of these numbers?), the following The two templates are convolved with the original image to obtain the difference value map of the x and y axes, and finally the gradient G and direction θ of the point are calculated

  The calculation of the gradient and direction belongs to the content of advanced mathematics. If you don’t understand, you should make up the basic mathematics. This concept is often used in image processing.

step3: non-maximum suppression

The edge detected by the sobel operator is too thick, and we need to suppress the pixels whose gradient is not large enough, and only keep the largest gradient, so as to achieve the purpose of thin edges . These pixels with insufficient gradients are likely to be transition points of a certain edge. According to the definition of the maximum value of the binary function on high numbers, that is, for all (x, y) in a certain neighborhood of the point (x0, y0), f (x, y) ≤ (f (x0, y0), It is said that f has a maximum value in (x0, y0), and the maximum value is f(x0, y0). The simple solution is to determine which of the 8 neighborhoods and the center pixel of a pixel is larger, but this is easy to filter out Noise, so we need to use gradient and gradient direction to help determine.

      As shown in the figure below, the gradient direction of the central pixel C is a blue straight line, so just compare the central point C with the size of dTmp1 and dTmp2. Since the pixels of these two points are not known, assuming that the pixel changes are continuous, we can use g1, g2 and g3, g4 for linear interpolation estimation. Suppose the amplitude of g1 is M(g1) and the amplitude of g2 is M(g2), then M(dtmp1)=w*M(g2)+(1-w)*M(g1), where w=distance(dtmp1, g2)/distance(g1,g2). That is, the distance from g1 and g2 to dTmp1 is used as the weight to estimate the value of dTmp1. w can be expressed as tan (θ) in the program, and it is divided into four cases (the right picture below) for discussion.

step4: Use dual threshold algorithm to detect and connect edges

    The dual-threshold method is very simple. We assume two types of edges: among the edge points after non-maximum suppression, those whose gradient value exceeds T1 are called strong edges, those whose gradient values ​​are less than T1 and greater than T2 are called weak edges, and those whose gradient is less than T2 Not the edge . To be sure, strong edges must be edge points, so T1 must be set high enough to require that the gradient value of the pixel point is large enough (changes are sharp enough), and weak edges may be edges or noise. How to judge What? When there are strong edge points in the surrounding 8 neighborhoods of the weak edge, the weak edge point is turned into a strong edge point to supplement the strong edge. In practice, people find that the ratio of T1:T2=2:1 is better. T1 can be specified manually, or you can design an algorithm to specify it adaptively. For example, define the first 30% of the gradient histogram as T1, which I realized It is an artificially specified threshold. The method of checking 8 neighborhoods is called edge lag tracking, and the method of connecting edges also includes area growing method and so on.

API introduction

Canny(gray_src, dst, threshold1, threshold2, 3, false);
//gray_src:输入灰度图像
//dst:输出边缘图像
//threshold1:强边缘阈值,即梯度大于该值的点都保留
//threshold2:若边缘阈值,即梯度小于该值的点都舍弃,梯度大于该值且小于强边缘阈值的点向强边缘连通
//3  : 梯度算子的大小,默认使用为sobel
//false: x与y方向梯度融合是否使用L2范数

Code practice

#include <iostream>
#include <math.h>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
Mat src,dst, gray_src;
int threshold1 = 125;
int threshold2 = 225;
char OUTPUT[] = "OUTPUT_WINDOS";
int max_threshold = 255;
void CallBack_Threshold(int, void*);

int main(int argc, char* argv[])
{
	//src = imread("src.jpg");
	Mat src = imread("cat.png");
	if (!src.data)
	{
		cout << "cannot open image" << endl;
		return -1;
	}

	Mat gray_src;
	cvtColor(src, gray_src, COLOR_RGB2GRAY);
	GaussianBlur(gray_src, gray_src, Size(3, 3), 0, 0);
	namedWindow("OUTPUT_WINDOS", WINDOW_AUTOSIZE);

	while (1)
	{
		cout << " 输入两个阈值:" << endl;
		cin >> threshold1 >> threshold2;
		if (threshold1 > max_threshold)threshold1 = max_threshold;
		if (threshold2 > threshold1)threshold2 = threshold1;
		Canny(gray_src, dst, threshold1, threshold2, 3, false);
		imshow("OUTPUT_WINDOS", dst);
		waitKey(10);
	}
	

	
	return 0;
}


Guess you like

Origin blog.csdn.net/fan1102958151/article/details/107357875