OpenCV3 entry (eight) edge detection

1 , edge detection based

Edge image is the basic feature of the image, an edge point is a step change in gray level pixels, i.e., the derivative of gray scale value larger or greatly place, edge detection is one image recognition. Gray with a first-order differential and second-order differential image to enhance the image of the transition, and the edge is gray change places. Thus, these conventional first order differential operator such as Robert, Sobel, prewitt the like, and the like Laplacian second-order differential operators are essentially be used to detect edges. These operators can be called edge detection operator.

Edge detection may greatly reduce the amount of data, excluding irrelevant information that retain the important structural property of the image, in general edge detection step are:

1) filter

Edge detection is a main image based on first and second derivatives, the derivative, are sensitive to noise differential, gradient calculation susceptible to noise, it is necessary to suppress noise by filtering.

2) enhanced

To detect the boundary, it is necessary to determine the neighborhood gray scale, base edge enhanced image is to determine the points of change in intensity value of the neighborhood, using a sharpening projection area gradation change.

3) Detection

After enhanced image, many points of the gradient values ​​in the neighborhood of relatively large, but not all the points are edge points need to choose a method is used, the threshold value is generally used to divide each of the image points.

2 , the edge detection operator

2.1 a first-order differential operator

1) Principle

The edge of gray image is the image occurs rapidly changing place. For f (t), its derivative f '(t) reflects a trend everywhere, the maximum derivative of change in the position at the fastest, sobel operator idea is to find the first derivative of the analog.

among them:

Direction of the gradient is the function f (x, y) the direction of maximum rate of change. Gradient magnitude as the maximum rate of change of the size of the metric values:

Discrete two-dimensional function f (i, j), may be used as a finite difference approximation of gradients.

To simplify the calculation, an absolute value can be approximated.

|▽f(i,j)|= |f(i+1,j)-f(i,j)| +|f(i,j+1)-f(i,j)|

2) Sobel operator

Sobel operator is a discrete differential operator (discrete differentiation operator), is used to calculate the approximate gradient of image intensity, the greater the gradient of the edge is more likely, a set of Sobel and differential Gaussian smoothing derivative, also known as first order differential operators promoter, operator derivative, the derivative in the horizontal and vertical directions, the resulting gradient image is an image in the X and Y directions method.

Prototype:

CV_EXPORTS_W void Sobel( InputArray src, OutputArray dst, int ddepth,
                         int dx, int dy, int ksize = 3,
                         double scale = 1, double delta = 0,
                         int borderType = BORDER_DEFAULT );

Sample code:

img = imread("D:\\WORK\\5.OpenCV\\LeanOpenCV\\pic_src\\pic7.bmp", IMREAD_GRAYSCALE);
imshow ( " artwork " , IMG);

// X-direction gradient of   
the Sobel (IMG, imgX, CV_8U, . 1 , 0 , . 3 , . 1 , . 1 , BORDER_DEFAULT);
convertScaleAbs (imgX, imgX);
imshow("X方向Sobel", imgX);

// the Y direction gradient of   
the Sobel (IMG, IMGY, CV_8U, 0 , . 1 , . 3 , . 1 , . 1 , BORDER_DEFAULT);
convertScaleAbs (imgY, imgY);
imshow("Y方向Sobel", imgY);

// Merge gradient (approximately)   
addWeighted (imgX, 0.5 , IMGY, 0.5 , 0 , IMG2);
imshow ( " the overall direction of the Sobel " , IMG2);

The output is:

2.2 the second-order differential operator

1) Principle

Two-dimensional function f (x, y) defined in the second order differential (Laplacian) are:

Obtained by adding the above equation after a Laplace operator:

Filter template corresponding to the following:

Considering the absolute value calculates a gradient, in response to positive and negative coefficients of the same pattern, the above templates can also be expressed as:

The above template have symmetry, so a filter can be required, not like the calculation of a 2nd order differential above.

2) Application

Laplacian second-order differential operator is sensitive to noise, Laplace operator response to isolate more strongly than the pixel edge or line of response, only suitable for noise-free image. In the presence of noise, the need to make use of low-pass filtering prior to edge detection Laplacian operator. Gauss - Laplacian, also called LoG operator, is to complement this defect is created, it first Gaussian low-pass filtering, then second order differential Laplace sharpening.

Examples are as follows.

img = imread("D:\\WORK\\5.OpenCV\\LeanOpenCV\\pic_src\\pic7.bmp", IMREAD_GRAYSCALE);
imshow ( " artwork " , IMG);
GaussianBlur(img, img2, Size(5, 5), 0, 0);
imshow("高斯图", img2);
Laplacian(img2, img3, CV_8U, 3, 1, 0);
imshow("Laplacian图", img3);

输出结果为:

2.3 Canny算子

1)原理

在图像边缘检测中,抑制噪声和边缘精准定位是无法同时满足的,一些边缘检测算法通过平滑滤波去除噪声的同时,也增加了边缘检测的不确定性,而提高边缘检测算子对边缘的敏感性的同时,也提高了对噪声的敏感性。Canny算子力图在抗噪声干扰和精准定位之间寻求最佳折中方案。

Canny算法主要有4个步骤:

  • 用高斯滤波器来平滑图像;
  • 用一介偏导的有限差分来计算梯度的幅值和方向;
  • 对梯度进行非极大值抑制,保留极大值,抑制其他值;
  • 用双阈值算法检测和连接边缘。

2)应用

函数原型为:

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

示例如下:

img = imread("D:\\WORK\\5.OpenCV\\LeanOpenCV\\pic_src\\pic7.bmp", IMREAD_GRAYSCALE);
imshow("原图", img);
Canny(img, img2, 3, 9, 3);
imshow("canny", img2);

输出效果如下图。

修改阈值之后,Canny(img, img2, 45, 90, 3);效果如下图。

3、参考文献

1、《OpenCV3 编程入门》,电子工业出版社,毛星雨著

2、《学习OpenCV》,清华大学出版社,Gary Bradski Adrian kaehler

3Sobel边缘检测

https://www.cnblogs.com/yibeimingyue/p/10878514.html

4、学习笔记-canny边缘检测

https://www.cnblgs.com/mmmmc/p/10524640.html

 

个人博客,转载请注明。

https://www.cnblogs.com/pingwen/p/12324348.html

Guess you like

Origin www.cnblogs.com/pingwen/p/12324348.html