"Furnace-Refining AI" machine learning 046-image edge detection method

Source: Internet
Author: User

"Furnace-Refining AI" machine learning 046-image edge detection method

(Python libraries and version numbers used in this article: Python 3.6, Numpy 1.14, Scikit-learn 0.19, matplotlib 2.2)

Image in the detection of various shapes in the field of computer vision is one of the most common technology, especially in the image of straight line detection, round detection, Image edge detection, and so on, let us look at how to quickly detect the edge of the image.

The edge is the dividing line of different regions, a set of pixels that have a significant change in the surrounding (local) pixels, with a magnitude and direction of two attributes. This is not an absolute definition, mainly remembering that the edges are local features and the surrounding pixels change significantly to produce edges.

Common edge detection operators: Roberts, Sobel, Prewitt, Laplacian, Log/marr, Canny, Kirsch, Nevitia


1. Sobel operator

Sobel operator is one of the most important operators in image edge detection, which plays a pivotal role in machine learning, and technically, it is a discrete first order difference operator, which is used to calculate the approximate value of one step degree of image luminance function. Using this operator at any point in the image, you will get a gradient vector or a normal vector for that point.

In the calculation formula, the Sobel operator consists of two sets of 3*3 matrices, transverse and longitudinal respectively, and the two matrices are made to the plane convolution of the image, and the luminance difference approximation of the transverse and longitudinal values can be obtained respectively. So these two operators, one is to detect the horizontal edge, and the other is to detect the vertical edge, compared with the Prewitt operator, the Sobel operator on the position of the pixel is weighted, can reduce the edge blur degree, so the effect is better.

The advantage of the Sobel operator algorithm is that the calculation is simple, the speed is fast, but because only the two-direction template is used to detect the horizontal and vertical edges, the edge detection effect of the algorithm is not ideal for the image with complex texture. The algorithm thinks that: where the new gray value is greater than or equal to the threshold of pixels is the edge point, this judgment is not reasonable, will cause the edge of the false, because many noise points of gray value is also very large.

import cv2image=cv2.imread(‘E:\PyProjects\DataSet\FireAI/chair.jpg‘)# Sobel 算子进行图像边缘检测sobel_h=cv2.Sobel(image,cv2.CV_64F,1,0,ksize=3)sobel_v=cv2.Sobel(image,cv2.CV_64F,0,1,ksize=3)plt.figure(13,figsize=(15,30))plt.subplot(131)plt.imshow(image,cmap=‘gray‘) # 彩色图像显示异常,plt采用RGB模式,而cv2采用BGR模式plt.title(‘raw_img‘)plt.subplot(132)plt.imshow(sobel_h,cmap=‘gray‘)plt.title(‘sobel_h‘)plt.subplot(133)plt.imshow(sobel_v,cmap=‘gray‘)plt.title(‘sobel_v‘)

Where the Sobel operator needs to be optimized, there may be only ksize one parameter. Ksize can only go to 1,3,5,7 these four numbers.


2. Laplacian operator

The Laplacian operator is a second-order differential operator in n-dimensional Euclidean space, defined as the divergence div of the gradient grad. The calculation and principle of this operator are not explained here, only the use method and effect are explained.

# Laplacian算子进行图像边缘检测lap=cv2.Laplacian(image, cv2.CV_64F)plt.figure(12,figsize=(10,30))plt.subplot(121)plt.imshow(image,cmap=‘gray‘) # 彩色图像显示异常,plt采用RGB模式,而cv2采用BGR模式plt.title(‘raw_img‘)plt.subplot(122)plt.imshow(lap,cmap=‘gray‘)plt.title(‘Laplacian‘)


3. Canny operator

The goal of canny is to find an optimal edge detection algorithm, with the meaning of the most edge detection:

1, good detection: The algorithm can identify as many of the actual edges in the image as possible.

2, good positioning: Identify the edges to be as close as possible to the actual edges in the actual image.

3, Minimum: the edges in the image can only be identified once, and the image noise that may exist should not be identified as edges

To meet these requirements, canny uses the Variational method, which is a method of finding functions that satisfy a particular function, and the optimal detection uses four exponential function terms and representations, but it is very similar to the first derivative of the Gaussian function.

The steps of the canny algorithm can be divided into: noise reduction, looking for gradients, tracking edges. Noise reduction is a convolution of the original image and the Gaussian smoothing template, resulting in a slightly blurred image compared to the original image, which is done in such a way that the individual pixel noise becomes almost unaffected by the Gaussian smoothing process.

Looking for gradients: The canny operator uses 4 mask detection levels, vertical and diagonal edges, and the original image is stored with each mask's convolution. For each point we identify the maximum value at this point and the resulting edge direction. This allows us to generate the luminance gradient of each point in the image and the direction of the luminance gradient from the original image.

Trailing edges: Higher luminance gradients are more likely to be edges, but there is no exact value to limit how much luminance gradients are edges, so canny uses hysteresis thresholds-high and low thresholds.

When the above steps are complete, we get a binary graph with each point indicating whether it is an edge point.

# Canny算子canny = cv2.Canny(image, 50, 240)plt.figure(12,figsize=(10,30))plt.subplot(121)plt.imshow(image,cmap=‘gray‘) # 彩色图像显示异常,plt采用RGB模式,而cv2采用BGR模式plt.title(‘raw_img‘)plt.subplot(122)plt.imshow(canny,cmap=‘gray‘)plt.title(‘Canny‘)

Canny operator use of a difficult point is the high threshold and low threshold choice, in fact, for any threshold choice, are faced with a dilemma, set too high, may miss some information, set too low, will be the noise as an important signal to deal with, it is very much like machine learning accuracy rate and recall relationship.

The canny operator is suitable for different occasions, and its parameters allow for the adaptation of specific requirements according to different implementations to identify different edge characteristics.

####################### #小 ********** Knot ###############################

1, the three edge detection operator explained here, the use is relatively simple, the more difficult is to understand its intrinsic meaning.

2, from the effect point of view, I personally prefer canny operator, because from the picture can be seen, the least noise, get the best edge effect.

#################################################################


Note: This section of the code has been uploaded to ( my GitHub), Welcome to download.

Resources:

1, Python machine learning classic example, Prateek Joshi, Tao Junjie, Chen Xiaoli translation

"Furnace-Refining AI" machine learning 046-image edge detection method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.